Image   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 12
eloc 37
c 0
b 0
f 0
dl 0
loc 74
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
C getIsAnimatedGif() 0 58 12
1
<?php
2
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
3
 * @link      https://craftcms.com/
0 ignored issues
show
Coding Style introduced by
The tag in position 1 should be the @copyright tag
Loading history...
4
 * @copyright Copyright (c) Pixel & Tonic, Inc.
0 ignored issues
show
Coding Style introduced by
@copyright tag must contain a year and the name of the copyright holder
Loading history...
5
 * @license   https://craftcms.github.io/license/
0 ignored issues
show
Coding Style introduced by
@license tag must contain a URL and a license name
Loading history...
6
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
Coding Style introduced by
Missing @package tag in file comment
Loading history...
Coding Style introduced by
Missing @author tag in file comment
Loading history...
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
Coding Style introduced by
Missing short description in doc comment
Loading history...
19
 * @author    nystudio107
0 ignored issues
show
Coding Style introduced by
Tag value for @author tag indented incorrectly; expected 2 spaces but found 4
Loading history...
Coding Style introduced by
The tag in position 1 should be the @package tag
Loading history...
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
20
 * @package   ImageOptimize
0 ignored issues
show
Coding Style introduced by
Tag value for @package tag indented incorrectly; expected 1 spaces but found 3
Loading history...
21
 * @since     1.6.4
0 ignored issues
show
Coding Style introduced by
The tag in position 3 should be the @author tag
Loading history...
Coding Style introduced by
Tag value for @since tag indented incorrectly; expected 3 spaces but found 5
Loading history...
22
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
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
Coding Style introduced by
Missing parameter comment
Loading history...
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
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
68
                'app',
69
                'Not enough memory available to perform this image operation.'
70
            ));
0 ignored issues
show
Coding Style introduced by
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
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
78
                'app',
79
                'The file “{name}” does not appear to be an image.',
80
                ['name' => pathinfo($path, PATHINFO_BASENAME)]
81
            ));
0 ignored issues
show
Coding Style introduced by
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
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
88
                'app',
89
                'The file “{path}” does not appear to be an image.',
90
                ['path' => $path]
91
            ), 0, $e);
0 ignored issues
show
Coding Style introduced by
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