Passed
Push — master ( 8feef3...f9dc8e )
by Petr
09:08 queued 06:38
created

Processor   A

Complexity

Total Complexity 30

Size/Duplication

Total Lines 170
Duplicated Lines 0 %

Test Coverage

Coverage 88.06%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 61
c 1
b 0
f 0
dl 0
loc 170
ccs 59
cts 67
cp 0.8806
rs 10
wmc 30

9 Methods

Rating   Name   Duplication   Size   Complexity  
A save() 0 6 1
A load() 0 5 1
A checkResource() 0 4 2
A create() 0 9 2
B __construct() 0 18 8
A height() 0 10 2
A resize() 0 17 6
A width() 0 10 2
A resample() 0 17 6
1
<?php
2
3
namespace kalanis\kw_images\Graphics;
4
5
6
use kalanis\kw_images\ImagesException;
7
use kalanis\kw_images\Interfaces\IIMTranslations;
8
use kalanis\kw_images\TLang;
9
10
11
/**
12
 * Class Processor
13
 * @package kalanis\kw_images
14
 * Pass images in temporary local storage  - cannot work with images directly in main storage
15
 */
16
class Processor
17
{
18
    use TLang;
19
20
    /** @var Format\Factory */
21
    protected $factory = null;
22
    /** @var resource|\GdImage|null */
23
    protected $resource = null;
24
25
    /**
26
     * @param Format\Factory $factory
27
     * @param IIMTranslations|null $lang
28
     * @throws ImagesException
29
     */
30 24
    public function __construct(Format\Factory $factory, ?IIMTranslations $lang = null)
31
    {
32 24
        $this->setLang($lang);
33
34 24
        if (!(function_exists('imagecreatetruecolor')
35 24
            && function_exists('imagecolorallocate')
36 24
            && function_exists('imagesetpixel')
37 24
            && function_exists('imagecopyresized')
38 24
            && function_exists('imagecopyresampled')
39 24
            && function_exists('imagesx')
40 24
            && function_exists('imagesy')
41
        )) {
42
            // @codeCoverageIgnoreStart
43
            throw new ImagesException($this->getLang()->imGdLibNotPresent());
44
        }
45
        // @codeCoverageIgnoreEnd
46
47 24
        $this->factory = $factory;
48 24
    }
49
50
    /**
51
     * @param string $type
52
     * @param string $tempPath
53
     * @throws ImagesException
54
     * @return $this
55
     */
56 10
    public function load(string $type, string $tempPath): self
57
    {
58 10
        $processor = $this->factory->getByType($type, $this->getLang());
59 9
        $this->resource = $processor->load($tempPath);
60 9
        return $this;
61
    }
62
63
    /**
64
     * @param string $type
65
     * @param string $tempPath
66
     * @throws ImagesException
67
     * @return $this
68
     */
69 9
    public function save(string $type, string $tempPath): self
70
    {
71 9
        $this->checkResource();
72 9
        $processor = $this->factory->getByType($type, $this->getLang());
73 9
        $processor->save($tempPath, $this->resource);
74 9
        return $this;
75
    }
76
77
    /**
78
     * Change image size - cut it to desired size
79
     * @param int|null $width
80
     * @param int|null $height
81
     * @throws ImagesException
82
     * @return $this
83
     */
84 1
    public function resize(?int $width = null, ?int $height = null): self
85
    {
86 1
        $this->checkResource();
87 1
        $fromWidth = $this->width();
88 1
        $fromHeight = $this->height();
89 1
        $width = (!is_null($width) && (0 < $width)) ? intval($width) : $fromWidth;
90 1
        $height = (!is_null($height) && (0 < $height)) ? intval($height) : $fromHeight;
91 1
        $resource = $this->create($width, $height);
92 1
        if (false === imagecopyresized($resource, $this->resource, 0, 0, 0, 0, $width, $height, $fromWidth, $fromHeight)) {
93
            // @codeCoverageIgnoreStart
94
            imagedestroy($resource);
95
            throw new ImagesException($this->getLang()->imImageCannotResize());
96
        }
97
        // @codeCoverageIgnoreEnd
98 1
        imagedestroy($this->resource);
99 1
        $this->resource = $resource;
100 1
        return $this;
101
    }
102
103
    /**
104
     * Change image size - content will change its proportions according the passed sizes
105
     * @param int|null $width
106
     * @param int|null $height
107
     * @throws ImagesException
108
     * @return $this
109
     */
110 7
    public function resample(?int $width = null, ?int $height = null)
111
    {
112 7
        $this->checkResource();
113 7
        $fromWidth = $this->width();
114 7
        $fromHeight = $this->height();
115 7
        $width = (!is_null($width) && (0 < $width)) ? intval($width) : $fromWidth;
116 7
        $height = (!is_null($height) && (0 < $height)) ? intval($height) : $fromHeight;
117 7
        $resource = $this->create($width, $height);
118 7
        if (false === imagecopyresampled($resource, $this->resource, 0, 0, 0, 0, $width, $height, $fromWidth, $fromHeight)) {
119
            // @codeCoverageIgnoreStart
120
            imagedestroy($resource);
121
            throw new ImagesException($this->getLang()->imImageCannotResample());
122
        }
123
        // @codeCoverageIgnoreEnd
124 7
        imagedestroy($this->resource);
125 7
        $this->resource = $resource;
126 7
        return $this;
127
    }
128
129
    /**
130
     * Create empty image resource
131
     * @param int $width
132
     * @param int $height
133
     * @throws ImagesException
134
     * @return \GdImage|resource
135
     */
136 8
    protected function create(int $width, int $height)
137
    {
138 8
        $resource = imagecreatetruecolor($width, $height);
139 8
        if (false === $resource) {
140
            // @codeCoverageIgnoreStart
141
            throw new ImagesException($this->getLang()->imImageCannotCreateEmpty());
142
        }
143
        // @codeCoverageIgnoreEnd
144 8
        return $resource;
145
    }
146
147
    /**
148
     * @throws ImagesException
149
     * @return int
150
     */
151 9
    public function width(): int
152
    {
153 9
        $this->checkResource();
154 9
        $size = imagesx($this->resource);
155 9
        if (false === $size) {
156
            // @codeCoverageIgnoreStart
157
            throw new ImagesException($this->getLang()->imImageCannotGetSize());
158
        }
159
        // @codeCoverageIgnoreEnd
160 9
        return intval($size);
161
    }
162
163
    /**
164
     * @throws ImagesException
165
     * @return int
166
     */
167 10
    public function height(): int
168
    {
169 10
        $this->checkResource();
170 9
        $size = imagesy($this->resource);
171 9
        if (false === $size) {
172
            // @codeCoverageIgnoreStart
173
            throw new ImagesException($this->getLang()->imImageCannotGetSize());
174
        }
175
        // @codeCoverageIgnoreEnd
176 9
        return intval($size);
177
    }
178
179
    /**
180
     * @throws ImagesException
181
     */
182 10
    protected function checkResource(): void
183
    {
184 10
        if (empty($this->resource)) {
185 1
            throw new ImagesException($this->getLang()->imImageLoadFirst());
186
        }
187 9
    }
188
}
189