aboutsummaryrefslogtreecommitdiff
path: root/lib/models/StockImage.php
blob: fa423ed163a974fac1b7ea8b4d86e082f0aa8824 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php

/**
 * @property string $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);
    }
}