Skip to content
Snippets Groups Projects
Select Git revision
  • 00b4e32c6dcff8ca9b038c59b81e93b11737ac25
  • main default protected
  • studip-rector
  • ci-opt
  • course-members-export-as-word
  • data-vue-app
  • pipeline-improvements
  • webpack-optimizations
  • rector
  • icon-renewal
  • http-client-and-factories
  • jsonapi-atomic-operations
  • vueify-messages
  • tic-2341
  • 135-translatable-study-areas
  • extensible-sorm-action-parameters
  • sorm-configuration-trait
  • jsonapi-mvv-routes
  • docblocks-for-magic-methods
19 results

Institute.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));
        }
    }