Completed
Push — 2.0.0 ( 632760...4947fe )
by John
04:53
created

Image::renderHTMLLink()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 12
rs 8.8571
cc 6
eloc 7
nc 8
nop 1
1
<?php
2
3
namespace Alpha\View\Widget;
4
5
use Alpha\Util\Logging\Logger;
6
use Alpha\Util\Config\ConfigProvider;
7
use Alpha\Model\Type\Integer;
8
use Alpha\Model\Type\Enum;
9
use Alpha\Model\Type\Boolean;
10
use Alpha\Model\Type\Double;
11
use Alpha\Exception\IllegalArguementException;
12
use Alpha\Controller\Controller;
13
use Alpha\Controller\Front\FrontController;
14
15
/**
16
 * A widget that can generate an image which is scaled to the screen resolution of the
17
 * user, and can be made secured to prevent hot-linking from remote sites.  Note that by
18
 * default, a jpg file will be returned (the source file can be jpg, png, or gif).
19
 *
20
 * @since 1.0
21
 *
22
 * @author John Collins <[email protected]>
23
 * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
24
 * @copyright Copyright (c) 2015, John Collins (founder of Alpha Framework).
25
 * All rights reserved.
26
 *
27
 * <pre>
28
 * Redistribution and use in source and binary forms, with or
29
 * without modification, are permitted provided that the
30
 * following conditions are met:
31
 *
32
 * * Redistributions of source code must retain the above
33
 *   copyright notice, this list of conditions and the
34
 *   following disclaimer.
35
 * * Redistributions in binary form must reproduce the above
36
 *   copyright notice, this list of conditions and the
37
 *   following disclaimer in the documentation and/or other
38
 *   materials provided with the distribution.
39
 * * Neither the name of the Alpha Framework nor the names
40
 *   of its contributors may be used to endorse or promote
41
 *   products derived from this software without specific
42
 *   prior written permission.
43
 *
44
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
45
 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
46
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
47
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
48
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
49
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
50
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
51
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
52
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
54
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
55
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
56
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
57
 * </pre>
58
 */
