Completed
Push — feature-images ( defc63...7ab818 )
by Arnaud
02:34
created

Image   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 43
c 3
b 0
f 0
dl 0
loc 96
rs 10
wmc 12

2 Methods

Rating   Name   Duplication   Size   Complexity  
B resize() 0 66 11
A __construct() 0 3 1
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 string */
25
    private $path;
26
    /** @var int */
27
    private $size;
28
    /** @var bool */
29
    private $local = true;
30
    /** @var string */
31
    private $source;
32
    /** @var string */
33
    private $destination = null;
34
35
    const CACHE_IMAGES_THUMBS_DIR = 'images/thumbs';
36
37
    public function __construct(Builder $builder)
38
    {
39
        $this->config = $builder->getConfig();
40
    }
41
42
    /**
43
     * Resize an image.
44
     *
45
     * @param string $path       Image path (relative from static/ dir or external)
46
     * @param int    $this->size Image new size (width)
47
     *
48
     * @return string Path to image thumbnail
49
     */
50
    public function resize(string $path, int $size): string
51
    {
52
        // is not a local image?
53
        if (Util::isExternalUrl($path)) {
54
            $this->local = false;
55
        }
56
57
        $this->path = '/'.ltrim($path, '/');
58
        $this->size = $size;
59
        $returnPath = '/'.self::CACHE_IMAGES_THUMBS_DIR.'/'.$this->size.$this->path;
60
61
        // source file
62
        if ($this->local) {
63
            $this->source = $this->config->getStaticPath().$this->path;
64
            if (!Util::getFS()->exists($this->source)) {
65
                throw new Exception(sprintf('Can\'t process "%s": file doesn\'t exists.', $this->source));
66
            }
67
        } else {
68
            $this->source = $path;
69
            if (!Util::isUrlFileExists($this->source)) {
70
                throw new Exception(sprintf('Can\'t process "%s": remonte file doesn\'t exists.', $this->source));
71
            }
72
        }
73
74
        // images cache path
75
        $this->cachePath = $this->config->getCachePath().'/'.self::CACHE_IMAGES_THUMBS_DIR.'/';
0 ignored issues
show
Bug Best Practice introduced by
The property cachePath does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
76
77
        // is size is already OK?
78
        list($width, $height) = getimagesize($this->source);
79
        if ($width <= $this->size && $height <= $this->size) {
80
            return $this->path;
81
        }
82
83
        // if GD extension is not installed: can't process
84
        if (!extension_loaded('gd')) {
85
            throw new Exception('GD extension is required to use images resize.');
86
        }
87
88
        $this->destination = $this->cachePath.$this->size.$this->path;
89
90
        if (Util::getFS()->exists($this->destination)) {
91
            return $returnPath;
92
        }
93
94
        // image object
95
        try {
96
            $img = ImageManager::make($this->source);
97
            $img->resize($this->size, null, function (\Intervention\Image\Constraint $constraint) {
98
                $constraint->aspectRatio();
99
                $constraint->upsize();
100
            });
101
        } catch (NotReadableException $e) {
102
            throw new Exception(sprintf('Cannot get image "%s"', $this->path));
103
        }
104
105
        // return data:image for external image
106
        if (!$this->local) {
107
            return (string) $img->encode('data-url');
108
        }
109
110
        // save file
111
        Util::getFS()->mkdir(dirname($this->destination));
112
        $img->save($this->destination);
113
114
        // return new path
115
        return $returnPath;
116
    }
117
}
118