diff --git a/src/models/OptimizedImage.php b/src/models/OptimizedImage.php
index ab4d4036..175003b9 100644
--- a/src/models/OptimizedImage.php
+++ b/src/models/OptimizedImage.php
@@ -12,7 +12,6 @@
use Craft;
use craft\base\Model;
-use craft\helpers\Html;
use craft\helpers\Template;
use craft\validators\ArrayValidator;
use nystudio107\imageoptimize\helpers\Color as ColorHelper;
@@ -384,153 +383,31 @@ public function colorPaletteRgb(): array
* Generate a complete tag for this OptimizedImages model
* ref: https://web.dev/preload-responsive-images/#imagesrcset-and-imagesizes
*
- * @param array $linkAttrs
- *
- * @return Markup
+ * @return LinkPreloadTag
*/
- public function linkPreloadTag($linkAttrs = [])
+ public function linkPreloadTag(): LinkPreloadTag
{
- // Any web browser that supports link rel="preload" as="image" also supports webp, so prefer that
- $srcset = $this->optimizedImageUrls;
- if (!empty($this->optimizedWebPImageUrls)) {
- $srcset = $this->optimizedWebPImageUrls;
- }
- // Merge the passed in options with the tag attributes
- $attrs = array_merge([
- 'rel' => 'preload',
- 'as' => 'image',
- 'href' => reset($srcset),
- 'imagesrcset' => $this->getSrcsetFromArray($srcset),
- 'imagesizes' => '100vw',
- ],
- $linkAttrs
- );
- // Remove any empty attributes
- $attrs = array_filter($attrs);
- // Render the tag
- $tag = Html::tag('link', '', $attrs);
-
- return Template::raw($tag);
+ return new LinkPreloadTag(['optimizedImage' => $this]);
}
/**
- * Generate a complete tag for this OptimizedImages model
- *
- * @param string $loading 'eager', 'lazy', 'lazySizes', 'lazySizesFallback'
- * @param string $placeHolder 'box', 'color', 'image', 'silhouette'
- * @param array $imgAttrs
+ * Generate a complete tag for this OptimizedImage model
*
- * @return Markup
+ * @return ImgTag
*/
- public function imgTag($loading = 'eager', $placeHolder = 'box', $imgAttrs = []): Markup
- {
- // Merge the passed in options with the tag attributes
- $attrs = array_merge([
- 'class' => '',
- 'style' => '',
- 'width' => $this->placeholderWidth,
- 'height' => $this->placeholderHeight,
- 'src' => reset($this->optimizedImageUrls),
- 'srcset' => $this->getSrcsetFromArray($this->optimizedImageUrls),
- 'sizes' => '100vw',
- 'loading' => '',
- ],
- $imgAttrs
- );
- // Handle lazy loading
- if ($loading !== 'eager') {
- $attrs = $this->swapLazyLoadAttrs($loading, $placeHolder, $attrs);
- }
- // Remove any empty attributes
- $attrs = array_filter($attrs);
- // Render the tag
- $tag = Html::tag('img', '', $attrs);
-
- return Template::raw($tag);
+ public function imgTag(): ImgTag
+ {
+ return new ImgTag(['optimizedImage' => $this]);
}
/**
- * Generate a complete