nystudio107 /
craft-imageoptimize
| 1 | <?php |
||
| 2 | /** |
||
|
0 ignored issues
–
show
Coding Style
introduced
by
Loading history...
|
|||
| 3 | * @link https://craftcms.com/ |
||
|
0 ignored issues
–
show
|
|||
| 4 | * @copyright Copyright (c) Pixel & Tonic, Inc. |
||
|
0 ignored issues
–
show
|
|||
| 5 | * @license https://craftcms.github.io/license/ |
||
|
0 ignored issues
–
show
|
|||
| 6 | */ |
||
|
0 ignored issues
–
show
|
|||
| 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 | /** |
||
|
0 ignored issues
–
show
|
|||
| 19 | * @author nystudio107 |
||
|
0 ignored issues
–
show
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
|
|||
| 20 | * @package ImageOptimize |
||
|
0 ignored issues
–
show
|
|||
| 21 | * @since 1.6.4 |
||
|
0 ignored issues
–
show
|
|||
| 22 | */ |
||
|
0 ignored issues
–
show
|
|||
| 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 |
||
|
0 ignored issues
–
show
|
|||
| 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( |
||
|
0 ignored issues
–
show
|
|||
| 68 | 'app', |
||
| 69 | 'Not enough memory available to perform this image operation.' |
||
| 70 | )); |
||
|
0 ignored issues
–
show
For multi-line function calls, the closing parenthesis should be on a new line.
If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line: someFunctionCall(
$firstArgument,
$secondArgument,
$thirdArgument
); // Closing parenthesis on a new line.
Loading history...
|
|||
| 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( |
||
|
0 ignored issues
–
show
|
|||
| 78 | 'app', |
||
| 79 | 'The file “{name}” does not appear to be an image.', |
||
| 80 | ['name' => pathinfo($path, PATHINFO_BASENAME)] |
||
| 81 | )); |
||
|
0 ignored issues
–
show
For multi-line function calls, the closing parenthesis should be on a new line.
If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line: someFunctionCall(
$firstArgument,
$secondArgument,
$thirdArgument
); // Closing parenthesis on a new line.
Loading history...
|
|||
| 82 | } |
||
| 83 | |||
| 84 | try { |
||
| 85 | $image = $instance->open($path); |
||
| 86 | } catch (Throwable $e) { |
||
| 87 | throw new ImageException(Craft::t( |
||
|
0 ignored issues
–
show
|
|||
| 88 | 'app', |
||
| 89 | 'The file “{path}” does not appear to be an image.', |
||
| 90 | ['path' => $path] |
||
| 91 | ), 0, $e); |
||
|
0 ignored issues
–
show
For multi-line function calls, the closing parenthesis should be on a new line.
If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line: someFunctionCall(
$firstArgument,
$secondArgument,
$thirdArgument
); // Closing parenthesis on a new line.
Loading history...
|
|||
| 92 | } |
||
| 93 | |||
| 94 | $extension = pathinfo($path, PATHINFO_EXTENSION); |
||
| 95 | |||
| 96 | return $extension === 'gif' && $image->layers()->count() > 1; |
||
| 97 | } |
||
| 98 | } |
||
| 99 |