Passed
Push — master ( eb90b5...21801a )
by Petr
02:33
created

Graphics::loadGraphics()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 2
dl 0
loc 9
ccs 6
cts 6
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace kalanis\kw_images;
4
5
6
use kalanis\kw_images\Interfaces\IIMTranslations;
7
use kalanis\kw_images\Interfaces\ISizes;
8
use kalanis\kw_images\Traits\TSizes;
9
use kalanis\kw_images\Traits\TType;
10
use kalanis\kw_mime\Interfaces\IMime;
11
use kalanis\kw_mime\MimeException;
12
13
14
/**
15
 * Class Graphics
16
 * Main image itself
17
 * @package kalanis\kw_images\Graphics
18
 */
19
class Graphics
20
{
21
    use TType;
22
    use TSizes;
23
24
    protected Graphics\Processor $libGraphics;
25
    protected ?ISizes $libSizes = null;
26
27 38
    public function __construct(Graphics\Processor $libGraphics, IMime $libMime, ?IIMTranslations $lang = null)
28
    {
29 38
        $this->initType($libMime, $lang);
30 38
        $this->libGraphics = $libGraphics;
31
    }
32
33 17
    public function setSizes(ISizes $sizes): self
34
    {
35 17
        $this->libSizes = $sizes;
36 17
        return $this;
37
    }
38
39
    /**
40
     * @param string $tempPath path to temp file
41
     * @param string[] $realSourceName real file name for extension detection of source image
42
     * @throws ImagesException
43
     * @throws MimeException
44
     * @return bool
45
     */
46 8
    public function check(string $tempPath, array $realSourceName): bool
47
    {
48 8
        $this->getLibSizes();
49 6
        $size = @filesize($tempPath);
50 6
        if (false === $size) {
51 1
            throw new ImagesException($this->getImLang()->imImageSizeExists());
52
        }
53 5
        if ($this->getLibSizes()->getMaxFileSize() < $size) {
54 1
            throw new ImagesException($this->getImLang()->imImageSizeTooLarge());
55
        }
56 4
        $this->loadGraphics($realSourceName, $tempPath);
57 4
        if ($this->getLibSizes()->getMaxInHeight() < $this->libGraphics->height()) {
58 1
            throw new ImagesException($this->getImLang()->imImageSizeTooLarge());
59
        }
60 3
        if ($this->getLibSizes()->getMaxInWidth() < $this->libGraphics->width()) {
61 1
            throw new ImagesException($this->getImLang()->imImageSizeTooLarge());
62
        }
63 2
        return true;
64
    }
65
66
    /**
67
     * @param string $tempPath path to temp file which will be loaded and saved
68
     * @param string[] $realSourceName real file name for extension detection of source image
69
     * @param string[]|null $realTargetName real file name for extension detection of target image
70
     * @throws ImagesException
71
     * @throws MimeException
72
     * @return bool
73
     */
74 12
    public function resize(string $tempPath, array $realSourceName, ?array $realTargetName = null): bool
75
    {
76 12
        $this->getLibSizes();
77 11
        $realTargetName = is_null($realTargetName) ? $realSourceName : $realTargetName;
78 11
        $this->loadGraphics($realSourceName, $tempPath);
79 8
        $sizes = $this->calculateSize(
80 8
            $this->libGraphics->width(),
81 8
            $this->getLibSizes()->getMaxStoreWidth(),
82 8
            $this->libGraphics->height(),
83 8
            $this->getLibSizes()->getMaxStoreHeight()
84 8
        );
85 8
        $this->libGraphics->resample($sizes['width'], $sizes['height']);
86 8
        $this->libGraphics->save($this->getType($realTargetName), $tempPath);
87 8
        return true;
88
    }
89
90
    /**
91
     * @param float|null $angle positive to rotate clockwise, negative counter-clockwise
92
     * @param int|null $flipMode
93
     * @param string $tempPath path to temp file which will be loaded and saved
94
     * @param string[] $realSourceName real file name for extension detection of source image
95
     * @param string[]|null $realTargetName real file name for extension detection of target image
96
     * @throws ImagesException
97
     * @throws MimeException
98
     * @return bool
99
     */
100 9
    public function rotate(?float $angle, ?int $flipMode, string $tempPath, array $realSourceName, ?array $realTargetName = null): bool
101
    {
102 9
        if (is_null($angle) && is_null($flipMode)) {
103 1
            return false;
104
        }
105
106 8
        $realTargetName = is_null($realTargetName) ? $realSourceName : $realTargetName;
107 8
        $this->loadGraphics($realSourceName, $tempPath);
108 7
        if (!empty($angle)) {
109 5
            $this->libGraphics->rotate($angle);
110
        }
111 7
        if (!is_null($flipMode)) {
112 2
            $this->libGraphics->flip($flipMode);
113
        }
114 7
        $this->libGraphics->save($this->getType($realTargetName), $tempPath);
115 7
        return true;
116
    }
117
118
    /**
119
     * @throws ImagesException
120
     * @return ISizes
121
     */
122 19
    protected function getLibSizes(): ISizes
123
    {
124 19
        if (empty($this->libSizes)) {
125 3
            throw new ImagesException($this->getImLang()->imSizesNotSet());
126
        }
127 16
        return $this->libSizes;
128
    }
129
130
    /**
131
     * @param string[] $realName
132
     * @param string $tempPath
133
     * @throws ImagesException
134
     * @throws MimeException
135
     * @return void
136
     */
137 20
    protected function loadGraphics(array $realName, string $tempPath): void
138
    {
139
        try {
140 20
            $this->libGraphics->load($this->getType($realName), $tempPath);
141 4
        } catch (ImagesException $ex) {
142 4
            if (ImagesException::TRAIT_WRONG_MIME == $ex->getCode()) {
143 3
                $this->libGraphics->load('autodetect', $tempPath);
144
            } else {
145 1
                throw $ex;
146
            }
147
        }
148
    }
149
}
150