Skip to content
Snippets Groups Projects
Select Git revision
  • bf84e121ecfe12dff9c2cbbc98410d245fe639ce
  • main default protected
  • step-3263
  • feature/plugins-cli
  • feature/vite
  • step-2484-peerreview
  • biest/issue-5051
  • tests/simplify-jsonapi-tests
  • fix/typo-in-1a70031
  • feature/broadcasting
  • database-seeders-and-factories
  • feature/peer-review-2
  • feature-feedback-jsonapi
  • feature/peerreview
  • feature/balloon-plus
  • feature/stock-images-unsplash
  • tic-2588
  • 5.0
  • 5.2
  • biest/unlock-blocks
  • biest-1514
21 results

Resource.class.php

Blame
  • Forked from Stud.IP / Stud.IP
    Source project has a limited visibility.
    Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    Scaler.php 2.00 KiB
    <?php
    
    namespace Studip\StockImages;
    
    final class Scaler
    {
        /**
         * @param \StockImage $stockImage
         */
        public function __invoke(\StockImage $stockImage): void
        {
            foreach (\StockImage::sizes() as $name => $width) {
                if ($name !== \StockImage::SIZE_ORIGINAL) {
                    $this->scaleToWidth($stockImage, $name, $width);
                }
            }
        }
    
        private function scaleToWidth(\StockImage $stockImage, string $sizeName, int $targetWidth): bool
        {
            $image = $this->createImage($stockImage);
            $width = imagesx($image);
    
            if ($width < $targetWidth) {
                $scaledImage = $image;
            } else {
                $scaledImage = imagescale($image, $targetWidth);
                imagedestroy($image);
            }
    
            return $this->storeImage($stockImage, $scaledImage, $sizeName);
        }
    
        /**
         * @return resource the \GDImage created from the original image file
         */
        private function createImage(\StockImage $stockImage)
        {
            $type = $stockImage->mime_type;
            $lookup = [
                'image/gif' => 'imagecreatefromgif',
                'image/jpeg' => 'imagecreatefromjpeg',
                'image/png' => 'imagecreatefrompng',
                'image/webp' => 'imagecreatefromwebp',
            ];
            if (!isset($lookup[$type])) {
                throw new \RuntimeException(_('Unsupported image type.'));
            }
    
            return $lookup[$type]($stockImage->getPath());
        }
    
        /**
         * @param resource $image the scaled image
         */
        private function storeImage(\StockImage $stockImage, $image, string $sizeName): bool
        {
            $type = $stockImage->mime_type;
            $lookup = [
                'image/gif' => 'imagegif',
                'image/jpeg' => 'imagejpeg',
                'image/png' => 'imagepng',
                'image/webp' => 'imagewebp',
            ];
            if (!isset($lookup[$type])) {
                throw new \RuntimeException(_('Unsupported image type.'));
            }
    
            return $lookup[$type]($image, $stockImage->getPath($sizeName));
        }
    }