xref: /webtrees/app/Factories/ImageFactory.php (revision 6d24947fac0fb841ad23303de6f03229d5232220)
16577bfc3SGreg Roach<?php
26577bfc3SGreg Roach
36577bfc3SGreg Roach/**
46577bfc3SGreg Roach * webtrees: online genealogy
589f7189bSGreg Roach * Copyright (C) 2021 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
1589f7189bSGreg Roach * along with this program. If not, see <https://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\MediaFile;
276b9cb339SGreg Roachuse Fisharebest\Webtrees\Registry;
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;
35f7cf8a15SGreg Roachuse League\Flysystem\FilesystemException;
36f7cf8a15SGreg Roachuse League\Flysystem\FilesystemOperator;
37f32d77e6SGreg Roachuse League\Flysystem\UnableToReadFile;
386577bfc3SGreg Roachuse Psr\Http\Message\ResponseInterface;
396577bfc3SGreg Roachuse RuntimeException;
406577bfc3SGreg Roachuse Throwable;
416577bfc3SGreg Roach
426577bfc3SGreg Roachuse function addcslashes;
436577bfc3SGreg Roachuse function basename;
446577bfc3SGreg Roachuse function extension_loaded;
45f32d77e6SGreg Roachuse function get_class;
466577bfc3SGreg Roachuse function pathinfo;
476577bfc3SGreg Roachuse function response;
486577bfc3SGreg Roachuse function strlen;
496577bfc3SGreg Roachuse function view;
506577bfc3SGreg Roach
516577bfc3SGreg Roachuse const PATHINFO_EXTENSION;
526577bfc3SGreg Roach
536577bfc3SGreg Roach/**
546577bfc3SGreg Roach * Make an image (from another image).
556577bfc3SGreg Roach */
566577bfc3SGreg Roachclass ImageFactory implements ImageFactoryInterface
576577bfc3SGreg Roach{
586577bfc3SGreg Roach    // Imagick can detect the quality setting for images.  GD cannot.
596577bfc3SGreg Roach    protected const GD_DEFAULT_IMAGE_QUALITY     = 90;
606577bfc3SGreg Roach    protected const GD_DEFAULT_THUMBNAIL_QUALITY = 70;
616577bfc3SGreg Roach
626577bfc3SGreg Roach    protected const WATERMARK_FILE = 'resources/img/watermark.png';
636577bfc3SGreg Roach
646577bfc3SGreg Roach    protected const THUMBNAIL_CACHE_TTL = 8640000;
656577bfc3SGreg Roach
666577bfc3SGreg Roach    protected const INTERVENTION_DRIVERS = ['imagick', 'gd'];
676577bfc3SGreg Roach
686577bfc3SGreg Roach    protected const INTERVENTION_FORMATS = [
696577bfc3SGreg Roach        'image/jpeg' => 'jpg',
706577bfc3SGreg Roach        'image/png'  => 'png',
716577bfc3SGreg Roach        'image/gif'  => 'gif',
726577bfc3SGreg Roach        'image/tiff' => 'tif',
736577bfc3SGreg Roach        'image/bmp'  => 'bmp',
746577bfc3SGreg Roach        'image/webp' => 'webp',
756577bfc3SGreg Roach    ];
766577bfc3SGreg Roach
776577bfc3SGreg Roach    /**
786577bfc3SGreg Roach     * Send the original file - either inline or as a download.
796577bfc3SGreg Roach     *
80f7cf8a15SGreg Roach     * @param FilesystemOperator $filesystem
816577bfc3SGreg Roach     * @param string             $path
826577bfc3SGreg Roach     * @param bool               $download
836577bfc3SGreg Roach     *
846577bfc3SGreg Roach     * @return ResponseInterface
856577bfc3SGreg Roach     */
86f7cf8a15SGreg Roach    public function fileResponse(FilesystemOperator $filesystem, string $path, bool $download): ResponseInterface
876577bfc3SGreg Roach    {
886577bfc3SGreg Roach        try {
896577bfc3SGreg Roach            $data = $filesystem->read($path);
906577bfc3SGreg Roach
916577bfc3SGreg Roach            $headers = [
92f7cf8a15SGreg Roach                'Content-Type'   => $filesystem->mimeType($path),
936577bfc3SGreg Roach                'Content-Length' => (string) strlen($data),
946577bfc3SGreg Roach            ];
956577bfc3SGreg Roach
966577bfc3SGreg Roach            if ($download) {
976577bfc3SGreg Roach                $headers['Content-Disposition'] = 'attachment; filename="' . addcslashes(basename($path), '"');
986577bfc3SGreg Roach            }
996577bfc3SGreg Roach
1006577bfc3SGreg Roach            return response($data, StatusCodeInterface::STATUS_OK, $headers);
101f7cf8a15SGreg Roach        } catch (FilesystemException $ex) {
1026577bfc3SGreg Roach            return $this->replacementImageResponse((string) StatusCodeInterface::STATUS_NOT_FOUND);
1036577bfc3SGreg Roach        }
1046577bfc3SGreg Roach    }
1056577bfc3SGreg Roach
1066577bfc3SGreg Roach    /**
1076577bfc3SGreg Roach     * Send a thumbnail.
1086577bfc3SGreg Roach     *
109f7cf8a15SGreg Roach     * @param FilesystemOperator $filesystem
1106577bfc3SGreg Roach     * @param string             $path
1116577bfc3SGreg Roach     * @param int                $width
1126577bfc3SGreg Roach     * @param int                $height
1136577bfc3SGreg Roach     * @param string             $fit
1146577bfc3SGreg Roach     *
1156577bfc3SGreg Roach     *
1166577bfc3SGreg Roach     * @return ResponseInterface
1176577bfc3SGreg Roach     */
1186577bfc3SGreg Roach    public function thumbnailResponse(
119f7cf8a15SGreg Roach        FilesystemOperator $filesystem,
1206577bfc3SGreg Roach        string $path,
1216577bfc3SGreg Roach        int $width,
1226577bfc3SGreg Roach        int $height,
1236577bfc3SGreg Roach        string $fit
1246577bfc3SGreg Roach    ): ResponseInterface {
1256577bfc3SGreg Roach        try {
1266577bfc3SGreg Roach            $image = $this->imageManager()->make($filesystem->readStream($path));
1276577bfc3SGreg Roach            $image = $this->autorotateImage($image);
1286577bfc3SGreg Roach            $image = $this->resizeImage($image, $width, $height, $fit);
1296577bfc3SGreg Roach
1306577bfc3SGreg Roach            $format  = static::INTERVENTION_FORMATS[$image->mime()] ?? 'jpg';
1316577bfc3SGreg Roach            $quality = $this->extractImageQuality($image, static::GD_DEFAULT_THUMBNAIL_QUALITY);
1326577bfc3SGreg Roach            $data    = (string) $image->encode($format, $quality);
1336577bfc3SGreg Roach
1346577bfc3SGreg Roach            return $this->imageResponse($data, $image->mime(), '');
1356577bfc3SGreg Roach        } catch (NotReadableException $ex) {
1366577bfc3SGreg Roach            return $this->replacementImageResponse('.' . pathinfo($path, PATHINFO_EXTENSION));
137f32d77e6SGreg Roach        } catch (UnableToReadFile $ex) {
1386577bfc3SGreg Roach            return $this->replacementImageResponse((string) StatusCodeInterface::STATUS_NOT_FOUND);
1396577bfc3SGreg Roach        } catch (Throwable $ex) {
1406577bfc3SGreg Roach            return $this->replacementImageResponse((string) StatusCodeInterface::STATUS_INTERNAL_SERVER_ERROR)
141*6d24947fSGreg Roach                ->withHeader('X-Thumbnail-Exception', get_class($ex) . ': ' . $ex->getMessage());
1426577bfc3SGreg Roach        }
1436577bfc3SGreg Roach    }
1446577bfc3SGreg Roach
1456577bfc3SGreg Roach    /**
1466577bfc3SGreg Roach     * Create a full-size version of an image.
1476577bfc3SGreg Roach     *
1486577bfc3SGreg Roach     * @param MediaFile $media_file
1496577bfc3SGreg Roach     * @param bool      $add_watermark
1506577bfc3SGreg Roach     * @param bool      $download
1516577bfc3SGreg Roach     *
1526577bfc3SGreg Roach     * @return ResponseInterface
1536577bfc3SGreg Roach     */
1546577bfc3SGreg Roach    public function mediaFileResponse(MediaFile $media_file, bool $add_watermark, bool $download): ResponseInterface
1556577bfc3SGreg Roach    {
1566b9cb339SGreg Roach        $filesystem = Registry::filesystem()->media($media_file->media()->tree());
1576577bfc3SGreg Roach        $filename   = $media_file->filename();
1586577bfc3SGreg Roach
1596577bfc3SGreg Roach        if (!$add_watermark || !$media_file->isImage()) {
1606577bfc3SGreg Roach            return $this->fileResponse($filesystem, $filename, $download);
1616577bfc3SGreg Roach        }
1626577bfc3SGreg Roach
1636577bfc3SGreg Roach        try {
1646577bfc3SGreg Roach            $image = $this->imageManager()->make($filesystem->readStream($filename));
1656577bfc3SGreg Roach            $image = $this->autorotateImage($image);
1666577bfc3SGreg Roach
1676577bfc3SGreg Roach            $watermark_image = $this->createWatermark($image->width(), $image->height(), $media_file);
1686577bfc3SGreg Roach
1696577bfc3SGreg Roach            $image = $this->addWatermark($image, $watermark_image);
1706577bfc3SGreg Roach
1716577bfc3SGreg Roach            $download_filename = $download ? basename($filename) : '';
1726577bfc3SGreg Roach
1736577bfc3SGreg Roach            $format  = static::INTERVENTION_FORMATS[$image->mime()] ?? 'jpg';
1746577bfc3SGreg Roach            $quality = $this->extractImageQuality($image, static::GD_DEFAULT_IMAGE_QUALITY);
1756577bfc3SGreg Roach            $data    = (string) $image->encode($format, $quality);
1766577bfc3SGreg Roach
1776577bfc3SGreg Roach            return $this->imageResponse($data, $image->mime(), $download_filename);
1786577bfc3SGreg Roach        } catch (NotReadableException $ex) {
1796577bfc3SGreg Roach            return $this->replacementImageResponse(pathinfo($filename, PATHINFO_EXTENSION))
1806577bfc3SGreg Roach                ->withHeader('X-Image-Exception', $ex->getMessage());
181f32d77e6SGreg Roach        } catch (UnableToReadFile $ex) {
1826577bfc3SGreg Roach            return $this->replacementImageResponse((string) StatusCodeInterface::STATUS_NOT_FOUND);
1836577bfc3SGreg Roach        } catch (Throwable $ex) {
1846577bfc3SGreg Roach            return $this->replacementImageResponse((string) StatusCodeInterface::STATUS_INTERNAL_SERVER_ERROR)
1856577bfc3SGreg Roach                ->withHeader('X-Image-Exception', $ex->getMessage());
1866577bfc3SGreg Roach        }
1876577bfc3SGreg Roach    }
1886577bfc3SGreg Roach
1896577bfc3SGreg Roach    /**
1906577bfc3SGreg Roach     * Create a smaller version of an image.
1916577bfc3SGreg Roach     *
1926577bfc3SGreg Roach     * @param MediaFile $media_file
1936577bfc3SGreg Roach     * @param int       $width
1946577bfc3SGreg Roach     * @param int       $height
1956577bfc3SGreg Roach     * @param string    $fit
1966577bfc3SGreg Roach     * @param bool      $add_watermark
1976577bfc3SGreg Roach     *
1986577bfc3SGreg Roach     * @return ResponseInterface
1996577bfc3SGreg Roach     */
2006577bfc3SGreg Roach    public function mediaFileThumbnailResponse(
2016577bfc3SGreg Roach        MediaFile $media_file,
2026577bfc3SGreg Roach        int $width,
2036577bfc3SGreg Roach        int $height,
2046577bfc3SGreg Roach        string $fit,
2056577bfc3SGreg Roach        bool $add_watermark
2066577bfc3SGreg Roach    ): ResponseInterface {
2076577bfc3SGreg Roach        // Where are the images stored.
2086b9cb339SGreg Roach        $filesystem = Registry::filesystem()->media($media_file->media()->tree());
2096577bfc3SGreg Roach
2106577bfc3SGreg Roach        // Where is the image stored in the filesystem.
2116577bfc3SGreg Roach        $path = $media_file->filename();
2126577bfc3SGreg Roach
2136577bfc3SGreg Roach        try {
214f7cf8a15SGreg Roach            $mime_type = $filesystem->mimeType($path);
2156577bfc3SGreg Roach
2166577bfc3SGreg Roach            $key = implode(':', [
2176577bfc3SGreg Roach                $media_file->media()->tree()->name(),
2186577bfc3SGreg Roach                $path,
219f7cf8a15SGreg Roach                $filesystem->lastModified($path),
2206577bfc3SGreg Roach                (string) $width,
2216577bfc3SGreg Roach                (string) $height,
2226577bfc3SGreg Roach                $fit,
2236577bfc3SGreg Roach                (string) $add_watermark,
2246577bfc3SGreg Roach            ]);
2256577bfc3SGreg Roach
2266577bfc3SGreg Roach            $closure = function () use ($filesystem, $path, $width, $height, $fit, $add_watermark, $media_file): string {
2276577bfc3SGreg Roach                $image = $this->imageManager()->make($filesystem->readStream($path));
2286577bfc3SGreg Roach                $image = $this->autorotateImage($image);
2296577bfc3SGreg Roach                $image = $this->resizeImage($image, $width, $height, $fit);
2306577bfc3SGreg Roach
2316577bfc3SGreg Roach                if ($add_watermark) {
2326577bfc3SGreg Roach                    $watermark = $this->createWatermark($image->width(), $image->height(), $media_file);
2336577bfc3SGreg Roach                    $image     = $this->addWatermark($image, $watermark);
2346577bfc3SGreg Roach                }
2356577bfc3SGreg Roach
2366577bfc3SGreg Roach                $format  = static::INTERVENTION_FORMATS[$image->mime()] ?? 'jpg';
2376577bfc3SGreg Roach                $quality = $this->extractImageQuality($image, static::GD_DEFAULT_THUMBNAIL_QUALITY);
2386577bfc3SGreg Roach
2396577bfc3SGreg Roach                return (string) $image->encode($format, $quality);
2406577bfc3SGreg Roach            };
2416577bfc3SGreg Roach
2426577bfc3SGreg Roach            // Images and Responses both contain resources - which cannot be serialized.
2436577bfc3SGreg Roach            // So cache the raw image data.
2446b9cb339SGreg Roach            $data = Registry::cache()->file()->remember($key, $closure, static::THUMBNAIL_CACHE_TTL);
2456577bfc3SGreg Roach
2466577bfc3SGreg Roach            return $this->imageResponse($data, $mime_type, '');
2476577bfc3SGreg Roach        } catch (NotReadableException $ex) {
2486577bfc3SGreg Roach            return $this->replacementImageResponse('.' . pathinfo($path, PATHINFO_EXTENSION));
249f32d77e6SGreg Roach        } catch (UnableToReadFile $ex) {
2506577bfc3SGreg Roach            return $this->replacementImageResponse((string) StatusCodeInterface::STATUS_NOT_FOUND);
2516577bfc3SGreg Roach        } catch (Throwable $ex) {
2526577bfc3SGreg Roach            return $this->replacementImageResponse((string) StatusCodeInterface::STATUS_INTERNAL_SERVER_ERROR)
253*6d24947fSGreg Roach                ->withHeader('X-Thumbnail-Exception', get_class($ex) . ': ' . $ex->getMessage());
2546577bfc3SGreg Roach        }
2556577bfc3SGreg Roach    }
2566577bfc3SGreg Roach
2576577bfc3SGreg Roach    /**
2586577bfc3SGreg Roach     * Does a full-sized image need a watermark?
2596577bfc3SGreg Roach     *
2606577bfc3SGreg Roach     * @param MediaFile     $media_file
2616577bfc3SGreg Roach     * @param UserInterface $user
2626577bfc3SGreg Roach     *
2636577bfc3SGreg Roach     * @return bool
2646577bfc3SGreg Roach     */
2656577bfc3SGreg Roach    public function fileNeedsWatermark(MediaFile $media_file, UserInterface $user): bool
2666577bfc3SGreg Roach    {
2676577bfc3SGreg Roach        $tree = $media_file->media()->tree();
2686577bfc3SGreg Roach
2696577bfc3SGreg Roach        return Auth::accessLevel($tree, $user) > $tree->getPreference('SHOW_NO_WATERMARK');
2706577bfc3SGreg Roach    }
2716577bfc3SGreg Roach
2726577bfc3SGreg Roach    /**
2736577bfc3SGreg Roach     * Does a thumbnail image need a watermark?
2746577bfc3SGreg Roach     *
2756577bfc3SGreg Roach     * @param MediaFile     $media_file
2766577bfc3SGreg Roach     * @param UserInterface $user
2776577bfc3SGreg Roach     *
2786577bfc3SGreg Roach     * @return bool
2796577bfc3SGreg Roach     */
2806577bfc3SGreg Roach    public function thumbnailNeedsWatermark(MediaFile $media_file, UserInterface $user): bool
2816577bfc3SGreg Roach    {
2826577bfc3SGreg Roach        return $this->fileNeedsWatermark($media_file, $user);
2836577bfc3SGreg Roach    }
2846577bfc3SGreg Roach
2856577bfc3SGreg Roach    /**
2866577bfc3SGreg Roach     * Create a watermark image, perhaps specific to a media-file.
2876577bfc3SGreg Roach     *
2886577bfc3SGreg Roach     * @param int       $width
2896577bfc3SGreg Roach     * @param int       $height
2906577bfc3SGreg Roach     * @param MediaFile $media_file
2916577bfc3SGreg Roach     *
2926577bfc3SGreg Roach     * @return Image
2936577bfc3SGreg Roach     */
2946577bfc3SGreg Roach    public function createWatermark(int $width, int $height, MediaFile $media_file): Image
2956577bfc3SGreg Roach    {
2966577bfc3SGreg Roach        return $this->imageManager()
2976577bfc3SGreg Roach            ->make(Webtrees::ROOT_DIR . static::WATERMARK_FILE)
2986577bfc3SGreg Roach            ->resize($width, $height, static function (Constraint $constraint) {
2996577bfc3SGreg Roach                $constraint->aspectRatio();
3006577bfc3SGreg Roach            });
3016577bfc3SGreg Roach    }
3026577bfc3SGreg Roach
3036577bfc3SGreg Roach    /**
3046577bfc3SGreg Roach     * Add a watermark to an image.
3056577bfc3SGreg Roach     *
3066577bfc3SGreg Roach     * @param Image $image
3076577bfc3SGreg Roach     * @param Image $watermark
3086577bfc3SGreg Roach     *
3096577bfc3SGreg Roach     * @return Image
3106577bfc3SGreg Roach     */
3116577bfc3SGreg Roach    public function addWatermark(Image $image, Image $watermark): Image
3126577bfc3SGreg Roach    {
3136577bfc3SGreg Roach        return $image->insert($watermark, 'center');
3146577bfc3SGreg Roach    }
3156577bfc3SGreg Roach
3166577bfc3SGreg Roach    /**
3176577bfc3SGreg Roach     * Send a replacement image, to replace one that could not be found or created.
3186577bfc3SGreg Roach     *
3196577bfc3SGreg Roach     * @param string $text HTTP status code or file extension
3206577bfc3SGreg Roach     *
3216577bfc3SGreg Roach     * @return ResponseInterface
3226577bfc3SGreg Roach     */
3236577bfc3SGreg Roach    public function replacementImageResponse(string $text): ResponseInterface
3246577bfc3SGreg Roach    {
3256577bfc3SGreg Roach        // We can't create a PNG/BMP/JPEG image, as the GD/IMAGICK libraries may be missing.
3266577bfc3SGreg Roach        $svg = view('errors/image-svg', ['status' => $text]);
3276577bfc3SGreg Roach
3286577bfc3SGreg Roach        // We can't send the actual status code, as browsers won't show images with 4xx/5xx.
3296577bfc3SGreg Roach        return response($svg, StatusCodeInterface::STATUS_OK, [
3306577bfc3SGreg Roach            'Content-Type' => 'image/svg+xml',
3316577bfc3SGreg Roach        ]);
3326577bfc3SGreg Roach    }
3336577bfc3SGreg Roach
3346577bfc3SGreg Roach    /**
3356577bfc3SGreg Roach     * @param string $data
3366577bfc3SGreg Roach     * @param string $mime_type
3376577bfc3SGreg Roach     * @param string $filename
3386577bfc3SGreg Roach     *
3396577bfc3SGreg Roach     * @return ResponseInterface
3406577bfc3SGreg Roach     */
3416577bfc3SGreg Roach    protected function imageResponse(string $data, string $mime_type, string $filename): ResponseInterface
3426577bfc3SGreg Roach    {
3436577bfc3SGreg Roach        $headers = [
3446577bfc3SGreg Roach            'Content-Type'   => $mime_type,
3456577bfc3SGreg Roach            'Content-Length' => (string) strlen($data),
3466577bfc3SGreg Roach        ];
3476577bfc3SGreg Roach
3486577bfc3SGreg Roach        if ($filename !== '') {
3496577bfc3SGreg Roach            $headers['Content-Disposition'] = 'attachment; filename="' . addcslashes(basename($filename), '"');
3506577bfc3SGreg Roach        }
3516577bfc3SGreg Roach
3526577bfc3SGreg Roach        return response($data, StatusCodeInterface::STATUS_OK, $headers);
3536577bfc3SGreg Roach    }
3546577bfc3SGreg Roach
3556577bfc3SGreg Roach    /**
3566577bfc3SGreg Roach     * @return ImageManager
3576577bfc3SGreg Roach     * @throws RuntimeException
3586577bfc3SGreg Roach     */
3596577bfc3SGreg Roach    protected function imageManager(): ImageManager
3606577bfc3SGreg Roach    {
3616577bfc3SGreg Roach        foreach (static::INTERVENTION_DRIVERS as $driver) {
3626577bfc3SGreg Roach            if (extension_loaded($driver)) {
3636577bfc3SGreg Roach                return new ImageManager(['driver' => $driver]);
3646577bfc3SGreg Roach            }
3656577bfc3SGreg Roach        }
3666577bfc3SGreg Roach
3676577bfc3SGreg Roach        throw new RuntimeException('No PHP graphics library is installed.  Need Imagick or GD');
3686577bfc3SGreg Roach    }
3696577bfc3SGreg Roach
3706577bfc3SGreg Roach    /**
3716577bfc3SGreg Roach     * Apply EXIF rotation to an image.
3726577bfc3SGreg Roach     *
3736577bfc3SGreg Roach     * @param Image $image
3746577bfc3SGreg Roach     *
3756577bfc3SGreg Roach     * @return Image
3766577bfc3SGreg Roach     */
3776577bfc3SGreg Roach    protected function autorotateImage(Image $image): Image
3786577bfc3SGreg Roach    {
3796577bfc3SGreg Roach        try {
3806577bfc3SGreg Roach            // Auto-rotate using EXIF information.
3816577bfc3SGreg Roach            return $image->orientate();
3826577bfc3SGreg Roach        } catch (NotSupportedException $ex) {
3836577bfc3SGreg Roach            // If we can't auto-rotate the image, then don't.
3846577bfc3SGreg Roach            return $image;
3856577bfc3SGreg Roach        }
3866577bfc3SGreg Roach    }
3876577bfc3SGreg Roach
3886577bfc3SGreg Roach    /**
3896577bfc3SGreg Roach     * Resize an image.
3906577bfc3SGreg Roach     *
3916577bfc3SGreg Roach     * @param Image  $image
3926577bfc3SGreg Roach     * @param int    $width
3936577bfc3SGreg Roach     * @param int    $height
3946577bfc3SGreg Roach     * @param string $fit
3956577bfc3SGreg Roach     *
3966577bfc3SGreg Roach     * @return Image
3976577bfc3SGreg Roach     */
3986577bfc3SGreg Roach    protected function resizeImage(Image $image, int $width, int $height, string $fit): Image
3996577bfc3SGreg Roach    {
4006577bfc3SGreg Roach        switch ($fit) {
4016577bfc3SGreg Roach            case 'crop':
4026577bfc3SGreg Roach                return $image->fit($width, $height);
4036577bfc3SGreg Roach            case 'contain':
4046577bfc3SGreg Roach                return $image->resize($width, $height, static function (Constraint $constraint) {
4056577bfc3SGreg Roach                    $constraint->aspectRatio();
4066577bfc3SGreg Roach                    $constraint->upsize();
4076577bfc3SGreg Roach                });
4086577bfc3SGreg Roach        }
4096577bfc3SGreg Roach
4106577bfc3SGreg Roach        return $image;
4116577bfc3SGreg Roach    }
4126577bfc3SGreg Roach
4136577bfc3SGreg Roach    /**
4146577bfc3SGreg Roach     * Extract the quality/compression parameter from an image.
4156577bfc3SGreg Roach     *
4166577bfc3SGreg Roach     * @param Image $image
4176577bfc3SGreg Roach     * @param int   $default
4186577bfc3SGreg Roach     *
4196577bfc3SGreg Roach     * @return int
4206577bfc3SGreg Roach     */
4216577bfc3SGreg Roach    protected function extractImageQuality(Image $image, int $default): int
4226577bfc3SGreg Roach    {
4236577bfc3SGreg Roach        $core = $image->getCore();
4246577bfc3SGreg Roach
4256577bfc3SGreg Roach        if ($core instanceof Imagick) {
426bf26501bSGreg Roach            return $core->getImageCompressionQuality() ?: $default;
4276577bfc3SGreg Roach        }
4286577bfc3SGreg Roach
4296577bfc3SGreg Roach        return $default;
4306577bfc3SGreg Roach    }
4316577bfc3SGreg Roach}
432