Passed
Push — master ( 64f7bc...187130 )
by Petr
02:19
created

Processor::create()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 9
ccs 4
cts 5
cp 0.8
crap 2.032
rs 10
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\Traits\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->setImLang($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->getImLang()->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->getImLang());
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
        $processor = $this->factory->getByType($type, $this->getImLang());
72 9
        $processor->save($tempPath, $this->getResource());
73 9
        return $this;
74
    }
75
76
    /**
77
     * Change image size - cut it to desired size
78
     * @param int|null $width
79
     * @param int|null $height
80
     * @throws ImagesException
81
     * @return $this
82
     */
83 1
    public function resize(?int $width = null, ?int $height = null): self
84
    {
85 1
        $fromWidth = $this->width();
86 1
        $fromHeight = $this->height();
87 1
        $width = (!is_null($width) && (0 < $width)) ? intval($width) : $fromWidth;
88 1
        $height = (!is_null($height) && (0 < $height)) ? intval($height) : $fromHeight;
89 1
        $resource = $this->create($width, $height);
90 1
        if (false === imagecopyresized($resource, $this->getResource(), 0, 0, 0, 0, $width, $height, $fromWidth, $fromHeight)) {
91
            // @codeCoverageIgnoreStart
92
            imagedestroy($resource);
93
            throw new ImagesException($this->getImLang()->imImageCannotResize());
94
        }
95
        // @codeCoverageIgnoreEnd
96 1
        imagedestroy($this->getResource());
97 1
        $this->resource = $resource;
98 1
        return $this;
99
    }
100
101
    /**
102
     * Change image size - content will change its proportions according the passed sizes
103
     * @param int|null $width
104
     * @param int|null $height
105
     * @throws ImagesException
106
     * @return $this
107
     */
108 7
    public function resample(?int $width = null, ?int $height = null)
109
    {
110 7
        $fromWidth = $this->width();
111 7
        $fromHeight = $this->height();
112 7
        $width = (!is_null($width) && (0 < $width)) ? intval($width) : $fromWidth;
113 7
        $height = (!is_null($height) && (0 < $height)) ? intval($height) : $fromHeight;
114 7
        $resource = $this->create($width, $height);
115 7
        if (false === imagecopyresampled($resource, $this->getResource(), 0, 0, 0, 0, $width, $height, $fromWidth, $fromHeight)) {
116
            // @codeCoverageIgnoreStart
117
            imagedestroy($resource);
118
            throw new ImagesException($this->getImLang()->imImageCannotResample());
119
        }
120
        // @codeCoverageIgnoreEnd
121 7
        imagedestroy($this->getResource());
122 7
        $this->resource = $resource;
123 7
        return $this;
124
    }
125
126
    /**
127
     * Create empty image resource
128
     * @param int $width
129
     * @param int $height
130
     * @throws ImagesException
131
     * @return \GdImage|resource
132
     */
133 8
    protected function create(int $width, int $height)
134
    {
135 8
        $resource = imagecreatetruecolor($width, $height);
136 8
        if (false === $resource) {
137
            // @codeCoverageIgnoreStart
138
            throw new ImagesException($this->getImLang()->imImageCannotCreateEmpty());
139
        }
140
        // @codeCoverageIgnoreEnd
141 8
        return $resource;
142
    }
143
144
    /**
145
     * @throws ImagesException
146
     * @return int
147
     */
148 9
    public function width(): int
149
    {
150 9
        $size = imagesx($this->getResource());
151 9
        if (false === $size) {
152
            // @codeCoverageIgnoreStart
153
            throw new ImagesException($this->getImLang()->imImageCannotGetSize());
154
        }
155
        // @codeCoverageIgnoreEnd
156 9
        return intval($size);
157
    }
158
159
    /**
160
     * @throws ImagesException
161
     * @return int
162
     */
163 10
    public function height(): int
164
    {
165 10
        $size = imagesy($this->getResource());
166 9
        if (false === $size) {
167
            // @codeCoverageIgnoreStart
168
            throw new ImagesException($this->getImLang()->imImageCannotGetSize());
169
        }
170
        // @codeCoverageIgnoreEnd
171 9
        return intval($size);
172
    }
173
174
    /**
175
     * @throws ImagesException
176
     * @return \GdImage|resource
177
     */
178 10
    public function getResource()
179
    {
180 10
        if (empty($this->resource)) {
181 1
            throw new ImagesException($this->getImLang()->imImageLoadFirst());
182
        }
183 9
        return $this->resource;
184
    }
185
}
186