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' |
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
|
|
|
* @param $config |
|
|
|
|
41
|
|
|
*/ |
42
|
|
|
public function __construct($config = []) |
43
|
|
|
{ |
44
|
|
|
parent::__construct($config); |
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
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Set the $loading property |
60
|
|
|
* |
61
|
|
|
* @param string $value |
|
|
|
|
62
|
|
|
* @return $this |
|
|
|
|
63
|
|
|
*/ |
64
|
|
|
public function loadingStrategy(string $value): ImgTag |
65
|
|
|
{ |
66
|
|
|
$this->loadingStrategy = $value; |
67
|
|
|
|
68
|
|
|
return $this; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Set the $placeholder property |
73
|
|
|
* |
74
|
|
|
* @param string $value |
|
|
|
|
75
|
|
|
* @return $this |
|
|
|
|
76
|
|
|
*/ |
77
|
|
|
public function placeholder(string $value): ImgTag |
78
|
|
|
{ |
79
|
|
|
$this->placeholder = $value; |
80
|
|
|
|
81
|
|
|
return $this; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Merge the passed array of tag attributes into $imgAttrs |
86
|
|
|
* |
87
|
|
|
* @param array $value |
|
|
|
|
88
|
|
|
* @return $this |
|
|
|
|
89
|
|
|
*/ |
90
|
|
|
public function imgAttrs(array $value): ImgTag |
91
|
|
|
{ |
92
|
|
|
$this->imgAttrs = array_merge($this->imgAttrs, $value); |
93
|
|
|
|
94
|
|
|
return $this; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Generate a complete <img> tag for the $optimizedImage OptimizedImage model |
99
|
|
|
* |
100
|
|
|
* @return Markup |
101
|
|
|
*/ |
102
|
|
|
public function render(): Markup |
103
|
|
|
{ |
104
|
|
|
$attrs = $this->imgAttrs; |
105
|
|
|
// Handle lazy loading |
106
|
|
|
if ($this->loadingStrategy !== 'eager') { |
107
|
|
|
$attrs = $this->swapLazyLoadAttrs($this->loadingStrategy, $this->placeholder, $attrs); |
108
|
|
|
} |
109
|
|
|
// Remove any empty attributes |
110
|
|
|
$attrs = array_filter($attrs); |
111
|
|
|
// Render the tag |
112
|
|
|
$tag = Html::tag('img', '', $attrs); |
113
|
|
|
|
114
|
|
|
return Template::raw($tag); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|