nystudio107 /
craft-imageoptimize
| 1 | <?php |
||||
| 2 | /** |
||||
| 3 | * ImageOptimize plugin for Craft CMS |
||||
| 4 | * |
||||
| 5 | * Automatically optimize images after they've been transformed |
||||
| 6 | * |
||||
| 7 | * @link https://nystudio107.com |
||||
|
0 ignored issues
–
show
Coding Style
introduced
by
Loading history...
|
|||||
| 8 | * @copyright Copyright (c) 2018 nystudio107 |
||||
|
0 ignored issues
–
show
|
|||||
| 9 | */ |
||||
|
0 ignored issues
–
show
|
|||||
| 10 | |||||
| 11 | namespace nystudio107\imageoptimize\imagetransforms; |
||||
| 12 | |||||
| 13 | use Craft; |
||||
| 14 | use craft\base\SavableComponent; |
||||
| 15 | use craft\elements\Asset; |
||||
| 16 | use craft\helpers\FileHelper; |
||||
| 17 | use craft\helpers\StringHelper; |
||||
| 18 | use craft\models\ImageTransform as CraftImageTransformModel; |
||||
| 19 | use nystudio107\imageoptimize\helpers\UrlHelper; |
||||
| 20 | use ReflectionClass; |
||||
| 21 | |||||
| 22 | /** |
||||
|
0 ignored issues
–
show
|
|||||
| 23 | * @author nystudio107 |
||||
|
0 ignored issues
–
show
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
|
|||||
| 24 | * @package ImageOptimize |
||||
|
0 ignored issues
–
show
|
|||||
| 25 | * @since 1.6.0 |
||||
|
0 ignored issues
–
show
|
|||||
| 26 | */ |
||||
|
0 ignored issues
–
show
|
|||||
| 27 | abstract class ImageTransform extends SavableComponent implements ImageTransformInterface |
||||
| 28 | { |
||||
| 29 | // Traits |
||||
| 30 | // ========================================================================= |
||||
| 31 | |||||
| 32 | use ImageTransformTrait; |
||||
| 33 | |||||
| 34 | // Static Methods |
||||
| 35 | // ========================================================================= |
||||
| 36 | |||||
| 37 | /** |
||||
|
0 ignored issues
–
show
|
|||||
| 38 | * @inheritdoc |
||||
| 39 | */ |
||||
|
0 ignored issues
–
show
|
|||||
| 40 | public static function displayName(): string |
||||
| 41 | { |
||||
| 42 | return Craft::t('image-optimize', 'Generic Transform'); |
||||
| 43 | } |
||||
| 44 | |||||
| 45 | /** |
||||
|
0 ignored issues
–
show
|
|||||
| 46 | * @inheritdoc |
||||
| 47 | */ |
||||
|
0 ignored issues
–
show
|
|||||
| 48 | public static function getTemplatesRoot(): array |
||||
| 49 | { |
||||
| 50 | $reflect = new ReflectionClass(static::class); |
||||
| 51 | $classPath = FileHelper::normalizePath( |
||||
| 52 | dirname($reflect->getFileName()) |
||||
|
0 ignored issues
–
show
|
|||||
| 53 | . '/../templates' |
||||
| 54 | ) |
||||
|
0 ignored issues
–
show
|
|||||
| 55 | . DIRECTORY_SEPARATOR; |
||||
| 56 | $id = StringHelper::toKebabCase($reflect->getShortName()); |
||||
| 57 | |||||
| 58 | return [$id, $classPath]; |
||||
| 59 | } |
||||
| 60 | |||||
| 61 | // Public Methods |
||||
| 62 | // ========================================================================= |
||||
| 63 | |||||
| 64 | /** |
||||
|
0 ignored issues
–
show
|
|||||
| 65 | * @inheritdoc |
||||
| 66 | */ |
||||
|
0 ignored issues
–
show
|
|||||
| 67 | public function getTransformUrl(Asset $asset, CraftImageTransformModel|string|array|null $transform): ?string |
||||
| 68 | { |
||||
| 69 | return null; |
||||
| 70 | } |
||||
| 71 | |||||
| 72 | /** |
||||
|
0 ignored issues
–
show
|
|||||
| 73 | * @inheritdoc |
||||
| 74 | */ |
||||
|
0 ignored issues
–
show
|
|||||
| 75 | public function getWebPUrl(string $url, Asset $asset, CraftImageTransformModel|string|array|null $transform): ?string |
||||
| 76 | { |
||||
| 77 | return $url; |
||||
| 78 | } |
||||
| 79 | |||||
| 80 | /** |
||||
|
0 ignored issues
–
show
|
|||||
| 81 | * @inheritdoc |
||||
| 82 | */ |
||||
|
0 ignored issues
–
show
|
|||||
| 83 | public function getPurgeUrl(Asset $asset): ?string |
||||
| 84 | { |
||||
| 85 | return null; |
||||
| 86 | } |
||||
| 87 | |||||
| 88 | /** |
||||
|
0 ignored issues
–
show
|
|||||
| 89 | * @inheritdoc |
||||
| 90 | */ |
||||
|
0 ignored issues
–
show
|
|||||
| 91 | public function purgeUrl(string $url): bool |
||||
| 92 | { |
||||
| 93 | return true; |
||||
| 94 | } |
||||
| 95 | |||||
| 96 | /** |
||||
|
0 ignored issues
–
show
|
|||||
| 97 | * @inheritdoc |
||||
| 98 | */ |
||||
|
0 ignored issues
–
show
|
|||||
| 99 | public function getAssetUri(Asset $asset): ?string |
||||
| 100 | { |
||||
| 101 | $volume = $asset->getVolume(); |
||||
| 102 | $assetPath = $volume->getSubpath() . $asset->getPath(); |
||||
| 103 | |||||
| 104 | // Account for volume types with a subfolder setting |
||||
| 105 | // e.g. craftcms/aws-s3, craftcms/google-cloud |
||||
| 106 | if ($volume->getFs()->subfolder ?? null) { |
||||
| 107 | $subfolder = $volume->getFs()->subfolder; |
||||
| 108 | $subfolder = Craft::parseEnv($subfolder); |
||||
|
0 ignored issues
–
show
The function
Craft::parseEnv() has been deprecated: in 3.7.29. [[App::parseEnv()]] should be used instead.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This function has been deprecated. The supplier of the function has supplied an explanatory message. The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead. Loading history...
|
|||||
| 109 | return rtrim($subfolder, '/') . '/' . $assetPath; |
||||
|
0 ignored issues
–
show
It seems like
$subfolder can also be of type null; however, parameter $string of rtrim() does only seem to accept string, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 110 | } |
||||
| 111 | |||||
| 112 | return $assetPath; |
||||
| 113 | } |
||||
| 114 | |||||
| 115 | /** |
||||
|
0 ignored issues
–
show
|
|||||
| 116 | * @param string $url |
||||
|
0 ignored issues
–
show
|
|||||
| 117 | * @noinspection PhpComposerExtensionStubsInspection |
||||
|
0 ignored issues
–
show
|
|||||
| 118 | */ |
||||
|
0 ignored issues
–
show
|
|||||
| 119 | public function prefetchRemoteFile(string $url): void |
||||
| 120 | { |
||||
| 121 | // Get an absolute URL with protocol that curl will be happy with |
||||
| 122 | $url = UrlHelper::absoluteUrlWithProtocol($url); |
||||
| 123 | $ch = curl_init($url); |
||||
| 124 | curl_setopt_array($ch, [ |
||||
|
0 ignored issues
–
show
|
|||||
| 125 | CURLOPT_RETURNTRANSFER => 1, |
||||
| 126 | CURLOPT_FOLLOWLOCATION => 1, |
||||
| 127 | CURLOPT_SSL_VERIFYPEER => 0, |
||||
| 128 | CURLOPT_NOBODY => 1, |
||||
| 129 | ]); |
||||
|
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...
|
|||||
| 130 | curl_exec($ch); |
||||
| 131 | curl_close($ch); |
||||
| 132 | } |
||||
| 133 | |||||
| 134 | /** |
||||
| 135 | * Append an extension a passed url or path |
||||
| 136 | * |
||||
| 137 | * @param $pathOrUrl |
||||
|
0 ignored issues
–
show
|
|||||
| 138 | * @param $extension |
||||
|
0 ignored issues
–
show
|
|||||
| 139 | * |
||||
| 140 | * @return string |
||||
| 141 | */ |
||||
| 142 | public function appendExtension($pathOrUrl, $extension): string |
||||
| 143 | { |
||||
| 144 | $path = $this->decomposeUrl($pathOrUrl); |
||||
| 145 | $path_parts = pathinfo($path['path']); |
||||
| 146 | $new_path = ($path_parts['filename']) . '.' . ($path_parts['extension'] ?? '') . $extension; |
||||
| 147 | if (!empty($path_parts['dirname']) && $path_parts['dirname'] !== '.') { |
||||
| 148 | $dirname = $path_parts['dirname']; |
||||
| 149 | $dirname = $dirname === '/' ? '' : $dirname; |
||||
| 150 | $new_path = $dirname . DIRECTORY_SEPARATOR . $new_path; |
||||
| 151 | $new_path = preg_replace('/([^:])(\/{2,})/', '$1/', $new_path); |
||||
| 152 | } |
||||
| 153 | |||||
| 154 | return $path['prefix'] . $new_path . $path['suffix']; |
||||
| 155 | } |
||||
| 156 | |||||
| 157 | // Protected Methods |
||||
| 158 | // ========================================================================= |
||||
| 159 | |||||
| 160 | /** |
||||
| 161 | * Decompose a URL into a prefix, path, and suffix |
||||
| 162 | * |
||||
| 163 | * @param $pathOrUrl |
||||
|
0 ignored issues
–
show
|
|||||
| 164 | * |
||||
| 165 | * @return array |
||||
| 166 | */ |
||||
| 167 | protected function decomposeUrl($pathOrUrl): array |
||||
| 168 | { |
||||
| 169 | $result = array(); |
||||
| 170 | |||||
| 171 | if (filter_var($pathOrUrl, FILTER_VALIDATE_URL)) { |
||||
| 172 | $url_parts = parse_url($pathOrUrl); |
||||
| 173 | $result['prefix'] = $url_parts['scheme'] . '://' . $url_parts['host']; |
||||
| 174 | if (!empty($url_parts['port'])) { |
||||
| 175 | $result['prefix'] .= ':' . $url_parts['port']; |
||||
| 176 | } |
||||
| 177 | $result['path'] = $url_parts['path']; |
||||
| 178 | $result['suffix'] = ''; |
||||
| 179 | $result['suffix'] .= empty($url_parts['query']) ? '' : '?' . $url_parts['query']; |
||||
| 180 | $result['suffix'] .= empty($url_parts['fragment']) ? '' : '#' . $url_parts['fragment']; |
||||
| 181 | } else { |
||||
| 182 | $result['prefix'] = ''; |
||||
| 183 | $result['path'] = $pathOrUrl; |
||||
| 184 | $result['suffix'] = ''; |
||||
| 185 | } |
||||
| 186 | |||||
| 187 | return $result; |
||||
| 188 | } |
||||
| 189 | } |
||||
| 190 |