59
class Image
60
{
61
    /**
62
     * The title of the image for alt text (optional).
63
     *
64
     * @var string
65
     *
66
     * @since 1.0
67
     */
68
    private $title;
0 ignored issues
show
Unused Code introduced by
The property $title is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
69
70
    /**
71
     * The absolute path to the source image.
72
     *
73
     * @var string
74
     *
75
     * @since 1.0
76
     */
77
    private $source;
78
79
    /**
80
     * The width of the image (can differ from the source file when scale=true).
81
     *
82
     * @var Alpha\Model\Type\Integer
83
     *
84
     * @since 1.0
85
     */
86
    private $width;
87
88
    /**
89
     * The height of the image (can differ from the source file when scale=true).
90
     *
91
     * @var Alpha\Model\Type\Integer
92
     *
93
     * @since 1.0
94
     */
95
    private $height;
96
97
    /**
98
     * The file type of the source image (gif, jpg, or png supported).
99
     *
100
     * @var Alpha\Model\Type\Enum
101
     *
102
     * @since 1.0
103
     */
104
    private $sourceType;
105
106
    /**
107
     * The quality of the jpg image generated (0.00 to 1.00, 0.75 by default).
108
     *
109
     * @var Alpha\Model\Type\Double
110
     *
111
     * @since 1.0
112
     */
113
    private $quality;
114
115
    /**
116
     * Flag to determine if the image will scale to match the target resolution (false
117
     * by default).
118
     *
119
     * @var Alpha\Model\Type\Boolean
120
     *
121
     * @since 1.0
122
     */
123
    private $scale;
124
125
    /**
126
     * Flag to determine if the link to the image will change every 24hrs, making hot-linking
127
     * to the image difficult (false by default).
128
     *
129
     * @var Alpha\Model\Type\Boolean
130
     *
131
     * @since 1.0
132
     */
133
    private $secure;
134
135
    /**
136
     * The auto-generated name of the cache file for the image.
137
     *
138
     * @var string
139
     *
140
     * @since 1.0
141
     */
142
    private $filename;
143
144
    /**
145
     * Trace logger.
146
     *
147
     * @var Alpha\Util\Logging\Logger
148
     *
149
     * @since 1.0
150
     */
151
    private static $logger = null;
152
153
    /**
154
     * The constructor.
155
     *
156
     * @param $source
157
     * @param $width
158
     * @param $height
159
     * @param $sourceType
160
     * @param $quality
161
     * @param $scale
162
     *
163
     * @throws Alpha\Exception\IllegalArguementException
164
     *
165
     * @since 1.0
166
     */
167
    public function __construct($source, $width, $height, $sourceType, $quality = 0.75, $scale = false, $secure = false)
168
    {
169
        self::$logger = new Logger('Image');
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Alpha\Util\Logging\Logger('Image') of type object<Alpha\Util\Logging\Logger> is incompatible with the declared type object<Alpha\View\Widget...ha\Util\Logging\Logger> of property $logger.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
170
        self::$logger->debug('>>__construct(source=['.$source.'], width=['.$width.'], height=['.$height.'], sourceType=['.$sourceType.'], quality=['.$quality.'], scale=['.$scale.'], secure=['.$secure.'])');
171
172
        $config = ConfigProvider::getInstance();
0 ignored issues
show
Unused Code introduced by
$config is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
173
174
        if (file_exists($source)) {
175
            $this->source = $source;
176
        } else {
177
            throw new IllegalArguementException('The source file for the Image widget ['.$source.'] cannot be found!');
178
        }
179
180
        $this->sourceType = new Enum();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Alpha\Model\Type\Enum() of type object<Alpha\Model\Type\Enum> is incompatible with the declared type object<Alpha\View\Widget\Alpha\Model\Type\Enum> of property $sourceType.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
181
        $this->sourceType->setOptions(array('jpg', 'png', 'gif'));
182
        $this->sourceType->setValue($sourceType);
183
184
        if ($quality < 0.0 || $quality > 1.0) {
185
            throw new IllegalArguementException('The quality setting of ['.$quality.'] is outside of the allowable range of 0.0 to 1.0');
186
        }
187
188
        $this->quality = new Double($quality);
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Alpha\Model\Type\Double($quality) of type object<Alpha\Model\Type\Double> is incompatible with the declared type object<Alpha\View\Widget\Alpha\Model\Type\Double> of property $quality.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
189
        $this->scale = new Boolean($scale);
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Alpha\Model\Type\Boolean($scale) of type object<Alpha\Model\Type\Boolean> is incompatible with the declared type object<Alpha\View\Widget...pha\Model\Type\Boolean> of property $scale.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
190
        $this->secure = new Boolean($secure);
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Alpha\Model\Type\Boolean($secure) of type object<Alpha\Model\Type\Boolean> is incompatible with the declared type object<Alpha\View\Widget...pha\Model\Type\Boolean> of property $secure.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
191
192
        $this->width = new Integer($width);
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Alpha\Model\Type\Integer($width) of type object<Alpha\Model\Type\Integer> is incompatible with the declared type object<Alpha\View\Widget...pha\Model\Type\Integer> of property $width.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
193
        $this->height = new Integer($height);
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Alpha\Model\Type\Integer($height) of type object<Alpha\Model\Type\Integer> is incompatible with the declared type object<Alpha\View\Widget...pha\Model\Type\Integer> of property $height.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
194
195
        $this->setFilename();
196
197
        self::$logger->debug('<<__construct');
198
    }
199
200
    /**
201
     * Renders the HTML <img> tag to the ViewImage controller, with all of the correct params to render the source
202
     * image in the desired resolution.
203
     *
204
     * @param $altText Set this value to render alternate text as part of the HTML link (defaults to no alternate text)
205
     *
206
     * @return string
207
     *
208
     * @since 1.0
209
     */
210
    public function renderHTMLLink($altText = '')
211
    {
212
        $config = ConfigProvider::getInstance();
213
214
        if ($this->secure->getBooleanValue()) {
215
            $params = Controller::generateSecurityFields();
216
217
            return '<img src="'.FrontController::generateSecureURL('act=Alpha\Controller\ImageController&source='.$this->source.'&width='.$this->width->getValue().'&height='.$this->height->getValue().'&type='.$this->sourceType->getValue().'&quality='.$this->quality->getValue().'&scale='.$this->scale->getValue().'&secure='.$this->secure->getValue().'&var1='.$params[0].'&var2='.$params[1]).'"'.(empty($altText) ? '' : ' alt="'.$altText.'"').($config->get('cms.images.widget.bootstrap.responsive') ? ' class="img-responsive"' : '').'/>';
218
        } else {
219
            return '<img src="'.FrontController::generateSecureURL('act=Alpha\Controller\ImageController&source='.$this->source.'&width='.$this->width->getValue().'&height='.$this->height->getValue().'&type='.$this->sourceType->getValue().'&quality='.$this->quality->getValue().'&scale='.$this->scale->getValue().'&secure='.$this->secure->getValue()).'"'.(empty($altText) ? '' : ' alt="'.$altText.'"').($config->get('cms.images.widget.bootstrap.responsive') ? ' class="img-responsive"' : '').'/>';
220
        }
221
    }
222
223
    /**
224
     * Setter for the filename, which also creates a sub-directory under /cache for images when required.
225
     *
226
     * @since 1.0
227
     *
228
     * @throws Alpha\Exception\AlphaException
229
     */
230
    private function setFilename()
231
    {
232
        $config = ConfigProvider::getInstance();
233
234
        if (!strpos($this->source, 'attachments/article_')) {
235
            // checking to see if we will write a jpg or png to the cache
236 View Code Duplication
            if ($this->sourceType->getValue() == 'png' && $config->get('cms.images.perserve.png')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
237
                $this->filename = $config->get('app.file.store.dir').'cache/images/'.basename($this->source, '.'.$this->sourceType->getValue()).'_'.$this->width->getValue().'x'.$this->height->getValue().'.png';
238
            } else {
239
                $this->filename = $config->get('app.file.store.dir').'cache/images/'.basename($this->source, '.'.$this->sourceType->getValue()).'_'.$this->width->getValue().'x'.$this->height->getValue().'.jpg';
240
            }
241
        } else {
242
            // make a cache dir for the article
243
            $cacheDir = $config->get('app.file.store.dir').'cache/images/article_'.mb_substr($this->source, mb_strpos($this->source, 'attachments/article_') + 20, 11);
244
            if (!file_exists($cacheDir)) {
245
                $success = mkdir($cacheDir);
246
247
                if (!$success) {
248
                    throw new AlphaException('Unable to create the folder '.$cacheDir.' for the cache image, source file is '.$this->source);
249
                }
250
251
                // ...and set write permissions on the folder
252
                $success = chmod($cacheDir, 0777);
253
254
                if (!$success) {
255
                    throw new AlphaException('Unable to set write permissions on the folder ['.$cacheDir.'].');
256
                }
257
            }
258
259
            // now set the filename to include the new cache directory
260 View Code Duplication
            if ($this->sourceType->getValue() == 'png' && $config->get('cms.images.perserve.png')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
261
                $this->filename = $cacheDir.'/'.basename($this->source, '.'.$this->sourceType->getValue()).'_'.$this->width->getValue().'x'.$this->height->getValue().'.png';
262
            } else {
263
                $this->filename = $cacheDir.'/'.basename($this->source, '.'.$this->sourceType->getValue()).'_'.$this->width->getValue().'x'.$this->height->getValue().'.jpg';
264
            }
265
        }
266
267
        self::$logger->debug('Image filename is ['.$this->filename.']');
268
    }
269
270
    /**
271
     * Gets the auto-generated filename for the image in the /cache directory.
272
     *
273
     * @since 1.0
274
     */
275
    public function getFilename()
276
    {
277
        return $this->filename;
278
    }
279
280
    /**
281
     * Renders the actual binary image using GD library calls.
282
     *
283
     *  @since 1.0
284
     */
285
    public function renderImage()
0 ignored issues
show
Coding Style introduced by
renderImage uses the super-global variable $_COOKIE which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
286
    {
287
        $config = ConfigProvider::getInstance();
288
289
        // if scaled, we need to compute the target image size
290
        if ($this->scale->getBooleanValue() && isset($_COOKIE['screenSize'])) {
291
            $originalScreenResolution = explode('x', $config->get('sysCMSImagesWidgetScreenResolution'));
292
            $originalScreenX = $originalScreenResolution[0];
293
            $originalScreenY = $originalScreenResolution[1];
294
295
            $targetScreenResolution = explode('x', $_COOKIE['screenSize']);
296
            $targetScreenX = $targetScreenResolution[0];
297
            $targetScreenY = $targetScreenResolution[1];
298
299
            // calculate the new units we will scale by
300
            $xu = $targetScreenX / $originalScreenX;
301
            $yu = $targetScreenY / $originalScreenY;
302
303
            $this->width = new Integer(intval($this->width->getValue() * $xu));
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Alpha\Model\Type\In...dth->getValue() * $xu)) of type object<Alpha\Model\Type\Integer> is incompatible with the declared type object<Alpha\View\Widget...pha\Model\Type\Integer> of property $width.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
304
            $this->height = new Integer(intval($this->height->getValue() * $yu));
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Alpha\Model\Type\In...ght->getValue() * $yu)) of type object<Alpha\Model\Type\Integer> is incompatible with the declared type object<Alpha\View\Widget...pha\Model\Type\Integer> of property $height.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
305
306
            // need to update the cache filename as the dimensions have changed
