Passed
Push — master ( ab911e...b4725e )
by Nicolaas
12:52 queued 09:56
created

PerfectCMSImages::get_mobile_height()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 8
rs 10
1
<?php
2
3
namespace Sunnysideup\PerfectCmsImages\Api;
4
5
use Psr\Log\LoggerInterface;
6
use SilverStripe\Assets\Filesystem;
7
use SilverStripe\Assets\Image;
8
use SilverStripe\Core\Config\Config;
9
use SilverStripe\Core\Flushable;
10
use SilverStripe\Core\Injector\Injector;
11
use SilverStripe\ORM\DB;
12
use Sunnysideup\PerfectCmsImages\Forms\PerfectCmsImagesUploadField;
13
use Sunnysideup\PerfectCmsImages\Model\File\PerfectCmsImageDataExtension;
14
15
class PerfectCMSImages implements Flushable
16
{
17
    /**
18
     *.htaccess content for assets ...
19
     *
20
     * @var string
21
     */
22
    private static $htaccess_content = <<<'EOT'
23
<IfModule mod_rewrite.c>
24
    RewriteEngine On
25
    RewriteBase /
26
27
    RewriteCond %{REQUEST_FILENAME} !-f
28
    RewriteCond %{REQUEST_FILENAME} !-d
29
    RewriteRule ^(.+)\.(v[A-Za-z0-9]+)\.(js|css|png|jpg|gif|svg|webp)$ $1.$3 [L]
30
</IfModule>
31
32
EOT;
33
34
    private static $unused_images_folder_name = 'unusedimages';
35
36
    /**
37
     * used to set the max width of the media value for mobile images,
38
     * eg <source srcset="small.jpg, small2x.jpg 2x" media="(max-width: 600px)">.
39
     *
40
     * @var string
41
     */
42
    private static $mobile_media_max_width = '600px';
43
44
    /*
45
     * Images Titles will be appended to the links only
46
     * if the ClassName of the Image is in this array
47
     * @var array
48
     *
49
     */
50
    private static $perfect_cms_images_append_title_to_image_links_classes = [
51
        Image::class,
52
    ];
53
54
    private static $retina_multiplier = 2;
55
56
    /**
57
     * force resample, put htaccess content if it does not exists.
58
     */
59
    public static function flush()
60
    {
61
        $tables = DB::table_list();
62
        if (array_key_exists(strtolower('PerfectCMSImageCache'), $tables)) {
63
            DB::query('DELETE FROM PerfectCMSImageCache;');
64
        }
65
        if (!Config::inst()->get(Image::class, 'force_resample')) {
66
            Config::modify()->merge(Image::class, 'force_resample', true);
67
        }
68
69
        if (class_exists('HashPathExtension')) {
70
            if (!file_exists(ASSETS_PATH)) {
71
                Filesystem::makeFolder(ASSETS_PATH);
72
            }
73
74
            $fileName = ASSETS_PATH . '/.htaccess';
75
            if (!file_exists($fileName)) {
76
                $string = Config::inst()->get(PerfectCMSImages::class, 'htaccess_content');
77
                file_put_contents($fileName, $string);
78
            }
79
        }
80
    }
81
82
    public static function get_description_for_cms(string $name): string
83
    {
84
        $widthRecommendation = (int) PerfectCMSImages::get_width($name);
85
        $heightRecommendation = (int) PerfectCMSImages::get_height($name);
86
        $useRetina = PerfectCMSImages::use_retina($name);
87
        $recommendedFileType = PerfectCMSImages::get_file_type($name);
88
        $multiplier = PerfectCMSImages::get_multiplier($useRetina);
89
        if ('' === $recommendedFileType) {
90
            $recommendedFileType = 'jpg';
91
        }
92
93
        if (0 !== (int) $widthRecommendation) {
94
            //cater for retina
95
            $widthRecommendation *= $multiplier;
96
            $actualWidthDescription = $widthRecommendation . 'px';
97
        } else {
98
            $actualWidthDescription = $widthRecommendation;
0 ignored issues
show
Unused Code introduced by
The assignment to $actualWidthDescription is dead and can be removed.
Loading history...
99
            $actualWidthDescription = 'flexible';
100
        }
101
102
        if (0 !== (int) $heightRecommendation) {
103
            //cater for retina
104
            $heightRecommendation *= $multiplier;
105
            $actualHeightDescription = $heightRecommendation . 'px';
106
        } else {
107
            $actualHeightDescription = $heightRecommendation;
0 ignored issues
show
Unused Code introduced by
The assignment to $actualHeightDescription is dead and can be removed.
Loading history...
108
            $actualHeightDescription = 'flexible';
109
        }
110
111
        $rightTitle = '<span>';
112
113
        if ('flexible' === $actualWidthDescription) {
114
            $rightTitle .= 'Image width is flexible';
115
        } else {
116
            $rightTitle .= "Image should to be <strong>{$actualWidthDescription}</strong> wide";
117
        }
118
119
        $rightTitle .= ' and ';
120
121
        if ('flexible' === $actualHeightDescription) {
122
            $rightTitle .= 'height is flexible';
123
        } else {
124
            $rightTitle .= " <strong>{$actualHeightDescription}</strong> tall";
125
        }
126
127
        $rightTitle .= '<br />';
128
        $maxSizeInKilobytes = PerfectCMSImages::max_size_in_kilobytes($name);
129
        if (0 !== $maxSizeInKilobytes) {
130
            $rightTitle .= 'Maximum file size: ' . round($maxSizeInKilobytes / 1024, 2) . ' megabyte.';
131
            $rightTitle .= '<br />';
132
        }
133
134
        if ('' !== $recommendedFileType) {
135
            if (strlen($recommendedFileType) < 5) {
136
                $rightTitle .= 'The recommend file type (file extension) is <strong>' . $recommendedFileType . '</strong>.';
137
            } else {
138
                $rightTitle .= '<strong>' . $recommendedFileType . '</strong>';
139
            }
140
        }
141
142
        return $rightTitle . '</span>';
143
    }
144
145
    public static function use_retina(string $name): bool
146
    {
147
        return self::get_one_value_for_image($name, 'use_retina', true);
148
    }
149
150
    public static function get_multiplier(bool $useRetina): int
151
    {
152
        $multiplier = 1;
153
        if ($useRetina) {
154
            $multiplier = Config::inst()->get(PerfectCMSImages::class, 'retina_multiplier');
155
        }
156
157
        if (!$multiplier) {
158
            $multiplier = 1;
159
        }
160
161
        return $multiplier;
162
    }
163
164
    public static function is_crop(string $name): bool
165
    {
166
        return self::get_one_value_for_image($name, 'crop', false);
167
    }
168
169
    /**
170
     * @return int|string
171
     */
172
    public static function get_width(string $name, bool $forceInteger = false)
173
    {
174
        $v = self::get_one_value_for_image($name, 'width', 0);
175
        if ($forceInteger) {
176
            $v = (int) $v - 0;
177
        }
178
179
        return $v;
180
    }
181
182
    /**
183
     * @return int|string
184
     */
185
    public static function get_height(string $name, bool $forceInteger = false)
186
    {
187
        $v = self::get_one_value_for_image($name, 'height', 0);
188
        if ($forceInteger) {
189
            $v = (int) $v - 0;
190
        }
191
192
        return $v;
193
    }
194
195
196
    public static function has_mobile($name): bool
197
    {
198
        return (bool) (self::get_mobile_width($name, true) || self::get_mobile_height($name, true));
199
    }
200
201
    /**
202
     * @return int?string
0 ignored issues
show
Documentation Bug introduced by
The doc comment int?string at position 0 could not be parsed: Unknown type name 'int?string' at position 0 in int?string.
Loading history...
203
     */
204
    public static function get_mobile_width(string $name, bool $forceInteger = false)
205
    {
206
        $v = self::get_one_value_for_image($name, 'mobile_width', 0);
207
        if ($forceInteger) {
208
            $v = (int) $v - 0;
209
        }
210
211
        return $v;
212
    }
213
214
    /**
215
     * @return int|string
216
     */
217
    public static function get_mobile_height(string $name, bool $forceInteger = false)
218
    {
219
        $v = self::get_one_value_for_image($name, 'mobile_height', 0);
220
        if ($forceInteger) {
221
            $v = (int) $v - 0;
222
        }
223
224
        return $v;
225
    }
226
227
    public static function get_folder(string $name): string
228
    {
229
        return self::get_one_value_for_image($name, 'folder', 'other-images');
230
    }
231
232
    public static function move_to_right_folder(string $name): bool
233
    {
234
        return self::get_one_value_for_image($name, 'move_to_right_folder', true);
235
    }
236
237
    public static function loading_style(string $name): string
238
    {
239
        return self::get_one_value_for_image($name, 'loading_style', 'lazy');
240
    }
241
242
    public static function max_size_in_kilobytes(string $name): int
243
    {
244
        $maxSizeInKilobytes = self::get_one_value_for_image($name, 'max_size_in_kilobytes', 0);
245
        if (!$maxSizeInKilobytes) {
246
            $maxSizeInKilobytes = Config::inst()->get(PerfectCmsImagesUploadField::class, 'max_size_in_kilobytes');
247
        }
248
249
        return (int) $maxSizeInKilobytes - 0;
250
    }
251
252
    public static function get_file_type(string $name): string
253
    {
254
        return self::get_one_value_for_image($name, 'filetype', 'jpg');
255
    }
256
257
    public static function get_enforce_size(string $name): bool
258
    {
259
        return self::get_one_value_for_image($name, 'enforce_size', false);
260
    }
261
262
    /**
263
     * @return null|string
264
     */
265
    public static function get_mobile_media_width(string $name)
266
    {
267
        return self::get_one_value_for_image(
268
            $name,
269
            'mobile_media_max_width',
270
            Config::inst()->get(PerfectCMSImages::class, 'mobile_media_max_width')
271
        );
272
    }
273
274
    public static function get_padding_bg_colour(string $name): string
275
    {
276
        return self::get_one_value_for_image(
277
            $name,
278
            'padding_bg_colour',
279
            Config::inst()->get(PerfectCmsImageDataExtension::class, 'perfect_cms_images_background_padding_color')
280
        );
281
    }
282
283
    public static function image_info_available(string $name): bool
284
    {
285
        $sizes = self::get_all_values_for_images();
286
        //print_r($sizes);die();
287
        return isset($sizes[$name]);
288
    }
289
290
    public static function get_all_values_for_images(): array
291
    {
292
        return Config::inst()->get(
293
            PerfectCmsImageDataExtension::class,
294
            'perfect_cms_images_image_definitions'
295
        ) ?: [];
296
    }
297
298
    /**
299
     * @param string $name
300
     * @param string $key
301
     * @param mixed $default - optional
302
     *
303
     * @return mixed
304
     */
305
    protected static function get_one_value_for_image(string $name, string $key, $default = '')
306
    {
307
        $sizes = self::get_all_values_for_images();
308
309
        return $sizes[$name][$key] ?? $default;
310
        // Injector::inst()->get(LoggerInterface::class)->info('no information for image with the name: ' . $name . '.' . $key);
311
    }
312
}
313