Passed
Push — master ( afe694...4c5a84 )
by Petr
02:23
created

Graphics   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Test Coverage

Coverage 72.72%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 53
c 1
b 0
f 0
dl 0
loc 116
ccs 40
cts 55
cp 0.7272
rs 10
wmc 16

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setSizes() 0 4 1
A resize() 0 14 2
A orientate() 0 24 5
A getLibSizes() 0 6 2
A check() 0 18 5
1
<?php
2
3
namespace kalanis\kw_images;
4
5
6
use kalanis\kw_images\Interfaces\IExifConstants;
7
use kalanis\kw_images\Interfaces\IIMTranslations;
8
use kalanis\kw_images\Interfaces\ISizes;
9
use kalanis\kw_images\Traits\TSizes;
10
use kalanis\kw_images\Traits\TType;
11
use kalanis\kw_mime\Interfaces\IMime;
12
use kalanis\kw_mime\MimeException;
13
14
15
/**
16
 * Class Graphics
17
 * Main image itself
18
 * @package kalanis\kw_images\Graphics
19
 */
20
class Graphics
21
{
22
    use TType;
23
    use TSizes;
24
25
    protected Graphics\Processor $libGraphics;
26
    protected ?ISizes $libSizes = null;
27
28 30
    public function __construct(Graphics\Processor $libGraphics, IMime $libMime, ?IIMTranslations $lang = null)
29
    {
30 30
        $this->initType($libMime, $lang);
31 30
        $this->libGraphics = $libGraphics;
32 30
    }
33
34 13
    public function setSizes(ISizes $sizes): self
35
    {
36 13
        $this->libSizes = $sizes;
37 13
        return $this;
38
    }
39
40
    /**
41
     * @param string $tempPath path to temp file
42
     * @param string[] $realSourceName real file name for extension detection of source image
43
     * @throws ImagesException
44
     * @throws MimeException
45
     * @return bool
46
     */
47 8
    public function check(string $tempPath, array $realSourceName): bool
48
    {
49 8
        $this->getLibSizes();
50 6
        $size = @filesize($tempPath);
51 6
        if (false === $size) {
52 1
            throw new ImagesException($this->getImLang()->imImageSizeExists());
53
        }
54 5
        if ($this->getLibSizes()->getMaxFileSize() < $size) {
55 1
            throw new ImagesException($this->getImLang()->imImageSizeTooLarge());
56
        }
57 4
        $this->libGraphics->load($this->getType($realSourceName), $tempPath);
58 4
        if ($this->getLibSizes()->getMaxInHeight() < $this->libGraphics->height()) {
59 1
            throw new ImagesException($this->getImLang()->imImageSizeTooLarge());
60
        }
61 3
        if ($this->getLibSizes()->getMaxInWidth() < $this->libGraphics->width()) {
62 1
            throw new ImagesException($this->getImLang()->imImageSizeTooLarge());
63
        }
64 2
        return true;
65
    }
66
67
    /**
68
     * @param string $tempPath path to temp file which will be loaded and saved
69
     * @param string[] $realSourceName real file name for extension detection of source image
70
     * @param string[]|null $realTargetName real file name for extension detection of target image
71
     * @throws ImagesException
72
     * @throws MimeException
73
     * @return bool
74
     */
75 9
    public function resize(string $tempPath, array $realSourceName, ?array $realTargetName = null): bool
76
    {
77 9
        $this->getLibSizes();
78 8
        $realTargetName = is_null($realTargetName) ? $realSourceName : $realTargetName;
79 8
        $this->libGraphics->load($this->getType($realSourceName), $tempPath);
80 6
        $sizes = $this->calculateSize(
81 6
            $this->libGraphics->width(),
82 6
            $this->getLibSizes()->getMaxStoreWidth(),
83 6
            $this->libGraphics->height(),
84 6
            $this->getLibSizes()->getMaxStoreHeight()
85
        );
86 6
        $this->libGraphics->resample($sizes['width'], $sizes['height']);
87 6
        $this->libGraphics->save($this->getType($realTargetName), $tempPath);
88 6
        return true;
89
    }
90
91
    /**
92
     * @param string $tempPath path to temp file which will be loaded and saved
93
     * @param string[] $realSourceName real file name for extension detection of source image
94
     * @param string[]|null $realTargetName real file name for extension detection of target image
95
     * @throws ImagesException
96
     * @throws MimeException
97
     * @return bool
98
     * @link https://stackoverflow.com/questions/7489742/php-read-exif-data-and-adjust-orientation
99
     */
100 3
    public function orientate(string $tempPath, array $realSourceName, ?array $realTargetName = null): bool
101
    {
102 3
        $exif = @exif_read_data($tempPath);
103 3
        if (false === $exif) {
104 3
            throw new ImagesException($this->getImLang()->imImageCannotOrientate());
105
        }
106
        if (!empty($exif['Orientation']) && in_array($exif['Orientation'], [
107
                IExifConstants::EXIF_ORIENTATION_NORMAL,
108
                IExifConstants::EXIF_ORIENTATION_MIRROR_SIMPLE,
109
                IExifConstants::EXIF_ORIENTATION_UPSIDE_DOWN,
110
                IExifConstants::EXIF_ORIENTATION_MIRROR_UPSIDE_DOWN,
111
                IExifConstants::EXIF_ORIENTATION_ON_LEFT,
112
                IExifConstants::EXIF_ORIENTATION_MIRROR_ON_LEFT,
113
                IExifConstants::EXIF_ORIENTATION_ON_RIGHT,
114
                IExifConstants::EXIF_ORIENTATION_MIRROR_ON_RIGHT,
115
            ])) {
116
            $realTargetName = is_null($realTargetName) ? $realSourceName : $realTargetName;
117
            $this->libGraphics->load($this->getType($realSourceName), $tempPath);
118
            $this->libGraphics->orientate($exif['Orientation']);
119
            $this->libGraphics->save($this->getType($realTargetName), $tempPath);
120
            return true;
121
        }
122
123
        return false;
124
    }
125
126
    /**
127
     * @throws ImagesException
128
     * @return ISizes
129
     */
130 16
    protected function getLibSizes(): ISizes
131
    {
132 16
        if (empty($this->libSizes)) {
133 3
            throw new ImagesException($this->getImLang()->imSizesNotSet());
134
        }
135 13
        return $this->libSizes;
136
    }
137
}
138