1
|
|
|
<?php |
2
|
|
|
/** |
|
|
|
|
3
|
|
|
* @link https://craftcms.com/ |
|
|
|
|
4
|
|
|
* @copyright Copyright (c) Pixel & Tonic, Inc. |
|
|
|
|
5
|
|
|
* @license https://craftcms.github.io/license/ |
|
|
|
|
6
|
|
|
*/ |
|
|
|
|
7
|
|
|
|
8
|
|
|
namespace nystudio107\imageoptimize\helpers; |
9
|
|
|
|
10
|
|
|
use Craft; |
11
|
|
|
use craft\errors\ImageException; |
12
|
|
|
use craft\helpers\FileHelper; |
13
|
|
|
use Imagine\Gd\Imagine as GdImagine; |
14
|
|
|
use Imagine\Imagick\Imagine as ImagickImagine; |
15
|
|
|
use Throwable; |
16
|
|
|
use yii\base\InvalidConfigException; |
17
|
|
|
|
18
|
|
|
/** |
|
|
|
|
19
|
|
|
* @author nystudio107 |
|
|
|
|
20
|
|
|
* @package ImageOptimize |
|
|
|
|
21
|
|
|
* @since 1.6.4 |
|
|
|
|
22
|
|
|
*/ |
|
|
|
|
23
|
|
|
class Image |
24
|
|
|
{ |
25
|
|
|
// Public Static Methods |
26
|
|
|
// ========================================================================= |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* This is a hacked version of `Raster::loadImage()` because the $_isAnimatedGif |
30
|
|
|
* property is private and there is no `getIsAnimatedGif()` getter |
31
|
|
|
* |
32
|
|
|
* @param string $path |
|
|
|
|
33
|
|
|
* |
34
|
|
|
* @return bool |
35
|
|
|
* |
36
|
|
|
* @throws ImageException |
37
|
|
|
* @throws InvalidConfigException |
38
|
|
|
*/ |
39
|
|
|
public static function getIsAnimatedGif(string $path): bool |
40
|
|
|
{ |
41
|
|
|
$generalConfig = Craft::$app->getConfig()->getGeneral(); |
42
|
|
|
|
43
|
|
|
$extension = strtolower($generalConfig->imageDriver); |
44
|
|
|
|
45
|
|
|
// If it's explicitly set, take their word for it. |
46
|
|
|
if ($extension === 'gd') { |
47
|
|
|
$instance = new GdImagine(); |
48
|
|
|
} elseif ($extension === 'imagick') { |
49
|
|
|
$instance = new ImagickImagine(); |
50
|
|
|
} elseif (Craft::$app->getImages()->getIsGd()) { |
51
|
|
|
$instance = new GdImagine(); |
52
|
|
|
} else { |
53
|
|
|
$instance = new ImagickImagine(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$imageService = Craft::$app->getImages(); |
57
|
|
|
if ($imageService->getIsGd()) { |
58
|
|
|
return false; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
if (!is_file($path)) { |
62
|
|
|
Craft::error('Tried to load an image at ' . $path . ', but the file does not exist.', __METHOD__); |
63
|
|
|
throw new ImageException(Craft::t('app', 'No file exists at the given path.')); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
if (!$imageService->checkMemoryForImage($path)) { |
67
|
|
|
throw new ImageException(Craft::t( |
|
|
|
|
68
|
|
|
'app', |
69
|
|
|
'Not enough memory available to perform this image operation.' |
70
|
|
|
)); |
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
// Make sure the image says it's an image |
74
|
|
|
$mimeType = FileHelper::getMimeType($path, null, false); |
75
|
|
|
|
76
|
|
|
if ($mimeType !== null && !str_starts_with($mimeType, 'image/') && !str_starts_with($mimeType, 'application/pdf')) { |
77
|
|
|
throw new ImageException(Craft::t( |
|
|
|
|
78
|
|
|
'app', |
79
|
|
|
'The file “{name}” does not appear to be an image.', |
80
|
|
|
['name' => pathinfo($path, PATHINFO_BASENAME)] |
81
|
|
|
)); |
|
|
|
|
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
try { |
85
|
|
|
$image = $instance->open($path); |
86
|
|
|
} catch (Throwable $e) { |
87
|
|
|
throw new ImageException(Craft::t( |
|
|
|
|
88
|
|
|
'app', |
89
|
|
|
'The file “{path}” does not appear to be an image.', |
90
|
|
|
['path' => $path] |
91
|
|
|
), 0, $e); |
|
|
|
|
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$extension = pathinfo($path, PATHINFO_EXTENSION); |
95
|
|
|
|
96
|
|
|
return $extension === 'gif' && $image->layers()->count() > 1; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|