1 | <?php |
||||||
2 | |||||||
3 | |||||||
4 | namespace ElePHPant\Favicon; |
||||||
5 | |||||||
6 | |||||||
7 | /** |
||||||
8 | * Class Cropper |
||||||
9 | * |
||||||
10 | * Please report bugs on https://github.com/wilderamorim/favicon/issues |
||||||
11 | * |
||||||
12 | * @package ElePHPant\Favicon |
||||||
13 | * @author Wilder Amorim <[email protected]> |
||||||
14 | * @copyright Copyright (c) 2020, Uebi. All rights reserved |
||||||
15 | * @license MIT License |
||||||
16 | */ |
||||||
17 | class Cropper extends Tags |
||||||
18 | { |
||||||
19 | /** @var */ |
||||||
20 | protected $imageName; |
||||||
21 | |||||||
22 | /** @var */ |
||||||
23 | protected $imageMime; |
||||||
24 | |||||||
25 | /** @var string[] */ |
||||||
26 | private static $allowedExt = ['image/png', 'image/jpeg']; |
||||||
27 | |||||||
28 | /** @var array */ |
||||||
29 | private $names = []; |
||||||
30 | |||||||
31 | /** |
||||||
32 | * @return string |
||||||
33 | * @throws \Exception |
||||||
34 | */ |
||||||
35 | public function create() |
||||||
36 | { |
||||||
37 | if (!file_exists($this->sourceImage)) { |
||||||
38 | return 'Image not found'; |
||||||
39 | } |
||||||
40 | |||||||
41 | $this->imageMime = mime_content_type($this->sourceImage); |
||||||
42 | |||||||
43 | if (!in_array($this->imageMime, self::$allowedExt)) { |
||||||
44 | return 'Not a valid JPG or PNG image'; |
||||||
45 | } |
||||||
46 | |||||||
47 | if (!file_exists($this->saveInPath) || !is_dir($this->saveInPath)) { |
||||||
48 | if (!mkdir($this->saveInPath, 0755)) { |
||||||
49 | throw new \Exception('Could not create cache folder'); |
||||||
50 | } |
||||||
51 | } |
||||||
52 | |||||||
53 | return $this->cropper(); |
||||||
54 | } |
||||||
55 | |||||||
56 | /** |
||||||
57 | * @return string |
||||||
58 | */ |
||||||
59 | private function cropper(): string |
||||||
60 | { |
||||||
61 | $iteration = 0; |
||||||
62 | |||||||
63 | foreach ($this->sizes as $size) { |
||||||
64 | //width / height |
||||||
65 | $resolutions = explode('x', $size); |
||||||
66 | $width = $resolutions[0]; |
||||||
67 | $height = $resolutions[1]; |
||||||
68 | |||||||
69 | //name |
||||||
70 | $this->imageName = "{$this->names()[$iteration++]}{$width}x{$height}"; |
||||||
71 | |||||||
72 | //create |
||||||
73 | $thumb = imagecreatetruecolor($width, $height); |
||||||
0 ignored issues
–
show
Bug
introduced
by
![]() $width of type string is incompatible with the type integer expected by parameter $width of imagecreatetruecolor() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
74 | $this->image($thumb, $width, $height); |
||||||
0 ignored issues
–
show
$width of type string is incompatible with the type integer expected by parameter $width of ElePHPant\Favicon\Cropper::image() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() $height of type string is incompatible with the type integer expected by parameter $height of ElePHPant\Favicon\Cropper::image() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
75 | } |
||||||
76 | |||||||
77 | return "{$this->saveInPath}/{$this->imageName}"; |
||||||
78 | } |
||||||
79 | |||||||
80 | /** |
||||||
81 | * @param $thumb |
||||||
82 | * @param int $width |
||||||
83 | * @param int $height |
||||||
84 | */ |
||||||
85 | private function image($thumb, int $width, int $height) |
||||||
86 | { |
||||||
87 | $source = ($this->imageMime == self::$allowedExt[1] ? imagecreatefromjpeg($this->sourceImage) : imagecreatefrompng($this->sourceImage)); |
||||||
88 | |||||||
89 | imagealphablending($thumb, false); |
||||||
90 | imagesavealpha($thumb, true); |
||||||
91 | $this->resample($thumb, $source, $width, $height); |
||||||
92 | imagepng($thumb, "{$this->saveInPath}/{$this->imageName}.png", $this->quality); |
||||||
93 | |||||||
94 | imagedestroy($thumb); |
||||||
95 | imagedestroy($source); |
||||||
0 ignored issues
–
show
It seems like
$source can also be of type false ; however, parameter $image of imagedestroy() does only seem to accept resource , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
96 | } |
||||||
97 | |||||||
98 | /** |
||||||
99 | * @param $thumb |
||||||
100 | * @param $source |
||||||
101 | * @param int $width |
||||||
102 | * @param int $height |
||||||
103 | * @return bool |
||||||
104 | */ |
||||||
105 | private function resample($thumb, $source, int $width, int $height): bool |
||||||
106 | { |
||||||
107 | list($src_w, $src_h) = getimagesize($this->sourceImage); |
||||||
108 | $src_x = 0; |
||||||
109 | $src_y = 0; |
||||||
110 | |||||||
111 | $cmp_x = $src_w / $width; |
||||||
112 | $cmp_y = $src_h / $height; |
||||||
113 | |||||||
114 | if ($cmp_x > $cmp_y) { |
||||||
115 | $src_w = round($src_w / $cmp_x * $cmp_y); |
||||||
116 | $src_x = round(($src_w - ($src_w / $cmp_x * $cmp_y))); |
||||||
117 | } elseif ($cmp_y > $cmp_x) { |
||||||
118 | $src_h = round($src_h / $cmp_y * $cmp_x); |
||||||
119 | $src_y = round(($src_h - ($src_h / $cmp_y * $cmp_x))); |
||||||
120 | } |
||||||
121 | |||||||
122 | $src_x = (int)$src_x; |
||||||
123 | $src_h = (int)$src_h; |
||||||
124 | $src_y = (int)$src_y; |
||||||
125 | $src_y = (int)$src_y; |
||||||
126 | |||||||
127 | return imagecopyresampled($thumb, $source, 0, 0, $src_x, $src_y, $width, $height, $src_w, $src_h); |
||||||
128 | } |
||||||
129 | |||||||
130 | /** |
||||||
131 | * @return array |
||||||
132 | */ |
||||||
133 | private function names(): array |
||||||
134 | { |
||||||
135 | foreach (self::APPLE_TOUCH_ICON_PRECOMPOSED as $item) { |
||||||
136 | $this->names[] = 'apple-touch-icon-'; |
||||||
137 | } |
||||||
138 | |||||||
139 | foreach (self::ICON as $item) { |
||||||
140 | $this->names[] = 'favicon-'; |
||||||
141 | } |
||||||
142 | |||||||
143 | $msApplication = $this->msApplication; |
||||||
144 | array_shift($msApplication); |
||||||
145 | foreach ($msApplication as $item) { |
||||||
146 | $this->names[] = 'mstile-'; |
||||||
147 | } |
||||||
148 | |||||||
149 | return $this->names; |
||||||
150 | } |
||||||
151 | } |