16577bfc3SGreg Roach<?php 26577bfc3SGreg Roach 36577bfc3SGreg Roach/** 46577bfc3SGreg Roach * webtrees: online genealogy 56577bfc3SGreg Roach * Copyright (C) 2020 webtrees development team 66577bfc3SGreg Roach * This program is free software: you can redistribute it and/or modify 76577bfc3SGreg Roach * it under the terms of the GNU General Public License as published by 86577bfc3SGreg Roach * the Free Software Foundation, either version 3 of the License, or 96577bfc3SGreg Roach * (at your option) any later version. 106577bfc3SGreg Roach * This program is distributed in the hope that it will be useful, 116577bfc3SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 126577bfc3SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 136577bfc3SGreg Roach * GNU General Public License for more details. 146577bfc3SGreg Roach * You should have received a copy of the GNU General Public License 156577bfc3SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>. 166577bfc3SGreg Roach */ 176577bfc3SGreg Roach 186577bfc3SGreg Roachdeclare(strict_types=1); 196577bfc3SGreg Roach 206577bfc3SGreg Roachnamespace Fisharebest\Webtrees\Factories; 216577bfc3SGreg Roach 226577bfc3SGreg Roachuse Fig\Http\Message\StatusCodeInterface; 236577bfc3SGreg Roachuse Fisharebest\Webtrees\Auth; 246577bfc3SGreg Roachuse Fisharebest\Webtrees\Contracts\ImageFactoryInterface; 256577bfc3SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface; 266577bfc3SGreg Roachuse Fisharebest\Webtrees\Factory; 276577bfc3SGreg Roachuse Fisharebest\Webtrees\MediaFile; 286577bfc3SGreg Roachuse Fisharebest\Webtrees\Webtrees; 296577bfc3SGreg Roachuse Imagick; 306577bfc3SGreg Roachuse Intervention\Image\Constraint; 316577bfc3SGreg Roachuse Intervention\Image\Exception\NotReadableException; 326577bfc3SGreg Roachuse Intervention\Image\Exception\NotSupportedException; 336577bfc3SGreg Roachuse Intervention\Image\Image; 346577bfc3SGreg Roachuse Intervention\Image\ImageManager; 356577bfc3SGreg Roachuse League\Flysystem\FileNotFoundException; 366577bfc3SGreg Roachuse League\Flysystem\FilesystemInterface; 376577bfc3SGreg Roachuse Psr\Http\Message\ResponseInterface; 386577bfc3SGreg Roachuse RuntimeException; 396577bfc3SGreg Roachuse Throwable; 406577bfc3SGreg Roach 416577bfc3SGreg Roachuse function addcslashes; 426577bfc3SGreg Roachuse function basename; 436577bfc3SGreg Roachuse function extension_loaded; 446577bfc3SGreg Roachuse function pathinfo; 456577bfc3SGreg Roachuse function response; 466577bfc3SGreg Roachuse function strlen; 476577bfc3SGreg Roachuse function view; 486577bfc3SGreg Roach 496577bfc3SGreg Roachuse const PATHINFO_EXTENSION; 506577bfc3SGreg Roach 516577bfc3SGreg Roach/** 526577bfc3SGreg Roach * Make an image (from another image). 536577bfc3SGreg Roach */ 546577bfc3SGreg Roachclass ImageFactory implements ImageFactoryInterface 556577bfc3SGreg Roach{ 566577bfc3SGreg Roach // Imagick can detect the quality setting for images. GD cannot. 576577bfc3SGreg Roach protected const GD_DEFAULT_IMAGE_QUALITY = 90; 586577bfc3SGreg Roach protected const GD_DEFAULT_THUMBNAIL_QUALITY = 70; 596577bfc3SGreg Roach 606577bfc3SGreg Roach protected const WATERMARK_FILE = 'resources/img/watermark.png'; 616577bfc3SGreg Roach 626577bfc3SGreg Roach protected const THUMBNAIL_CACHE_TTL = 8640000; 636577bfc3SGreg Roach 646577bfc3SGreg Roach protected const INTERVENTION_DRIVERS = ['imagick', 'gd']; 656577bfc3SGreg Roach 666577bfc3SGreg Roach protected const INTERVENTION_FORMATS = [ 676577bfc3SGreg Roach 'image/jpeg' => 'jpg', 686577bfc3SGreg Roach 'image/png' => 'png', 696577bfc3SGreg Roach 'image/gif' => 'gif', 706577bfc3SGreg Roach 'image/tiff' => 'tif', 716577bfc3SGreg Roach 'image/bmp' => 'bmp', 726577bfc3SGreg Roach 'image/webp' => 'webp', 736577bfc3SGreg Roach ]; 746577bfc3SGreg Roach 756577bfc3SGreg Roach /** 766577bfc3SGreg Roach * Send the original file - either inline or as a download. 776577bfc3SGreg Roach * 786577bfc3SGreg Roach * @param FilesystemInterface $filesystem 796577bfc3SGreg Roach * @param string $path 806577bfc3SGreg Roach * @param bool $download 816577bfc3SGreg Roach * 826577bfc3SGreg Roach * @return ResponseInterface 836577bfc3SGreg Roach */ 846577bfc3SGreg Roach public function fileResponse(FilesystemInterface $filesystem, string $path, bool $download): ResponseInterface 856577bfc3SGreg Roach { 866577bfc3SGreg Roach try { 876577bfc3SGreg Roach $data = $filesystem->read($path); 886577bfc3SGreg Roach 896577bfc3SGreg Roach $headers = [ 906577bfc3SGreg Roach 'Content-Type' => $filesystem->getMimetype($path), 916577bfc3SGreg Roach 'Content-Length' => (string) strlen($data), 926577bfc3SGreg Roach ]; 936577bfc3SGreg Roach 946577bfc3SGreg Roach if ($download) { 956577bfc3SGreg Roach $headers['Content-Disposition'] = 'attachment; filename="' . addcslashes(basename($path), '"'); 966577bfc3SGreg Roach } 976577bfc3SGreg Roach 986577bfc3SGreg Roach return response($data, StatusCodeInterface::STATUS_OK, $headers); 996577bfc3SGreg Roach } catch (FileNotFoundException $ex) { 1006577bfc3SGreg Roach return $this->replacementImageResponse((string) StatusCodeInterface::STATUS_NOT_FOUND); 1016577bfc3SGreg Roach } 1026577bfc3SGreg Roach } 1036577bfc3SGreg Roach 1046577bfc3SGreg Roach /** 1056577bfc3SGreg Roach * Send a thumbnail. 1066577bfc3SGreg Roach * 1076577bfc3SGreg Roach * @param FilesystemInterface $filesystem 1086577bfc3SGreg Roach * @param string $path 1096577bfc3SGreg Roach * @param int $width 1106577bfc3SGreg Roach * @param int $height 1116577bfc3SGreg Roach * @param string $fit 1126577bfc3SGreg Roach * 1136577bfc3SGreg Roach * 1146577bfc3SGreg Roach * @return ResponseInterface 1156577bfc3SGreg Roach */ 1166577bfc3SGreg Roach public function thumbnailResponse( 1176577bfc3SGreg Roach FilesystemInterface $filesystem, 1186577bfc3SGreg Roach string $path, 1196577bfc3SGreg Roach int $width, 1206577bfc3SGreg Roach int $height, 1216577bfc3SGreg Roach string $fit 1226577bfc3SGreg Roach ): ResponseInterface { 1236577bfc3SGreg Roach try { 1246577bfc3SGreg Roach $image = $this->imageManager()->make($filesystem->readStream($path)); 1256577bfc3SGreg Roach $image = $this->autorotateImage($image); 1266577bfc3SGreg Roach $image = $this->resizeImage($image, $width, $height, $fit); 1276577bfc3SGreg Roach 1286577bfc3SGreg Roach $format = static::INTERVENTION_FORMATS[$image->mime()] ?? 'jpg'; 1296577bfc3SGreg Roach $quality = $this->extractImageQuality($image, static::GD_DEFAULT_THUMBNAIL_QUALITY); 1306577bfc3SGreg Roach $data = (string) $image->encode($format, $quality); 1316577bfc3SGreg Roach 1326577bfc3SGreg Roach return $this->imageResponse($data, $image->mime(), ''); 1336577bfc3SGreg Roach } catch (NotReadableException $ex) { 1346577bfc3SGreg Roach return $this->replacementImageResponse('.' . pathinfo($path, PATHINFO_EXTENSION)); 1356577bfc3SGreg Roach } catch (FileNotFoundException $ex) { 1366577bfc3SGreg Roach return $this->replacementImageResponse((string) StatusCodeInterface::STATUS_NOT_FOUND); 1376577bfc3SGreg Roach } catch (Throwable $ex) { 1386577bfc3SGreg Roach return $this->replacementImageResponse((string) StatusCodeInterface::STATUS_INTERNAL_SERVER_ERROR) 1396577bfc3SGreg Roach ->withHeader('X-Thumbnail-Exception', $ex->getMessage()); 1406577bfc3SGreg Roach } 1416577bfc3SGreg Roach } 1426577bfc3SGreg Roach 1436577bfc3SGreg Roach /** 1446577bfc3SGreg Roach * Create a full-size version of an image. 1456577bfc3SGreg Roach * 1466577bfc3SGreg Roach * @param MediaFile $media_file 1476577bfc3SGreg Roach * @param bool $add_watermark 1486577bfc3SGreg Roach * @param bool $download 1496577bfc3SGreg Roach * 1506577bfc3SGreg Roach * @return ResponseInterface 1516577bfc3SGreg Roach */ 1526577bfc3SGreg Roach public function mediaFileResponse(MediaFile $media_file, bool $add_watermark, bool $download): ResponseInterface 1536577bfc3SGreg Roach { 1546577bfc3SGreg Roach $filesystem = Factory::filesystem()->media($media_file->media()->tree()); 1556577bfc3SGreg Roach $filename = $media_file->filename(); 1566577bfc3SGreg Roach 1576577bfc3SGreg Roach if (!$add_watermark || !$media_file->isImage()) { 1586577bfc3SGreg Roach return $this->fileResponse($filesystem, $filename, $download); 1596577bfc3SGreg Roach } 1606577bfc3SGreg Roach 1616577bfc3SGreg Roach try { 1626577bfc3SGreg Roach $image = $this->imageManager()->make($filesystem->readStream($filename)); 1636577bfc3SGreg Roach $image = $this->autorotateImage($image); 1646577bfc3SGreg Roach 1656577bfc3SGreg Roach $watermark_image = $this->createWatermark($image->width(), $image->height(), $media_file); 1666577bfc3SGreg Roach 1676577bfc3SGreg Roach $image = $this->addWatermark($image, $watermark_image); 1686577bfc3SGreg Roach 1696577bfc3SGreg Roach $download_filename = $download ? basename($filename) : ''; 1706577bfc3SGreg Roach 1716577bfc3SGreg Roach $format = static::INTERVENTION_FORMATS[$image->mime()] ?? 'jpg'; 1726577bfc3SGreg Roach $quality = $this->extractImageQuality($image, static::GD_DEFAULT_IMAGE_QUALITY); 1736577bfc3SGreg Roach $data = (string) $image->encode($format, $quality); 1746577bfc3SGreg Roach 1756577bfc3SGreg Roach return $this->imageResponse($data, $image->mime(), $download_filename); 1766577bfc3SGreg Roach } catch (NotReadableException $ex) { 1776577bfc3SGreg Roach return $this->replacementImageResponse(pathinfo($filename, PATHINFO_EXTENSION)) 1786577bfc3SGreg Roach ->withHeader('X-Image-Exception', $ex->getMessage()); 1796577bfc3SGreg Roach } catch (FileNotFoundException $ex) { 1806577bfc3SGreg Roach return $this->replacementImageResponse((string) StatusCodeInterface::STATUS_NOT_FOUND); 1816577bfc3SGreg Roach } catch (Throwable $ex) { 1826577bfc3SGreg Roach return $this->replacementImageResponse((string) StatusCodeInterface::STATUS_INTERNAL_SERVER_ERROR) 1836577bfc3SGreg Roach ->withHeader('X-Image-Exception', $ex->getMessage()); 1846577bfc3SGreg Roach } 1856577bfc3SGreg Roach } 1866577bfc3SGreg Roach 1876577bfc3SGreg Roach /** 1886577bfc3SGreg Roach * Create a smaller version of an image. 1896577bfc3SGreg Roach * 1906577bfc3SGreg Roach * @param MediaFile $media_file 1916577bfc3SGreg Roach * @param int $width 1926577bfc3SGreg Roach * @param int $height 1936577bfc3SGreg Roach * @param string $fit 1946577bfc3SGreg Roach * @param bool $add_watermark 1956577bfc3SGreg Roach * 1966577bfc3SGreg Roach * @return ResponseInterface 1976577bfc3SGreg Roach */ 1986577bfc3SGreg Roach public function mediaFileThumbnailResponse( 1996577bfc3SGreg Roach MediaFile $media_file, 2006577bfc3SGreg Roach int $width, 2016577bfc3SGreg Roach int $height, 2026577bfc3SGreg Roach string $fit, 2036577bfc3SGreg Roach bool $add_watermark 2046577bfc3SGreg Roach ): ResponseInterface { 2056577bfc3SGreg Roach // Where are the images stored. 2066577bfc3SGreg Roach $filesystem = Factory::filesystem()->media($media_file->media()->tree()); 2076577bfc3SGreg Roach 2086577bfc3SGreg Roach // Where is the image stored in the filesystem. 2096577bfc3SGreg Roach $path = $media_file->filename(); 2106577bfc3SGreg Roach 2116577bfc3SGreg Roach try { 2126577bfc3SGreg Roach $mime_type = $filesystem->getMimetype($path); 2136577bfc3SGreg Roach 2146577bfc3SGreg Roach $key = implode(':', [ 2156577bfc3SGreg Roach $media_file->media()->tree()->name(), 2166577bfc3SGreg Roach $path, 2176577bfc3SGreg Roach $filesystem->getTimestamp($path), 2186577bfc3SGreg Roach (string) $width, 2196577bfc3SGreg Roach (string) $height, 2206577bfc3SGreg Roach $fit, 2216577bfc3SGreg Roach (string) $add_watermark, 2226577bfc3SGreg Roach ]); 2236577bfc3SGreg Roach 2246577bfc3SGreg Roach $closure = function () use ($filesystem, $path, $width, $height, $fit, $add_watermark, $media_file): string { 2256577bfc3SGreg Roach $image = $this->imageManager()->make($filesystem->readStream($path)); 2266577bfc3SGreg Roach $image = $this->autorotateImage($image); 2276577bfc3SGreg Roach $image = $this->resizeImage($image, $width, $height, $fit); 2286577bfc3SGreg Roach 2296577bfc3SGreg Roach if ($add_watermark) { 2306577bfc3SGreg Roach $watermark = $this->createWatermark($image->width(), $image->height(), $media_file); 2316577bfc3SGreg Roach $image = $this->addWatermark($image, $watermark); 2326577bfc3SGreg Roach } 2336577bfc3SGreg Roach 2346577bfc3SGreg Roach $format = static::INTERVENTION_FORMATS[$image->mime()] ?? 'jpg'; 2356577bfc3SGreg Roach $quality = $this->extractImageQuality($image, static::GD_DEFAULT_THUMBNAIL_QUALITY); 2366577bfc3SGreg Roach 2376577bfc3SGreg Roach return (string) $image->encode($format, $quality); 2386577bfc3SGreg Roach }; 2396577bfc3SGreg Roach 2406577bfc3SGreg Roach // Images and Responses both contain resources - which cannot be serialized. 2416577bfc3SGreg Roach // So cache the raw image data. 2426577bfc3SGreg Roach $data = Factory::cache()->file()->remember($key, $closure, static::THUMBNAIL_CACHE_TTL); 2436577bfc3SGreg Roach 2446577bfc3SGreg Roach return $this->imageResponse($data, $mime_type, ''); 2456577bfc3SGreg Roach } catch (NotReadableException $ex) { 2466577bfc3SGreg Roach return $this->replacementImageResponse('.' . pathinfo($path, PATHINFO_EXTENSION)); 2476577bfc3SGreg Roach } catch (FileNotFoundException $ex) { 2486577bfc3SGreg Roach return $this->replacementImageResponse((string) StatusCodeInterface::STATUS_NOT_FOUND); 2496577bfc3SGreg Roach } catch (Throwable $ex) { 2506577bfc3SGreg Roach return $this->replacementImageResponse((string) StatusCodeInterface::STATUS_INTERNAL_SERVER_ERROR) 2516577bfc3SGreg Roach ->withHeader('X-Thumbnail-Exception', $ex->getMessage()); 2526577bfc3SGreg Roach } 2536577bfc3SGreg Roach } 2546577bfc3SGreg Roach 2556577bfc3SGreg Roach /** 2566577bfc3SGreg Roach * Does a full-sized image need a watermark? 2576577bfc3SGreg Roach * 2586577bfc3SGreg Roach * @param MediaFile $media_file 2596577bfc3SGreg Roach * @param UserInterface $user 2606577bfc3SGreg Roach * 2616577bfc3SGreg Roach * @return bool 2626577bfc3SGreg Roach */ 2636577bfc3SGreg Roach public function fileNeedsWatermark(MediaFile $media_file, UserInterface $user): bool 2646577bfc3SGreg Roach { 2656577bfc3SGreg Roach $tree = $media_file->media()->tree(); 2666577bfc3SGreg Roach 2676577bfc3SGreg Roach return Auth::accessLevel($tree, $user) > $tree->getPreference('SHOW_NO_WATERMARK'); 2686577bfc3SGreg Roach } 2696577bfc3SGreg Roach 2706577bfc3SGreg Roach /** 2716577bfc3SGreg Roach * Does a thumbnail image need a watermark? 2726577bfc3SGreg Roach * 2736577bfc3SGreg Roach * @param MediaFile $media_file 2746577bfc3SGreg Roach * @param UserInterface $user 2756577bfc3SGreg Roach * 2766577bfc3SGreg Roach * @return bool 2776577bfc3SGreg Roach */ 2786577bfc3SGreg Roach public function thumbnailNeedsWatermark(MediaFile $media_file, UserInterface $user): bool 2796577bfc3SGreg Roach { 2806577bfc3SGreg Roach return $this->fileNeedsWatermark($media_file, $user); 2816577bfc3SGreg Roach } 2826577bfc3SGreg Roach 2836577bfc3SGreg Roach /** 2846577bfc3SGreg Roach * Create a watermark image, perhaps specific to a media-file. 2856577bfc3SGreg Roach * 2866577bfc3SGreg Roach * @param int $width 2876577bfc3SGreg Roach * @param int $height 2886577bfc3SGreg Roach * @param MediaFile $media_file 2896577bfc3SGreg Roach * 2906577bfc3SGreg Roach * @return Image 2916577bfc3SGreg Roach */ 2926577bfc3SGreg Roach public function createWatermark(int $width, int $height, MediaFile $media_file): Image 2936577bfc3SGreg Roach { 2946577bfc3SGreg Roach return $this->imageManager() 2956577bfc3SGreg Roach ->make(Webtrees::ROOT_DIR . static::WATERMARK_FILE) 2966577bfc3SGreg Roach ->resize($width, $height, static function (Constraint $constraint) { 2976577bfc3SGreg Roach $constraint->aspectRatio(); 2986577bfc3SGreg Roach }); 2996577bfc3SGreg Roach } 3006577bfc3SGreg Roach 3016577bfc3SGreg Roach /** 3026577bfc3SGreg Roach * Add a watermark to an image. 3036577bfc3SGreg Roach * 3046577bfc3SGreg Roach * @param Image $image 3056577bfc3SGreg Roach * @param Image $watermark 3066577bfc3SGreg Roach * 3076577bfc3SGreg Roach * @return Image 3086577bfc3SGreg Roach */ 3096577bfc3SGreg Roach public function addWatermark(Image $image, Image $watermark): Image 3106577bfc3SGreg Roach { 3116577bfc3SGreg Roach return $image->insert($watermark, 'center'); 3126577bfc3SGreg Roach } 3136577bfc3SGreg Roach 3146577bfc3SGreg Roach /** 3156577bfc3SGreg Roach * Send a replacement image, to replace one that could not be found or created. 3166577bfc3SGreg Roach * 3176577bfc3SGreg Roach * @param string $text HTTP status code or file extension 3186577bfc3SGreg Roach * 3196577bfc3SGreg Roach * @return ResponseInterface 3206577bfc3SGreg Roach */ 3216577bfc3SGreg Roach public function replacementImageResponse(string $text): ResponseInterface 3226577bfc3SGreg Roach { 3236577bfc3SGreg Roach // We can't create a PNG/BMP/JPEG image, as the GD/IMAGICK libraries may be missing. 3246577bfc3SGreg Roach $svg = view('errors/image-svg', ['status' => $text]); 3256577bfc3SGreg Roach 3266577bfc3SGreg Roach // We can't send the actual status code, as browsers won't show images with 4xx/5xx. 3276577bfc3SGreg Roach return response($svg, StatusCodeInterface::STATUS_OK, [ 3286577bfc3SGreg Roach 'Content-Type' => 'image/svg+xml', 3296577bfc3SGreg Roach ]); 3306577bfc3SGreg Roach } 3316577bfc3SGreg Roach 3326577bfc3SGreg Roach /** 3336577bfc3SGreg Roach * @param string $data 3346577bfc3SGreg Roach * @param string $mime_type 3356577bfc3SGreg Roach * @param string $filename 3366577bfc3SGreg Roach * 3376577bfc3SGreg Roach * @return ResponseInterface 3386577bfc3SGreg Roach */ 3396577bfc3SGreg Roach protected function imageResponse(string $data, string $mime_type, string $filename): ResponseInterface 3406577bfc3SGreg Roach { 3416577bfc3SGreg Roach $headers = [ 3426577bfc3SGreg Roach 'Content-Type' => $mime_type, 3436577bfc3SGreg Roach 'Content-Length' => (string) strlen($data), 3446577bfc3SGreg Roach ]; 3456577bfc3SGreg Roach 3466577bfc3SGreg Roach if ($filename !== '') { 3476577bfc3SGreg Roach $headers['Content-Disposition'] = 'attachment; filename="' . addcslashes(basename($filename), '"'); 3486577bfc3SGreg Roach } 3496577bfc3SGreg Roach 3506577bfc3SGreg Roach return response($data, StatusCodeInterface::STATUS_OK, $headers); 3516577bfc3SGreg Roach } 3526577bfc3SGreg Roach 3536577bfc3SGreg Roach /** 3546577bfc3SGreg Roach * @return ImageManager 3556577bfc3SGreg Roach * @throws RuntimeException 3566577bfc3SGreg Roach */ 3576577bfc3SGreg Roach protected function imageManager(): ImageManager 3586577bfc3SGreg Roach { 3596577bfc3SGreg Roach foreach (static::INTERVENTION_DRIVERS as $driver) { 3606577bfc3SGreg Roach if (extension_loaded($driver)) { 3616577bfc3SGreg Roach return new ImageManager(['driver' => $driver]); 3626577bfc3SGreg Roach } 3636577bfc3SGreg Roach } 3646577bfc3SGreg Roach 3656577bfc3SGreg Roach throw new RuntimeException('No PHP graphics library is installed. Need Imagick or GD'); 3666577bfc3SGreg Roach } 3676577bfc3SGreg Roach 3686577bfc3SGreg Roach /** 3696577bfc3SGreg Roach * Apply EXIF rotation to an image. 3706577bfc3SGreg Roach * 3716577bfc3SGreg Roach * @param Image $image 3726577bfc3SGreg Roach * 3736577bfc3SGreg Roach * @return Image 3746577bfc3SGreg Roach */ 3756577bfc3SGreg Roach protected function autorotateImage(Image $image): Image 3766577bfc3SGreg Roach { 3776577bfc3SGreg Roach try { 3786577bfc3SGreg Roach // Auto-rotate using EXIF information. 3796577bfc3SGreg Roach return $image->orientate(); 3806577bfc3SGreg Roach } catch (NotSupportedException $ex) { 3816577bfc3SGreg Roach // If we can't auto-rotate the image, then don't. 3826577bfc3SGreg Roach return $image; 3836577bfc3SGreg Roach } 3846577bfc3SGreg Roach } 3856577bfc3SGreg Roach 3866577bfc3SGreg Roach /** 3876577bfc3SGreg Roach * Resize an image. 3886577bfc3SGreg Roach * 3896577bfc3SGreg Roach * @param Image $image 3906577bfc3SGreg Roach * @param int $width 3916577bfc3SGreg Roach * @param int $height 3926577bfc3SGreg Roach * @param string $fit 3936577bfc3SGreg Roach * 3946577bfc3SGreg Roach * @return Image 3956577bfc3SGreg Roach */ 3966577bfc3SGreg Roach protected function resizeImage(Image $image, int $width, int $height, string $fit): Image 3976577bfc3SGreg Roach { 3986577bfc3SGreg Roach switch ($fit) { 3996577bfc3SGreg Roach case 'crop': 4006577bfc3SGreg Roach return $image->fit($width, $height); 4016577bfc3SGreg Roach case 'contain': 4026577bfc3SGreg Roach return $image->resize($width, $height, static function (Constraint $constraint) { 4036577bfc3SGreg Roach $constraint->aspectRatio(); 4046577bfc3SGreg Roach $constraint->upsize(); 4056577bfc3SGreg Roach }); 4066577bfc3SGreg Roach } 4076577bfc3SGreg Roach 4086577bfc3SGreg Roach return $image; 4096577bfc3SGreg Roach } 4106577bfc3SGreg Roach 4116577bfc3SGreg Roach /** 4126577bfc3SGreg Roach * Extract the quality/compression parameter from an image. 4136577bfc3SGreg Roach * 4146577bfc3SGreg Roach * @param Image $image 4156577bfc3SGreg Roach * @param int $default 4166577bfc3SGreg Roach * 4176577bfc3SGreg Roach * @return int 4186577bfc3SGreg Roach */ 4196577bfc3SGreg Roach protected function extractImageQuality(Image $image, int $default): int 4206577bfc3SGreg Roach { 4216577bfc3SGreg Roach $core = $image->getCore(); 4226577bfc3SGreg Roach 4236577bfc3SGreg Roach if ($core instanceof Imagick) { 424*bf26501bSGreg Roach return $core->getImageCompressionQuality() ?: $default; 4256577bfc3SGreg Roach } 4266577bfc3SGreg Roach 4276577bfc3SGreg Roach return $default; 4286577bfc3SGreg Roach } 4296577bfc3SGreg Roach} 430