| @@ -1,0 +1,271 @@ | ||
| 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 | + // New Height the picture should be | |
| 11 | + public int $maxHeight; | |
| 12 | + // New Width the picture should be | |
| 13 | + public int $maxWidth; | |
| 14 | + // Folder where pictures should be stored | |
| 15 | + public string $destination; | |
| 16 | + | |
| 17 | + public function removePictures(string $text): string | |
| 18 | + { | |
| 19 | + $return = preg_replace('#< *img((?!content_main_image)[^>])*>#Ui', '', $text); | |
| 20 | + | |
| 21 | + // Clean JCE caption | |
| 22 | + return preg_replace('#< *div[^>]*class="jce_caption"[^>]*>[^<]*(< *div[^>]*>[^<]*<\/div>)*[^<]*<\/div>#Ui', '', $return); | |
| 23 | + } | |
| 24 | + | |
| 25 | + public function available(): bool | |
| 26 | + { | |
| 27 | + if (!function_exists('gd_info')) { | |
| 28 | + $this->error = 'The GD library is not installed.'; | |
| 29 | + | |
| 30 | + return false; | |
| 31 | + } | |
| 32 | + if (!function_exists('getimagesize')) { | |
| 33 | + $this->error = 'Cound not find getimagesize function'; | |
| 34 | + | |
| 35 | + return false; | |
| 36 | + } | |
| 37 | + if (!function_exists('imagealphablending')) { | |
| 38 | + $this->error = "Please make sure you're using GD 2.0.1 or later version"; | |
| 39 | + | |
| 40 | + return false; | |
| 41 | + } | |
| 42 | + | |
| 43 | + return true; | |
| 44 | + } | |
| 45 | + | |
| 46 | + //This function will check all images from the input and return an output with the pictures transformed with the right size | |
| 47 | + public function resizePictures(string $input): string | |
| 48 | + { | |
| 49 | + $this->destination = ACYM_MEDIA.'resized'.DS; | |
| 50 | + acym_createDir($this->destination); | |
| 51 | + $content = acym_absoluteURL($input); | |
| 52 | + | |
| 53 | + //regex take all images: | |
| 54 | + preg_match_all('#<img([^>]*)>#Ui', $content, $results); | |
| 55 | + if (empty($results[1])) { | |
| 56 | + return $input; | |
| 57 | + } | |
| 58 | + | |
| 59 | + $replace = []; | |
| 60 | + | |
| 61 | + foreach ($results[1] as $onepicture) { | |
| 62 | + //Check if there the "donotresize" element inside, we don't resize it. | |
| 63 | + //It can be either in the class or in the filename itself | |
| 64 | + if (strpos($onepicture, 'donotresize') !== false) { | |
| 65 | + continue; | |
| 66 | + } | |
| 67 | + | |
| 68 | + //Take the path | |
| 69 | + if (!preg_match('#src="([^"]*)"#Ui', $onepicture, $path)) { | |
| 70 | + continue; | |
| 71 | + } | |
| 72 | + $imageUrl = $path[1]; | |
| 73 | + | |
| 74 | + //images paths on joomla 4 have some content like : #joomlaImage... , we delete it to get and resize the image without errors | |
| 75 | + $imageUrl = preg_replace('/#joomlaImage.*/', '', $imageUrl); | |
| 76 | + | |
| 77 | + //We are nice guys... sometimes users use www. or not... so we convert both, same thing for httpS or http | |
| 78 | + $imageUrl = acym_internalUrlToPath($imageUrl); | |
| 79 | + | |
| 80 | + $newPicture = $this->generateThumbnail($imageUrl); | |
| 81 | + | |
| 82 | + $newDimension = 'max-width:'.$this->maxWidth.'px;max-height:'.$this->maxHeight.'px;'; | |
| 83 | + | |
| 84 | + //Maybe we don't need to resize anything... | |
| 85 | + if (empty($newPicture)) { | |
| 86 | + if (strpos($onepicture, 'style="') !== false) { | |
| 87 | + $replace[$onepicture] = preg_replace('#style="([^"]*)"#Uis', 'style="'.$newDimension.'$1"', $onepicture); | |
| 88 | + } else { | |
| 89 | + $replace[$onepicture] = ' style="'.$newDimension.'" '.$onepicture; | |
| 90 | + } | |
| 91 | + continue; | |
| 92 | + } | |
| 93 | + | |
| 94 | + //Because the ACYM_ROOT may be empty, we only make sure to replace the first instance found. | |
| 95 | + $newPicture['file'] = preg_replace('#^'.preg_quote(ACYM_ROOT, '#').'#i', ACYM_LIVE, $newPicture['file']); | |
| 96 | + $newPicture['file'] = str_replace(DS, '/', $newPicture['file']); | |
| 97 | + //replace the image url now with the new one... | |
| 98 | + $replaceImage = []; | |
| 99 | + $replaceImage[$path[1]] = $newPicture['file']; | |
| 100 | + //replace information if we had some (height,width...) | |
| 101 | + if (preg_match_all('#(width|height)(:|=) *"?([0-9]+)#i', $onepicture, $resultsSize)) { | |
| 102 | + foreach ($resultsSize[0] as $i => $oneArg) { | |
| 103 | + $newVal = (strtolower($resultsSize[1][$i]) == 'width') ? $newPicture['width'] : $newPicture['height']; | |
| 104 | + if ($newVal > $resultsSize[3][$i]) { | |
| 105 | + continue; | |
| 106 | + } | |
| 107 | + $replaceImage[$oneArg] = str_replace($resultsSize[3][$i], $newVal, $oneArg); | |
| 108 | + } | |
| 109 | + } | |
| 110 | + | |
| 111 | + $replace[$onepicture] = str_replace(array_keys($replaceImage), $replaceImage, $onepicture); | |
| 112 | + | |
| 113 | + // Make sure that we resized the image | |
| 114 | + if (strpos($replace[$onepicture], 'width') === false) { | |
| 115 | + if (strpos($onepicture, 'style="') !== false) { | |
| 116 | + $replace[$onepicture] = preg_replace('#style="([^"]*)"#Uis', 'style="'.$newDimension.'$1"', $replace[$onepicture]); | |
| 117 | + } else { | |
| 118 | + $replace[$onepicture] = ' style="'.$newDimension.'" '.$replace[$onepicture]; | |
| 119 | + } | |
| 120 | + } | |
| 121 | + } | |
| 122 | + | |
| 123 | + if (!empty($replace)) { | |
| 124 | + $input = str_replace(array_keys($replace), $replace, $content); | |
| 125 | + } | |
| 126 | + | |
| 127 | + return $input; | |
| 128 | + } | |
| 129 | + | |
| 130 | + public function generateThumbnail(string $picturePath): array | |
| 131 | + { | |
| 132 | + $paramsPos = strpos($picturePath, '?'); | |
| 133 | + if ($paramsPos !== false) { | |
| 134 | + $picturePath = substr($picturePath, 0, $paramsPos); | |
| 135 | + } | |
| 136 | + | |
| 137 | + [$currentwidth, $currentheight] = @getimagesize($picturePath); | |
| 138 | + if (empty($currentwidth) || empty($currentheight)) { | |
| 139 | + return []; | |
| 140 | + } | |
| 141 | + $factor = min($this->maxWidth / $currentwidth, $this->maxHeight / $currentheight); | |
| 142 | + if ($factor >= 1) { | |
| 143 | + return []; | |
| 144 | + } | |
| 145 | + $newWidth = round($currentwidth * $factor); | |
| 146 | + $newHeight = round($currentheight * $factor); | |
| 147 | + | |
| 148 | + if (strpos($picturePath, 'http') === 0) { | |
| 149 | + $filename = substr($picturePath, strrpos($picturePath, '/') + 1); | |
| 150 | + } else { | |
| 151 | + $filename = basename($picturePath); | |
| 152 | + } | |
| 153 | + | |
| 154 | + if (substr($picturePath, 0, 10) == 'data:image') { | |
| 155 | + //It's a picture in base64 encoding... we will apply an extension name and name based on the content... | |
| 156 | + preg_match('#data:image/([^;]{1,5});#', $picturePath, $resultextension); | |
| 157 | + if (empty($resultextension[1])) { | |
| 158 | + return []; | |
| 159 | + } | |
| 160 | + $extension = $resultextension[1]; | |
| 161 | + $name = md5($picturePath); | |
| 162 | + } else { | |
| 163 | + $extension = strtolower(substr($filename, strrpos($filename, '.') + 1)); | |
| 164 | + $name = strtolower(substr($filename, 0, strrpos($filename, '.'))); | |
| 165 | + //We add the creation date of the file so if the file is modified, we will generate a new thumbnail | |
| 166 | + //No need to use the full date, the last 4 characters will be enough! | |
| 167 | + $name .= substr(@filemtime($picturePath), -4); | |
| 168 | + } | |
| 169 | + | |
| 170 | + $newImage = md5($picturePath).'-'.$name.'thumb'.$this->maxWidth.'x'.$this->maxHeight.'.'.$extension; | |
| 171 | + if (empty($this->destination)) { | |
| 172 | + $newFile = dirname($picturePath).DS.$newImage; | |
| 173 | + } else { | |
| 174 | + $newFile = $this->destination.$newImage; | |
| 175 | + } | |
| 176 | + | |
| 177 | + //The new file already exists, we don't have to create a new one | |
| 178 | + if (file_exists($newFile)) { | |
| 179 | + return [ | |
| 180 | + 'file' => $newFile, | |
| 181 | + 'width' => $newWidth, | |
| 182 | + 'height' => $newHeight, | |
| 183 | + ]; | |
| 184 | + } | |
| 185 | + | |
| 186 | + // Checks the real file type as it can be renamed, but not available on all servers | |
| 187 | + if (function_exists('exif_imagetype')) { | |
| 188 | + $imageRealType = exif_imagetype($picturePath); | |
| 189 | + } else { | |
| 190 | + if ($extension === 'gif') { | |
| 191 | + $imageRealType = IMAGETYPE_GIF; | |
| 192 | + } elseif ($extension === 'jpg' || $extension === 'jpeg') { | |
| 193 | + $imageRealType = IMAGETYPE_JPEG; | |
| 194 | + } elseif ($extension === 'png') { | |
| 195 | + $imageRealType = IMAGETYPE_PNG; | |
| 196 | + } elseif ($extension === 'webp') { | |
| 197 | + $imageRealType = IMAGETYPE_WEBP; | |
| 198 | + } else { | |
| 199 | + return []; | |
| 200 | + } | |
| 201 | + } | |
| 202 | + | |
| 203 | + switch ($imageRealType) { | |
| 204 | + case IMAGETYPE_GIF: | |
| 205 | + $img = imagecreatefromgif($picturePath); | |
| 206 | + break; | |
| 207 | + case IMAGETYPE_JPEG: | |
| 208 | + $img = imagecreatefromjpeg($picturePath); | |
| 209 | + break; | |
| 210 | + case IMAGETYPE_PNG: | |
| 211 | + $img = imagecreatefrompng($picturePath); | |
| 212 | + break; | |
| 213 | + case IMAGETYPE_WEBP: | |
| 214 | + if (function_exists('imagecreatefromwebp')) { | |
| 215 | + $img = imagecreatefromwebp($picturePath); | |
| 216 | + } | |
| 217 | + break; | |
| 218 | + default: | |
| 219 | + return []; | |
| 220 | + } | |
| 221 | + | |
| 222 | + if (empty($img)) { | |
| 223 | + return []; | |
| 224 | + } | |
| 225 | + | |
| 226 | + $thumb = imagecreatetruecolor($newWidth, $newHeight); | |
| 227 | + | |
| 228 | + if (in_array($imageRealType, [IMAGETYPE_GIF, IMAGETYPE_PNG])) { | |
| 229 | + imagealphablending($thumb, false); | |
| 230 | + imagesavealpha($thumb, true); | |
| 231 | + } | |
| 232 | + | |
| 233 | + if (function_exists('imagecopyresampled')) { | |
| 234 | + imagecopyresampled($thumb, $img, 0, 0, 0, 0, $newWidth, $newHeight, $currentwidth, $currentheight); | |
| 235 | + } else { | |
| 236 | + ImageCopyResized($thumb, $img, 0, 0, 0, 0, $newWidth, $newHeight, $currentwidth, $currentheight); | |
| 237 | + } | |
| 238 | + | |
| 239 | + ob_start(); | |
| 240 | + switch ($imageRealType) { | |
| 241 | + case IMAGETYPE_GIF: | |
| 242 | + $status = imagegif($thumb); | |
| 243 | + break; | |
| 244 | + case IMAGETYPE_JPEG: | |
| 245 | + $status = imagejpeg($thumb, null, 100); | |
| 246 | + break; | |
| 247 | + case IMAGETYPE_PNG: | |
| 248 | + $status = imagepng($thumb, null, 0); | |
| 249 | + break; | |
| 250 | + case IMAGETYPE_WEBP: | |
| 251 | + $status = imagewebp($thumb, null, 100); | |
| 252 | + break; | |
| 253 | + } | |
| 254 | + $imageContent = ob_get_clean(); | |
| 255 | + | |
| 256 | + $status = $status && acym_writeFile($newFile, $imageContent); | |
| 257 | + imagedestroy($thumb); | |
| 258 | + imagedestroy($img); | |
| 259 | + | |
| 260 | + //We could not create the picture... so let's resize it anyway | |
| 261 | + if (!$status) { | |
| 262 | + $newFile = $picturePath; | |
| 263 | + } | |
| 264 | + | |
| 265 | + return [ | |
| 266 | + 'file' => $newFile, | |
| 267 | + 'width' => $newWidth, | |
| 268 | + 'height' => $newHeight, | |
| 269 | + ]; | |
| 270 | + } | |
| 271 | +} | |