1 | <?php |
||
30 | class EasyImage extends Component |
||
31 | { |
||
32 | /** |
||
33 | * GD2 driver definition for Imagine implementation using the GD library. |
||
34 | */ |
||
35 | const DRIVER_GD2 = 'gd2'; |
||
36 | /** |
||
37 | * imagick driver definition. |
||
38 | */ |
||
39 | const DRIVER_IMAGICK = 'imagick'; |
||
40 | /** |
||
41 | * gmagick driver definition. |
||
42 | */ |
||
43 | const DRIVER_GMAGICK = 'gmagick'; |
||
44 | /** |
||
45 | * @var string alias for webroot folder |
||
46 | */ |
||
47 | public $webrootAlias = '@webroot'; |
||
48 | /** |
||
49 | * @var string relative path where the cache files are kept |
||
50 | */ |
||
51 | public $cachePath = '/assets/easyimage/'; |
||
52 | /** |
||
53 | * @var int cache lifetime in seconds |
||
54 | */ |
||
55 | public $cacheTime = 2592000; |
||
56 | /** |
||
57 | * @var array output image options |
||
58 | */ |
||
59 | public $imageOptions = [ |
||
60 | 'quality' => 100, |
||
61 | ]; |
||
62 | /** |
||
63 | * Additional tools to work with images |
||
64 | * ['toolName' => 'toolClass'] |
||
65 | * Tool class must implement yiicod\easyimage\base\ToolInterface |
||
66 | * |
||
67 | * @var array |
||
68 | */ |
||
69 | public $tools = []; |
||
70 | /** |
||
71 | * Default empty image in "base64" string |
||
72 | * |
||
73 | * @var string |
||
74 | */ |
||
75 | public $emptyImage = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAAA1BMVEUAAACnej3aAAAAAXRSTlMAQObYZgAAAApJREFUCNdjYAAAAAIAAeIhvDMAAAAASUVORK5CYII='; |
||
76 | /** |
||
77 | * @var array|string the driver to use. This can be either a single driver name or an array of driver names. |
||
78 | * If the latter, the first available driver will be used. |
||
79 | */ |
||
80 | public static $driver = [self::DRIVER_IMAGICK, self::DRIVER_GMAGICK, self::DRIVER_GD2]; |
||
81 | /** |
||
82 | * Imagine instance |
||
83 | * |
||
84 | * @var ImagineInterface |
||
85 | */ |
||
86 | protected static $imagine; |
||
87 | |||
88 | /** |
||
89 | * Init component |
||
90 | */ |
||
91 | public function init() |
||
97 | |||
98 | /** |
||
99 | * Initialize default tools |
||
100 | */ |
||
101 | protected function initTools() |
||
114 | |||
115 | /** |
||
116 | * Returns the `Imagine` object that supports various image manipulations. |
||
117 | * |
||
118 | * @return ImagineInterface the `Imagine` object |
||
119 | */ |
||
120 | public static function getImagine(): ImagineInterface |
||
128 | |||
129 | /** |
||
130 | * @param ImagineInterface $imagine the `Imagine` object |
||
131 | */ |
||
132 | public static function setImagine(ImagineInterface $imagine) |
||
136 | |||
137 | /** |
||
138 | * Creates an `Imagine` object based on the specified [[driver]]. |
||
139 | * |
||
140 | * @return ImagineInterface the new `Imagine` object |
||
141 | * |
||
142 | * @throws Exception |
||
143 | */ |
||
144 | protected static function createImagine(): ImagineInterface |
||
169 | |||
170 | /** |
||
171 | * Get url for cached image using available tools |
||
172 | * |
||
173 | * @param string $file |
||
174 | * @param array $params |
||
175 | * @param bool $absolute |
||
176 | * |
||
177 | * @return string |
||
178 | */ |
||
179 | public function getUrl(string $file, array $params = [], $absolute = false): string |
||
189 | |||
190 | /** |
||
191 | * Get path for cached image using available tools |
||
192 | * |
||
193 | * @param string $file |
||
194 | * @param array $params |
||
195 | * |
||
196 | * @return string |
||
197 | */ |
||
198 | public function getPath(string $file, array $params = []): string |
||
208 | |||
209 | /** |
||
210 | * Get component instance |
||
211 | * Added for better IDE support |
||
212 | * |
||
213 | * @return mixed |
||
214 | */ |
||
215 | public static function getInstance(): EasyImage |
||
219 | |||
220 | /** |
||
221 | * Get cahced image |
||
222 | * |
||
223 | * @param $file |
||
224 | * @param array $params |
||
225 | * |
||
226 | * @return bool|string |
||
227 | */ |
||
228 | protected function getCacheFile(string $file, array $params = []) |
||
249 | |||
250 | /** |
||
251 | * Create url for cached image |
||
252 | * |
||
253 | * @param string $filePath |
||
254 | * @param bool $absolute |
||
255 | * |
||
256 | * @return string |
||
257 | */ |
||
258 | protected function createUrl(string $filePath, $absolute = false): string |
||
262 | |||
263 | /** |
||
264 | * Create path for cached image |
||
265 | * |
||
266 | * @param string $filePath |
||
267 | * |
||
268 | * @return string |
||
269 | */ |
||
270 | protected function createPath(string $filePath): string |
||
274 | |||
275 | /** |
||
276 | * Generate cached image path |
||
277 | * |
||
278 | * @param string $file |
||
279 | * @param array $params |
||
280 | * |
||
281 | * @return string |
||
282 | */ |
||
283 | protected function generateCacheFilePath(string $file, array $params = []): string |
||
296 | |||
297 | /** |
||
298 | * Create new image using available tools |
||
299 | * |
||
300 | * @param string $file |
||
301 | * @param array $params |
||
302 | * @param string $savePath |
||
303 | * |
||
304 | * @return \Imagine\Image\ManipulatorInterface |
||
305 | */ |
||
306 | protected function createImage(string $file, array $params, string $savePath): ManipulatorInterface |
||
322 | |||
323 | /** |
||
324 | * Make directory to save cache image |
||
325 | * |
||
326 | * @param string $savePath |
||
327 | */ |
||
328 | protected function makeDir(string $savePath) |
||
334 | |||
335 | /** |
||
336 | * Get default empty image |
||
337 | * |
||
338 | * @return string |
||
339 | */ |
||
340 | protected function getEmptyImage(): string |
||
344 | |||
345 | /** |
||
346 | * Log exception |
||
347 | * |
||
348 | * @param Exception $e |
||
349 | */ |
||
350 | protected function log(Exception $e) |
||
354 | } |
||
355 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: