PluginProbe
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress / 10.0.1
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress v10.0.1
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.0.1, at back/Helpers/ImageHelper.php

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