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