307
            $this->setFilename();
308
        }
309
310
        // check the image cache first before we proceed
311
        if ($this->checkCache()) {
312
            $this->loadCache();
313
        } else {
314
            // now get the old image
315
            switch ($this->sourceType->getValue()) {
316
                case 'gif':
317
                    $old_image = imagecreatefromgif($this->source);
318
                break;
319
                case 'jpg':
320
                    $old_image = imagecreatefromjpeg($this->source);
321
                break;
322
                case 'png':
323
                    $old_image = imagecreatefrompng($this->source);
324
                break;
325
            }
326
327
            if (!$old_image) {
328
                $im = imagecreatetruecolor($this->width->getValue(), $this->height->getValue());
329
                $bgc = imagecolorallocate($im, 255, 255, 255);
330
                $tc = imagecolorallocate($im, 0, 0, 0);
331
                imagefilledrectangle($im, 0, 0, $this->width->getValue(), $this->height->getValue(), $bgc);
332
333
                imagestring($im, 1, 5, 5, "Error loading $this->source", $tc);
334 View Code Duplication
                if ($this->sourceType->getValue() == 'png' && $config->get('cms.images.perserve.png')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
335
                    imagepng($im);
336
                } else {
337
                    imagejpeg($im);
338
                }
339
                imagedestroy($im);
340
            } else {
341
                // the dimensions of the source image
342
                $oldWidth = imagesx($old_image);
0 ignored issues
show
Bug introduced by
The variable $old_image does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
343
                $oldHeight = imagesy($old_image);
344
345
                // now create the new image
346
                $new_image = imagecreatetruecolor($this->width->getValue(), $this->height->getValue());
347
348
                // set a transparent background for PNGs
349
                if ($this->sourceType->getValue() == 'png' && $config->get('cms.images.perserve.png')) {
350
                    // Turn off transparency blending (temporarily)
351
                    imagealphablending($new_image, false);
352
353
                    // Create a new transparent color for image
354
                    $color = imagecolorallocatealpha($new_image, 255, 0, 0, 0);
355
356
                    // Completely fill the background of the new image with allocated color.
357
                    imagefill($new_image, 0, 0, $color);
358
359
                    // Restore transparency blending
360
                    imagesavealpha($new_image, true);
361
                }
362
                // copy the old image to the new image (in memory, not the file!)
363
                imagecopyresampled($new_image, $old_image, 0, 0, 0, 0, $this->width->getValue(), $this->height->getValue(), $oldWidth, $oldHeight);
364
365 View Code Duplication
                if ($this->sourceType->getValue() == 'png' && $config->get('cms.images.perserve.png')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
366
                    imagepng($new_image);
367
                } else {
368
                    imagejpeg($new_image, null, 100 * $this->quality->getValue());
369
                }
370
371
                $this->cache($new_image);
0 ignored issues
show
Documentation introduced by
$new_image is of type resource, but the function expects a object<Alpha\View\Widget\Image>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
372
                imagedestroy($old_image);
373
                imagedestroy($new_image);
374
            }
375
        }
