Passed
Branch master (a17811)
by compolom
03:05 queued 01:14
created

AbstractImage::setWidth()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
nc 1
cc 1
eloc 2
nop 1
1
<?php
2
3
namespace Compolomus\Compomage;
4
5
use Compolomus\Compomage\Interfaces\ImageInterface;
6
7
abstract class AbstractImage
8
{
9
    protected const POSITIONS = [
10
        'NORTHWEST' => ['x' => 0, 'y' => 0, 'padX' => 10, 'padY' => 10],
11
        'NORTH' => ['x' => 1, 'y' => 0, 'padX' => 0, 'padY' => 10],
12
        'NORTHEAST' => ['x' => 2, 'y' => 0, 'padX' => -10, 'padY' => 10],
13
        'WEST' => ['x' => 0, 'y' => 1, 'padX' => 10, 'padY' => 0],
14
        'CENTER' => ['x' => 1, 'y' => 1, 'padX' => 0, 'padY' => 0],
15
        'EAST' => ['x' => 2, 'y' => 1, 'padX' => -10, 'padY' => 0],
16
        'SOUTHWEST' => ['x' => 0, 'y' => 2, 'padX' => 10, 'padY' => -10],
17
        'SOUTH' => ['x' => 1, 'y' => 2, 'padX' => 0, 'padY' => -10],
18
        'SOUTHEAST' => ['x' => 2, 'y' => 2, 'padX' => -10, 'padY' => -10]
19
    ];
20
21
    protected $width;
22
23
    protected $height;
24
25
    abstract public function getImage();
26
27
    /**
28
     * @param string $mode
29
     * @param int $param
30
     * @return ImageInterface
31
     * @throws \Exception
32
     */
33
    public function resizeBy(string $mode, int $param): ImageInterface
34
    {
35
        switch ($mode) {
36
            case 'width':
37
                return $this->resizeByWidth($param);
38
            case 'height':
39
                return $this->resizeByHeight($param);
40
            case 'percent':
41
                return $this->resizeByPercent($param);
42
            default:
43
                throw new \InvalidArgumentException('Unsupported mode type by resize');
44
        }
45
    }
46
47
    /**
48
     * @param int $width
49
     * @return ImageInterface
50
     */
51
    public function resizeByWidth(int $width): ImageInterface
52
    {
53
        return $this->resize($width, $this->getHeight() * ($width / $this->getWidth()));
54
    }
55
56
    abstract protected function resize(int $width, int $height): ImageInterface;
57
58
    public function getHeight(): int
59
    {
60
        return $this->height;
61
    }
62
63
    protected function setHeight(int $height): void
64
    {
65
        $this->height = $height;
66
    }
67
68
    public function getWidth(): int
69
    {
70
        return $this->width;
71
    }
72
73
    protected function setWidth(int $width): void
74
    {
75
        $this->width = $width;
76
    }
77
78
    /**
79
     * @param int $height
80
     * @return ImageInterface
81
     */
82
    public function resizeByHeight(int $height): ImageInterface
83
    {
84
        return $this->resize($this->getWidth() * ($height / $this->getHeight()), $height);
85
    }
86
87
    /**
88
     * @param int $percent
89
     * @return ImageInterface
90
     */
91
    public function resizeByPercent(int $percent): ImageInterface
92
    {
93
        $width = $this->getWidth() * ($percent / 100);
94
        $height = $this->getHeight() * ($percent / 100);
95
        return $this->resize($width, $height);
96
    }
97
98
    /**
99
     * @param Image $watermark
100
     * @param string $position
101
     * @return ImageInterface
102
     * @throws \Exception
103
     */
104
    public function watermark(Image $watermark, string $position): ImageInterface
105
    {
106
        if (!array_key_exists(strtoupper($position), self::POSITIONS)) {
107
            throw new \InvalidArgumentException('Wrong position');
108
        }
109
110
        return $this->prepareWatermark(
111
            $watermark,
112
            (int)((($this->getWidth() - $watermark->getWidth()) / 2) * self::POSITIONS[strtoupper($position)]['x']) + self::POSITIONS[strtoupper($position)]['padX'],
113
            (int)((($this->getHeight() - $watermark->getHeight()) / 2) * self::POSITIONS[strtoupper($position)]['y']) + self::POSITIONS[strtoupper($position)]['padY']
114
        );
115
    }
116
117
    abstract protected function prepareWatermark(Image $watermark, int $x, int $y): ImageInterface;
118
119
    public function thumbnail(int $width, int $height): ImageInterface
120
    {
121
        $newHeight = $height;
122
        $newWidth = $width;
123
124
        $this->getWidth() / $this->getHeight() >= $width / $height
125
            ? $newWidth = (int)($this->getWidth() / ($this->getHeight() / $height))
126
            : $newHeight = (int)($this->getHeight() / ($this->getWidth() / $width));
127
128
        return $this->prepareThumbnail($width, $height, $newWidth, $newHeight);
0 ignored issues
show
Unused Code introduced by
The call to AbstractImage::prepareThumbnail() has too many arguments starting with $newWidth.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
129
    }
130
131
    abstract protected function prepareThumbnail(int $width, int $height): ImageInterface;
132
133
    public function getBase64(): string
134
    {
135
        return chunk_split(base64_encode($this->__toString()));
136
137
    }
138
139
    abstract public function __toString(): string;
140
141
    abstract protected function setImage($image): void;
142
143
    abstract protected function setSizes(): void;
144
145
    /**
146
     * @param string $image
147
     * @throws \Exception
148
     */
149
    protected function init(string $image): void
150
    {
151
        switch ($image) {
152
            // base64
153
            case base64_encode(base64_decode($image, true)) === $image:
154
                $this->getImageByBase64($image);
155
                break;
156
            // URL
157
            case (0 === strpos($image, 'http')):
158
                $this->getImageByURL($image);
159
                break;
160
            // Local file
161
            default:
162
                $this->tmp(file_get_contents($image));
163
        }
164
    }
165
166
    /**
167
     * @param string $base64
168
     * @throws \Exception
169
     */
170
    protected function getImageByBase64(string $base64): void
171
    {
172
        $this->tmp(base64_decode($base64));
173
    }
174
175
    abstract protected function tmp(string $source): ImageInterface;
176
177
    /**
178
     * @param string $url
179
     * @return \Exception|null
180
     * @throws \Exception
181
     */
182
    protected function getImageByURL(string $url): ?\Exception
183
    {
184
        if (getimagesize($url)) {
185
            $upload = new \SplFileObject($url, 'rb');
186
            $image = '';
187
            while (!$upload->eof()) {
188
                $image .= $upload->fgets();
189
            }
190
        } else {
191
            throw new \InvalidArgumentException('Unsupported image type');
192
        }
193
        $this->tmp($image);
194
195
        return null;
196
    }
197
}
198