Test Failed
Push — master ( f9dc8e...64f7bc )
by Petr
08:10
created

Processor::width()   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

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 9
rs 10
ccs 4
cts 4
cp 1
crap 2
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 9
    }
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
    public function resize(?int $width = null, ?int $height = null): self
84 1
    {
85
        $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 1
            // @codeCoverageIgnoreStart
92 1
            imagedestroy($resource);
93
            throw new ImagesException($this->getImLang()->imImageCannotResize());
94
        }
95
        // @codeCoverageIgnoreEnd
96
        imagedestroy($this->getResource());
97
        $this->resource = $resource;
98 1
        return $this;
99 1
    }
100 1
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
    public function resample(?int $width = null, ?int $height = null)
109
    {
110 7
        $fromWidth = $this->width();
111
        $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 7
            // @codeCoverageIgnoreStart
117 7
            imagedestroy($resource);
118 7
            throw new ImagesException($this->getImLang()->imImageCannotResample());
119
        }
120
        // @codeCoverageIgnoreEnd
121
        imagedestroy($this->getResource());
122
        $this->resource = $resource;
123
        return $this;
124 7
    }
125 7
126 7
    /**
127
     * Create empty image resource
128
     * @param int $width
129
     * @param int $height
130
     * @throws ImagesException
131
     * @return \GdImage|resource
132
     */
133
    protected function create(int $width, int $height)
134
    {
135
        $resource = imagecreatetruecolor($width, $height);
136 8
        if (false === $resource) {
137
            // @codeCoverageIgnoreStart
138 8
            throw new ImagesException($this->getImLang()->imImageCannotCreateEmpty());
139 8
        }
140
        // @codeCoverageIgnoreEnd
141
        return $resource;
142
    }
143
144 8
    /**
145
     * @throws ImagesException
146
     * @return int
147
     */
148
    public function width(): int
149
    {
150
        $size = imagesx($this->getResource());
151 9
        if (false === $size) {
152
            // @codeCoverageIgnoreStart
153 9
            throw new ImagesException($this->getImLang()->imImageCannotGetSize());
154 9
        }
155 9
        // @codeCoverageIgnoreEnd
156
        return intval($size);
157
    }
158
159
    /**
160 9
     * @throws ImagesException
161
     * @return int
162
     */
163
    public function height(): int
164
    {
165
        $size = imagesy($this->getResource());
166
        if (false === $size) {
167 10
            // @codeCoverageIgnoreStart
168
            throw new ImagesException($this->getImLang()->imImageCannotGetSize());
169 10
        }
170 9
        // @codeCoverageIgnoreEnd
171 9
        return intval($size);
172
    }
173
174
    /**
175
     * @throws ImagesException
176 9
     * @return \GdImage|resource
177
     */
178
    public function getResource()
179
    {
180
        if (empty($this->resource)) {
181
            throw new ImagesException($this->getImLang()->imImageLoadFirst());
182 10
        }
183
        return $this->resource;
184 10
    }
185
}
186