Skip to content
Snippets Groups Projects
Select Git revision
  • abf34ac094736a16676ab4b0663b66f8dd5de615
  • 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

StockImage.php

Blame
  • Forked from Stud.IP / Stud.IP
    1234 commits behind the upstream repository.
    Sebastian Biller's avatar
    Sebastian Biller authored and Jan-Hendrik Willms committed
    Closes #4356
    
    Merge request studip/studip!3157
    abf34ac0
    History
    Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    StockImage.php 3.20 KiB
    <?php
    
    /**
     *
     * @property int $id database column
     * @property string $title database column
     * @property string $description database column
     * @property string $license database column
     * @property string $author database column
     * @property string $mime_type database column
     * @property int $size database column
     * @property int $width database column
     * @property int $height database column
     * @property string $palette database column
     * @property string $tags database column
     * @property int $mkdate database column
     * @property int $chdate database column
     */
    class StockImage extends \SimpleORMap
    {
        public const SIZE_ORIGINAL = 'original';
        public const SIZE_LARGE = 'large';
        public const SIZE_MEDIUM = 'medium';
        public const SIZE_SMALL = 'small';
    
        public static function sizes()
        {
            return [
                self::SIZE_ORIGINAL => -1,
                self::SIZE_LARGE => 2400,
                self::SIZE_MEDIUM => 1920,
                self::SIZE_SMALL => 640,
            ];
        }
    
        protected static function configure($config = [])
        {
            $config['db_table'] = 'stock_images';
    
            $config['registered_callbacks']['after_delete'][] = function ($resource) {
                if ($resource->hasFile()) {
                    foreach (array_keys(self::sizes()) as $sizeName) {
                        $path = $resource->getPath($sizeName);
                        if (file_exists($path)) {
                            unlink($path);
                        }
                    }
                }
            };
    
            parent::configure($config);
        }
    
        /**
         * @SuppressWarnings(PHPMD.Superglobals)
         */
        public function getPath(string $size = self::SIZE_ORIGINAL): string
        {
            return sprintf(
                '%s/stock-images/%s',
                $GLOBALS['DYNAMIC_CONTENT_PATH'],
                $this->getFilename($size)
            );
        }
    
        public function getFilename(string $size = self::SIZE_ORIGINAL): string
        {
            return sprintf(
                '%d-%s.%s',
                $this->id,
                $size,
                substr($this->mime_type, 6)
            );
        }
    
        /**
         * return string|null  either a string containing the public URL to the file
         *                     or null if there is still no such file
         *
         * @SuppressWarnings(PHPMD.Superglobals)
         */
        public function getDownloadURL(string $size = self::SIZE_ORIGINAL)
        {
            if (!$this->hasFile()) {
                return null;
            }
            $sizes = self::sizes();
            if (!(isset($sizes[$size]) && $sizes[$size] <= $this->width)) {
                return null;
            }
    
            return sprintf(
                '%s/stock-images/%s',
                $GLOBALS['DYNAMIC_CONTENT_URL'],
                $this->getFilename($size)
            );
        }
    
        /**
         * @return iterable<string,string> an associative array of sizes to URLs
         */
        public function getDownloadURLs(): iterable
        {
            return array_filter(
                array_reduce(
                    array_keys(self::sizes()),
                    function ($urls, $size) {
                        return array_merge($urls, [$size => $this->getDownloadURL($size)]);
                    },
                    []
                )
            );
        }
    
        public function hasFile(): bool
        {
            return !empty($this->mime_type);
        }
    }