|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Image Optimize plugin for Craft CMS |
|
4
|
|
|
* |
|
5
|
|
|
* Automatically optimize images after they've been transformed |
|
6
|
|
|
* |
|
7
|
|
|
* @link https://nystudio107.com |
|
|
|
|
|
|
8
|
|
|
* @copyright Copyright (c) nystudio107 |
|
|
|
|
|
|
9
|
|
|
*/ |
|
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
namespace nystudio107\imageoptimize\models; |
|
12
|
|
|
|
|
13
|
|
|
use craft\helpers\Html; |
|
14
|
|
|
use craft\helpers\Template; |
|
15
|
|
|
use Twig\Markup; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
|
|
|
|
|
18
|
|
|
* @author nystudio107 |
|
|
|
|
|
|
19
|
|
|
* @package ImageOptimize |
|
|
|
|
|
|
20
|
|
|
* @since 5.0.0-beta.1 |
|
|
|
|
|
|
21
|
|
|
*/ |
|
|
|
|
|
|
22
|
|
|
class ImgTag extends BaseImageTag |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
|
|
|
|
|
25
|
|
|
* @var string The loading scheme to use: 'eager', 'lazy', 'lazySizes', 'lazySizesFallback' |
|
26
|
|
|
*/ |
|
27
|
|
|
public string $loadingStrategy = 'eager'; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
|
|
|
|
|
30
|
|
|
* @var string The type of placeholder image to use: 'box', 'color', 'image', 'silhouette', or 'none' |
|
31
|
|
|
*/ |
|
32
|
|
|
public string $placeholder = 'box'; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
|
|
|
|
|
35
|
|
|
* @var array array of tag attributes for the <img> tag |
|
36
|
|
|
*/ |
|
37
|
|
|
public array $imgAttrs = []; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
|
|
|
|
|
40
|
|
|
* @inheritDoc |
|
41
|
|
|
*/ |
|
|
|
|
|
|
42
|
|
|
public function init(): void |
|
43
|
|
|
{ |
|
44
|
|
|
parent::init(); |
|
45
|
|
|
// Populate the $imageAttrs |
|
46
|
|
|
$this->imgAttrs = [ |
|
47
|
|
|
'class' => '', |
|
48
|
|
|
'style' => '', |
|
49
|
|
|
'width' => $this->optimizedImage->placeholderWidth, |
|
50
|
|
|
'height' => $this->optimizedImage->placeholderHeight, |
|
51
|
|
|
'src' => reset($this->optimizedImage->optimizedImageUrls), |
|
|
|
|
|
|
52
|
|
|
'srcset' => $this->optimizedImage->getSrcsetFromArray($this->optimizedImage->optimizedImageUrls), |
|
|
|
|
|
|
53
|
|
|
'sizes' => '100vw', |
|
54
|
|
|
'loading' => '', |
|
55
|
|
|
]; |
|
56
|
|
|
// If the original image is an SVG or gif, don't add the placeholder box CSS so that transparency works as intended |
|
57
|
|
|
$path = parse_url($this->imgAttrs['src'], PHP_URL_PATH); |
|
58
|
|
|
$extension = pathinfo($path, PATHINFO_EXTENSION); |
|
59
|
|
|
if ($extension === 'svg' || $extension === 'gif') { |
|
60
|
|
|
$this->placeholder = 'none'; |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Set the $loading property |
|
66
|
|
|
* |
|
67
|
|
|
* @param string $value |
|
|
|
|
|
|
68
|
|
|
* @return $this |
|
|
|
|
|
|
69
|
|
|
*/ |
|
70
|
|
|
public function loadingStrategy(string $value): ImgTag |
|
71
|
|
|
{ |
|
72
|
|
|
$this->loadingStrategy = $value; |
|
73
|
|
|
|
|
74
|
|
|
return $this; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Set the $placeholder property |
|
79
|
|
|
* |
|
80
|
|
|
* @param string $value |
|
|
|
|
|
|
81
|
|
|
* @return $this |
|
|
|
|
|
|
82
|
|
|
*/ |
|
83
|
|
|
public function placeholder(string $value): ImgTag |
|
84
|
|
|
{ |
|
85
|
|
|
$this->placeholder = $value; |
|
86
|
|
|
|
|
87
|
|
|
return $this; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Merge the passed array of tag attributes into $imgAttrs |
|
92
|
|
|
* |
|
93
|
|
|
* @param array $value |
|
|
|
|
|
|
94
|
|
|
* @return $this |
|
|
|
|
|
|
95
|
|
|
*/ |
|
96
|
|
|
public function imgAttrs(array $value): ImgTag |
|
97
|
|
|
{ |
|
98
|
|
|
$this->imgAttrs = array_merge($this->imgAttrs, $value); |
|
99
|
|
|
|
|
100
|
|
|
return $this; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Generate a complete <img> tag for the $optimizedImage OptimizedImage model |
|
105
|
|
|
* |
|
106
|
|
|
* @return Markup |
|
107
|
|
|
*/ |
|
108
|
|
|
public function render(): Markup |
|
109
|
|
|
{ |
|
110
|
|
|
$attrs = $this->imgAttrs; |
|
111
|
|
|
// Handle lazy loading |
|
112
|
|
|
if ($this->loadingStrategy !== 'eager') { |
|
113
|
|
|
$attrs = $this->swapLazyLoadAttrs($this->loadingStrategy, $this->placeholder, $attrs); |
|
114
|
|
|
} |
|
115
|
|
|
// Remove any empty attributes |
|
116
|
|
|
$attrs = $this->filterEmptyAttributes($attrs); |
|
117
|
|
|
// Render the tag |
|
118
|
|
|
$tag = Html::tag('img', '', $attrs); |
|
119
|
|
|
|
|
120
|
|
|
return Template::raw($tag); |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|