|
6 | 6 | use Exception;
|
7 | 7 | use GuzzleHttp\Psr7\Utils;
|
8 | 8 | use Illuminate\Support\Facades\Cache;
|
| 9 | +use Illuminate\Support\Facades\Log; |
9 | 10 | use Intervention\Image\Decoders\BinaryImageDecoder;
|
10 | 11 | use Intervention\Image\Drivers\Gd\Decoders\NativeObjectDecoder;
|
11 | 12 | use Intervention\Image\Drivers\Gd\Driver;
|
@@ -93,8 +94,8 @@ public function resizeToThumbnailUrl(
|
93 | 94 |
|
94 | 95 | $imageData = $disk->get($imagePath);
|
95 | 96 |
|
96 |
| - // Do not resize apng images where we're not cropping |
97 |
| - if ($keepRatio && $this->isApngData($image, $imageData)) { |
| 97 | + // Do not resize animated images where we're not cropping |
| 98 | + if ($keepRatio && $this->isAnimated($image, $imageData)) { |
98 | 99 | Cache::put($thumbCacheKey, $image->path, static::THUMBNAIL_CACHE_TIME);
|
99 | 100 |
|
100 | 101 | return $this->storage->getPublicUrl($image->path);
|
@@ -240,15 +241,50 @@ protected function getExtension(Image $image): string
|
240 | 241 | /**
|
241 | 242 | * Check if the given image and image data is apng.
|
242 | 243 | */
|
243 |
| - protected function isApngData(Image $image, string &$imageData): bool |
| 244 | + protected function isApngData(string &$imageData): bool |
244 | 245 | {
|
245 |
| - $isPng = strtolower(pathinfo($image->path, PATHINFO_EXTENSION)) === 'png'; |
246 |
| - if (!$isPng) { |
| 246 | + $initialHeader = substr($imageData, 0, strpos($imageData, 'IDAT')); |
| 247 | + |
| 248 | + return str_contains($initialHeader, 'acTL'); |
| 249 | + } |
| 250 | + |
| 251 | + /** |
| 252 | + * Check if the given avif image data represents an animated image. |
| 253 | + * This is based up the answer here: https://stackoverflow.com/a/79457313 |
| 254 | + */ |
| 255 | + protected function isAnimatedAvifData(string &$imageData): bool |
| 256 | + { |
| 257 | + $stszPos = strpos($imageData, 'stsz'); |
| 258 | + if ($stszPos === false) { |
247 | 259 | return false;
|
248 | 260 | }
|
249 | 261 |
|
250 |
| - $initialHeader = substr($imageData, 0, strpos($imageData, 'IDAT')); |
| 262 | + // Look 12 bytes after the start of 'stsz' |
| 263 | + $start = $stszPos + 12; |
| 264 | + $end = $start + 4; |
| 265 | + if ($end > strlen($imageData) - 1) { |
| 266 | + return false; |
| 267 | + } |
251 | 268 |
|
252 |
| - return str_contains($initialHeader, 'acTL'); |
| 269 | + $data = substr($imageData, $start, 4); |
| 270 | + $count = unpack('Nvalue', $data)['value']; |
| 271 | + return $count > 1; |
| 272 | + } |
| 273 | + |
| 274 | + /** |
| 275 | + * Check if the given image is animated. |
| 276 | + */ |
| 277 | + protected function isAnimated(Image $image, string &$imageData): bool |
| 278 | + { |
| 279 | + $extension = strtolower(pathinfo($image->path, PATHINFO_EXTENSION)); |
| 280 | + if ($extension === 'png') { |
| 281 | + return $this->isApngData($imageData); |
| 282 | + } |
| 283 | + |
| 284 | + if ($extension === 'avif') { |
| 285 | + return $this->isAnimatedAvifData($imageData); |
| 286 | + } |
| 287 | + |
| 288 | + return false; |
253 | 289 | }
|
254 | 290 | }
|
0 commit comments