PluginProbe
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress / 10.10.2
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress v10.10.2
11.1.1 11.1.0 11.0.5 11.0.4 11.0.3 11.0.2 11.0.1 11.0.0 10.11.1 10.11.0 10.10.2 10.10.1 10.10.0 10.9.1 trunk 10.0.0 10.0.1 10.1.0 10.1.1 10.1.2 10.1.3 10.1.4 10.2.0 10.2.1 10.2.2 All 62 releases
acymailing / back / Helpers / ImageHelper.php

ImageHelper.php in AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress 10.10.2, at back/Helpers/ImageHelper.php

250 lines 8.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace AcyMailing\Helpers;
4
5 use AcyMailing\Core\AcymObject;
6
7 class ImageHelper extends AcymObject
8 {
9 public string $error = '';
10 public int $maxHeight;
11 public int $maxWidth;
12 public string $destination;
13
14 public function removePictures(string $text): string
15 {
16 $return = preg_replace('#< *img((?!content_main_image)[^>])*>#Ui', '', $text);
17
18 return preg_replace('#< *div[^>]*class="jce_caption"[^>]*>[^<]*(< *div[^>]*>[^<]*<\/div>)*[^<]*<\/div>#Ui', '', $return);
19 }
20
21 public function available(): bool
22 {
23 if (!function_exists('gd_info')) {
24 $this->error = 'The GD library is not installed.';
25
26 return false;
27 }
28 if (!function_exists('getimagesize')) {
29 $this->error = 'Cound not find getimagesize function';
30
31 return false;
32 }
33 if (!function_exists('imagealphablending')) {
34 $this->error = "Please make sure you're using GD 2.0.1 or later version";
35
36 return false;
37 }
38
39 return true;
40 }
41
42 public function resizePictures(string $input): string
43 {
44 $this->destination = ACYM_MEDIA.'resized'.DS;
45 acym_createDir($this->destination);
46 $content = acym_absoluteURL($input);
47
48 preg_match_all('#<img([^>]*)>#Ui', $content, $results);
49 if (empty($results[1])) {
50 return $input;
51 }
52
53 $replace = [];
54
55 foreach ($results[1] as $onepicture) {
56 if (strpos($onepicture, 'donotresize') !== false) {
57 continue;
58 }
59
60 if (!preg_match('#src="([^"]*)"#Ui', $onepicture, $path)) {
61 continue;
62 }
63 $imageUrl = $path[1];
64
65 $imageUrl = preg_replace('/#joomlaImage.*/', '', $imageUrl);
66
67 $imageUrl = acym_internalUrlToPath($imageUrl);
68
69 $newPicture = $this->generateThumbnail($imageUrl);
70
71 $newDimension = 'max-width:'.$this->maxWidth.'px;max-height:'.$this->maxHeight.'px;';
72
73 if (empty($newPicture)) {
74 if (strpos($onepicture, 'style="') !== false) {
75 $replace[$onepicture] = preg_replace('#style="([^"]*)"#Uis', 'style="'.$newDimension.'$1"', $onepicture);
76 } else {
77 $replace[$onepicture] = ' style="'.$newDimension.'" '.$onepicture;
78 }
79 continue;
80 }
81
82 $newPicture['file'] = preg_replace('#^'.preg_quote(ACYM_ROOT, '#').'#i', ACYM_LIVE, $newPicture['file']);
83 $newPicture['file'] = str_replace(DS, '/', $newPicture['file']);
84 $replaceImage = [];
85 $replaceImage[$path[1]] = $newPicture['file'];
86 if (preg_match_all('#(width|height)(:|=) *"?([0-9]+)#i', $onepicture, $resultsSize)) {
87 foreach ($resultsSize[0] as $i => $oneArg) {
88 $newVal = (strtolower($resultsSize[1][$i]) == 'width') ? $newPicture['width'] : $newPicture['height'];
89 if ($newVal > $resultsSize[3][$i]) {
90 continue;
91 }
92 $replaceImage[$oneArg] = str_replace($resultsSize[3][$i], $newVal, $oneArg);
93 }
94 }
95
96 $replace[$onepicture] = str_replace(array_keys($replaceImage), $replaceImage, $onepicture);
97
98 if (strpos($replace[$onepicture], 'width') === false) {
99 if (strpos($onepicture, 'style="') !== false) {
100 $replace[$onepicture] = preg_replace('#style="([^"]*)"#Uis', 'style="'.$newDimension.'$1"', $replace[$onepicture]);
101 } else {
102 $replace[$onepicture] = ' style="'.$newDimension.'" '.$replace[$onepicture];
103 }
104 }
105 }
106
107 if (!empty($replace)) {
108 $input = str_replace(array_keys($replace), $replace, $content);
109 }
110
111 return $input;
112 }
113
114 public function generateThumbnail(string $picturePath): array
115 {
116 $paramsPos = strpos($picturePath, '?');
117 if ($paramsPos !== false) {
118 $picturePath = substr($picturePath, 0, $paramsPos);
119 }
120
121 [$currentwidth, $currentheight] = @getimagesize($picturePath);
122 if (empty($currentwidth) || empty($currentheight)) {
123 return [];
124 }
125 $factor = min($this->maxWidth / $currentwidth, $this->maxHeight / $currentheight);
126 if ($factor >= 1) {
127 return [];
128 }
129 $newWidth = round($currentwidth * $factor);
130 $newHeight = round($currentheight * $factor);
131
132 if (strpos($picturePath, 'http') === 0) {
133 $filename = substr($picturePath, strrpos($picturePath, '/') + 1);
134 } else {
135 $filename = basename($picturePath);
136 }
137
138 if (substr($picturePath, 0, 10) == 'data:image') {
139 preg_match('#data:image/([^;]{1,5});#', $picturePath, $resultextension);
140 if (empty($resultextension[1])) {
141 return [];
142 }
143 $extension = $resultextension[1];
144 $name = md5($picturePath);
145 } else {
146 $extension = strtolower(substr($filename, strrpos($filename, '.') + 1));
147 $name = strtolower(substr($filename, 0, strrpos($filename, '.')));
148 $name .= substr(@filemtime($picturePath), -4);
149 }
150
151 $newImage = md5($picturePath).'-'.$name.'thumb'.$this->maxWidth.'x'.$this->maxHeight.'.'.$extension;
152 if (empty($this->destination)) {
153 $newFile = dirname($picturePath).DS.$newImage;
154 } else {
155 $newFile = $this->destination.$newImage;
156 }
157
158 if (file_exists($newFile)) {
159 return [
160 'file' => $newFile,
161 'width' => $newWidth,
162 'height' => $newHeight,
163 ];
164 }
165
166 if (function_exists('exif_imagetype')) {
167 $imageRealType = exif_imagetype($picturePath);
168 } else {
169 if ($extension === 'gif') {
170 $imageRealType = IMAGETYPE_GIF;
171 } elseif ($extension === 'jpg' || $extension === 'jpeg') {
172 $imageRealType = IMAGETYPE_JPEG;
173 } elseif ($extension === 'png') {
174 $imageRealType = IMAGETYPE_PNG;
175 } elseif ($extension === 'webp') {
176 $imageRealType = IMAGETYPE_WEBP;
177 } else {
178 return [];
179 }
180 }
181
182 switch ($imageRealType) {
183 case IMAGETYPE_GIF:
184 $img = imagecreatefromgif($picturePath);
185 break;
186 case IMAGETYPE_JPEG:
187 $img = imagecreatefromjpeg($picturePath);
188 break;
189 case IMAGETYPE_PNG:
190 $img = imagecreatefrompng($picturePath);
191 break;
192 case IMAGETYPE_WEBP:
193 if (function_exists('imagecreatefromwebp')) {
194 $img = imagecreatefromwebp($picturePath);
195 }
196 break;
197 default:
198 return [];
199 }
200
201 if (empty($img)) {
202 return [];
203 }
204
205 $thumb = imagecreatetruecolor($newWidth, $newHeight);
206
207 if (in_array($imageRealType, [IMAGETYPE_GIF, IMAGETYPE_PNG])) {
208 imagealphablending($thumb, false);
209 imagesavealpha($thumb, true);
210 }
211
212 if (function_exists('imagecopyresampled')) {
213 imagecopyresampled($thumb, $img, 0, 0, 0, 0, $newWidth, $newHeight, $currentwidth, $currentheight);
214 } else {
215 ImageCopyResized($thumb, $img, 0, 0, 0, 0, $newWidth, $newHeight, $currentwidth, $currentheight);
216 }
217
218 ob_start();
219 switch ($imageRealType) {
220 case IMAGETYPE_GIF:
221 $status = imagegif($thumb);
222 break;
223 case IMAGETYPE_JPEG:
224 $status = imagejpeg($thumb, null, 100);
225 break;
226 case IMAGETYPE_PNG:
227 $status = imagepng($thumb, null, 0);
228 break;
229 case IMAGETYPE_WEBP:
230 $status = imagewebp($thumb, null, 100);
231 break;
232 }
233 $imageContent = ob_get_clean();
234
235 $status = $status && acym_writeFile($newFile, $imageContent);
236 imagedestroy($thumb);
237 imagedestroy($img);
238
239 if (!$status) {
240 $newFile = $picturePath;
241 }
242
243 return [
244 'file' => $newFile,
245 'width' => $newWidth,
246 'height' => $newHeight,
247 ];
248 }
249 }
250