376
    }
377
378
    /**
379
     * Caches the image to the cache directory.
380
     *
381
     * @param image $image the binary GD image stream to save
382
     *
383
     * @since 1.0
384
     */
385
    private function cache($image)
386
    {
387
        $config = ConfigProvider::getInstance();
388
389
        if ($this->sourceType->getValue() == 'png' && $config->get('cms.images.perserve.png')) {
390
            imagepng($image, $this->filename);
391
        } else {
392
            imagejpeg($image, $this->filename, 100 * $this->quality->getValue());
393
        }
394
    }
395
396
    /**
397
     * Used to check the image cache for the image jpeg cache file.
398
     *
399
     * @return bool
400
     *
401
     * @since 1.0
402
     */
403
    private function checkCache()
404
    {
405
        return file_exists($this->filename);
406
    }
407
408
    /**
409
     * Method to load the content of the image cache file to the standard output stream (the browser).
410
     *
411
     * @since 1.0
412
     */
413
    private function loadCache()
414
    {
415
        readfile($this->filename);
416
    }
417
418
    /**
419
     * Converts a URL for an image to a relative file system path for the image, assuming it is
420
     * hosted on the same server as the application.
421
     *
422
     * @param string $imgURL
423
     *
424
     * @return string the path of the image
425
     *
426
     * @since 1.0
427
     */
428
    public static function convertImageURLToPath($imgURL)
429
    {
430
        $config = ConfigProvider::getInstance();
431
432
        $imgPath = str_replace($config->get('app.url').'/', '', $imgURL);
433
434
        return $imgPath;
435
    }
436
}
437