Completed
Push — Assets/Image ( 2eacfc...628edb )
by Arnaud
08:37 queued 05:30
created

Image::resize()   B

Complexity

Conditions 11
Paths 32

Size

Total Lines 60
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 11
eloc 31
c 3
b 0
f 0
nc 32
nop 2
dl 0
loc 60
rs 7.3166

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
    /** @var int */
25
    private $size;
26
    /** @var string */
27
    private $source;
28
    /** @var string */
29
    private $destination = null;
30
    /** @var string */
31
    private $imageRelPath = null;
32
    /** @var string */
33
    private $thumbsDir = null;
34
35
    public function __construct(Builder $builder)
36
    {
37
        $this->config = $builder->getConfig();
38
    }
39
40
    /**
41
     * Resize an image.
42
     *
43
     * @param string $path       Image path (relative from static/ dir or external)
44
     * @param int    $this->size Image new size (width)
45
     *
46
     * @return string Path to image thumbnail
47
     */
48
    public function resize(string $path, int $size): string
49
    {
50
        $external = false;
51
        $this->source = $path;
52
        $this->size = $size;
53
54
        // is external image?
55
        if (Util::isExternalUrl($path)) {
56
            $external = true;
57
        }
58
59
        if (!$external) {
60
            // source
61
            $this->source = $this->config->getStaticPath().'/'.$path;
62
            if (!Util::getFS()->exists($this->source)) {
63
                throw new Exception(sprintf('Can\'t resize "%s": file doesn\'t exits.', $path));
64
            }
65
            // destination
66
            // ie: .cache/images/thumbs
67
            $this->thumbsDir = (string) $this->config->get('cache.dir')
68
                .'/'.(string) $this->config->get('cache.images.dir')
69
                .'/'.(string) $this->config->get('cache.images.thumbs.dir')
70
                .'/'.$this->size;
71
            // ie: .cache/images/thumbs/img/logo.png
72
            $this->imageRelPath = $this->thumbsDir.'/'.ltrim($path, '/');
73
            // full absolute path
74
            $this->destination = $this->config->getDestinationDir().'/'.$this->imageRelPath;
75
            if ((bool) $this->config->get('cache.external')) {
76
                $this->destination = $this->imageRelPath;
77
            }
78
        }
79
80
        // is size is already OK?
81
        list($width, $height) = getimagesize($external ? $path : $this->source);
82
        if ($width <= $this->size && $height <= $this->size) {
83
            return $path;
84
        }
85
86
        // if GD extension is not installed: can't process
87
        if (!extension_loaded('gd')) {
88
            return $path;
89
        }
90
91
        // external image: return data URL
92
        if ($external) {
93
            try {
94
                $img = ImageManager::make($path);
95
            } catch (NotReadableException $e) {
96
                throw new Exception(sprintf('Cannot get image "%s"', $path));
97
            }
98
99
            return (string) $img->encode('data-url');
100
        }
101
102
        // resize
103
        $this->doResize();
104
105
        // return relative path
106
        return '/'.$this->config->get('cache.images.dir')
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

106
        return '/'./** @scrutinizer ignore-type */ $this->config->get('cache.images.dir')
Loading history...
107
            .'/'.(string) $this->config->get('cache.images.thumbs.dir').'/'.$this->size.'/'.ltrim($path, '/');
108
    }
109
110
    /**
111
     * Resize with Intervention ImageManager.
112
     */
113
    private function doResize()
114
    {
115
        if (!Util::getFS()->exists($this->destination)) {
116
            $img = ImageManager::make($this->source);
117
            $img->resize($this->size, null, function (\Intervention\Image\Constraint $constraint) {
118
                $constraint->aspectRatio();
119
                $constraint->upsize();
120
            });
121
            // is a sub dir is necessary?
122
            $imageSubDir = Util::getFS()->makePathRelative('/'.dirname($this->imageRelPath), '/'.$this->thumbsDir.'/');
123
            if (!empty($imageSubDir)) {
124
                $destDir = $this->config->getCacheImagesThumbsPath().'/'.$this->size.'/'.$imageSubDir;
125
                Util::getFS()->mkdir($destDir);
126
            }
127
            $img->save($this->destination);
128
        }
129
    }
130
}
131