Completed
Push — analysis-D2M1DL ( 825c2b )
by Arnaud
06:16 queued 11s
created

Image::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
/*
3
 * This file is part of the Cecil/Cecil package.
4
 *
5
 * Copyright (c) Arnaud Ligny <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Cecil\Assets;
12
13
use Cecil\Builder;
14
use Cecil\Config;
15
use Cecil\Exception\Exception;
16
use Cecil\Util;
17
use Intervention\Image\Exception\NotReadableException;
18
use Intervention\Image\ImageManagerStatic as ImageManager;
19
20
class Image
21
{
22
    /** @var Config */
23
    private $config;
24
25
    public function __construct(Builder $builder)
26
    {
27
        $this->config = $builder->getConfig();
28
    }
29
30
    /**
31
     * Resize an image.
32
     *
33
     * @param string $path Image path (relative from static/ dir or external)
34
     * @param int    $size Image new size (width)
35
     *
36
     * @return string Path to image thumbnail
37
     */
38
    public function resize(string $path, int $size): string
39
    {
40
        $external = false;
41
42
        // is external image?
43
        if (Util::isExternalUrl($path)) {
44
            $external = true;
45
            $source = $path;
46
        }
47
48
        if (!$external) {
49
            // source
50
            $source = $this->config->getStaticPath().'/'.$path;
51
            if (!Util::getFS()->exists($source)) {
52
                throw new Exception(sprintf('Can\'t resize "%s": file doesn\'t exits.', $path));
53
            }
54
            // destination
55
            // ie: .cache/images/thumbs
56
            $thumbsDir = (string) $this->config->get('cache.dir').'/'.$this->config->get('cache.images.dir').'/'.$this->config->get('cache.images.thumbs.dir').'/'.$size;
0 ignored issues
show
Bug introduced by
Are you sure $this->config->get('cache.images.dir') of type array|mixed|null can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

56
            $thumbsDir = (string) $this->config->get('cache.dir').'/'./** @scrutinizer ignore-type */ $this->config->get('cache.images.dir').'/'.$this->config->get('cache.images.thumbs.dir').'/'.$size;
Loading history...
Bug introduced by
Are you sure $this->config->get('cache.images.thumbs.dir') of type array|mixed|null can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

56
            $thumbsDir = (string) $this->config->get('cache.dir').'/'.$this->config->get('cache.images.dir').'/'./** @scrutinizer ignore-type */ $this->config->get('cache.images.thumbs.dir').'/'.$size;
Loading history...
57
            // ie: .cache/images/thumbs/img/logo.png
58
            $imageRelPath = $thumbsDir.'/'.ltrim($path, '/');
59
            // full absolute path
60
            $destination = $this->config->getDestinationDir().'/'.$imageRelPath;
61
            if ((bool) $this->config->get('cache.external')) {
62
                $destination = $imageRelPath;
63
            }
64
        }
65
66
        // is size is already OK?
67
        list($width, $height) = getimagesize($external ? $path : $source);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $source does not seem to be defined for all execution paths leading up to this point.
Loading history...
68
        if ($width <= $size && $height <= $size) {
69
            return $path;
70
        }
71
72
        // if GD extension is not installed: can't process
73
        if (!extension_loaded('gd')) {
74
            return $path;
75
        }
76
77
        // external image: return data URL
78
        if ($external) {
79
            try {
80
                $img = ImageManager::make($path);
81
            } catch (NotReadableException $e) {
82
                throw new Exception(sprintf('Cannot get image "%s"', $path));
83
            }
84
85
            return (string) $img->encode('data-url');
86
        }
87
88
        // resize
89
        if (!Util::getFS()->exists($destination)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $destination does not seem to be defined for all execution paths leading up to this point.
Loading history...
90
            $img = ImageManager::make($source);
91
            $img->resize($size, null, function (\Intervention\Image\Constraint $constraint) {
92
                $constraint->aspectRatio();
93
                $constraint->upsize();
94
            });
95
            // is a sub dir is necessary?
96
            $imageSubDir = Util::getFS()->makePathRelative('/'.dirname($imageRelPath), '/'.$thumbsDir.'/');
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $imageRelPath does not seem to be defined for all execution paths leading up to this point.
Loading history...
Comprehensibility Best Practice introduced by
The variable $thumbsDir does not seem to be defined for all execution paths leading up to this point.
Loading history...
97
            if (!empty($imageSubDir)) {
98
                $destDir = $this->config->getCacheImagesThumbsPath().'/'.$size.'/'.$imageSubDir;
99
                Util::getFS()->mkdir($destDir);
100
            }
101
            $img->save($destination);
102
        }
103
104
        // return relative path
105
        return '/'.$this->config->get('cache.images.dir').'/'.$this->config->get('cache.images.thumbs.dir').'/'.$size.'/'.ltrim($path, '/');
106
    }
107
}
108