XoopsModules25x /
suico
| 1 | <?php declare(strict_types=1); |
||
| 2 | |||
| 3 | namespace XoopsModules\Yogurt\Common; |
||
| 4 | |||
| 5 | /* |
||
| 6 | You may not change or alter any portion of this comment or credits |
||
| 7 | of supporting developers from this source code or any supporting source code |
||
| 8 | which is considered copyrighted (c) material of the original comment or credit authors. |
||
| 9 | |||
| 10 | This program is distributed in the hope that it will be useful, |
||
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
||
| 13 | */ |
||
| 14 | |||
| 15 | /** |
||
| 16 | * Image resizer class for xoops |
||
| 17 | * |
||
| 18 | * @copyright 2000-2020 XOOPS Project (https://xoops.org) |
||
| 19 | * @license GPL 2.0 or later |
||
| 20 | * @package XOOPS common |
||
| 21 | * @since 1.0 |
||
| 22 | * @min_xoops 2.5.9 |
||
| 23 | * @author Goffy - Wedega - Email:<[email protected]> - Website:<https://wedega.com> |
||
| 24 | */ |
||
| 25 | class Resizer |
||
| 26 | { |
||
| 27 | public $sourceFile = ''; |
||
| 28 | public $endFile = ''; |
||
| 29 | public $maxWidth = 0; |
||
| 30 | public $maxHeight = 0; |
||
| 31 | public $imageMimetype = ''; |
||
| 32 | public $jpgQuality = 90; |
||
| 33 | public $mergeType = 0; |
||
| 34 | public $mergePos = 0; |
||
| 35 | public $degrees = 0; |
||
| 36 | public $error = ''; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * resize image if size exceed given width/height |
||
| 40 | * @return string|bool |
||
| 41 | */ |
||
| 42 | public function resizeImage() |
||
| 43 | { |
||
| 44 | // check file extension |
||
| 45 | switch ($this->imageMimetype) { |
||
| 46 | case 'image/png': |
||
| 47 | $img = \imagecreatefrompng($this->sourceFile); |
||
| 48 | break; |
||
| 49 | case 'image/jpeg': |
||
| 50 | $img = \imagecreatefromjpeg($this->sourceFile); |
||
| 51 | if (!$img) { |
||
| 52 | $img = \imagecreatefromstring(file_get_contents($this->sourceFile)); |
||
| 53 | } |
||
| 54 | break; |
||
| 55 | case 'image/gif': |
||
| 56 | $img = \imagecreatefromgif($this->sourceFile); |
||
| 57 | break; |
||
| 58 | default: |
||
| 59 | return 'Unsupported format'; |
||
| 60 | } |
||
| 61 | |||
| 62 | $width = \imagesx($img); |
||
| 63 | $height = \imagesy($img); |
||
| 64 | |||
| 65 | if ($width > $this->maxWidth || $height > $this->maxHeight) { |
||
| 66 | // recalc image size based on this->maxWidth/this->maxHeight |
||
| 67 | if ($width > $height) { |
||
| 68 | if ($width < $this->maxWidth) { |
||
| 69 | $new_width = $width; |
||
| 70 | } else { |
||
| 71 | $new_width = $this->maxWidth; |
||
| 72 | $divisor = $width / $new_width; |
||
| 73 | $newHeight = (int)\floor($height / $divisor); |
||
| 74 | } |
||
| 75 | } elseif ($height < $this->maxHeight) { |
||
| 76 | $newHeight = (int)$height; |
||
| 77 | } else { |
||
| 78 | $newHeight = $this->maxHeight; |
||
| 79 | $divisor = $height / $newHeight; |
||
| 80 | $new_width = (int)\floor($width / $divisor); |
||
| 81 | } |
||
| 82 | |||
| 83 | // Create a new temporary image. |
||
| 84 | $tmpimg = \imagecreatetruecolor($new_width, $newHeight); |
||
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Loading history...
|
|||
| 85 | imagealphablending($tmpimg, false); |
||
| 86 | imagesavealpha($tmpimg, true); |
||
| 87 | |||
| 88 | // Copy and resize old image into new image. |
||
| 89 | \imagecopyresampled( |
||
| 90 | $tmpimg, |
||
| 91 | $img, |
||
| 92 | 0, |
||
| 93 | 0, |
||
| 94 | 0, |
||
| 95 | 0, |
||
| 96 | $new_width, |
||
| 97 | $newHeight, |
||
| 98 | $width, |
||
| 99 | $height |
||
| 100 | ); |
||
| 101 | |||
| 102 | \unlink($this->endFile); |
||
| 103 | //compressing the file |
||
| 104 | switch ($this->imageMimetype) { |
||
| 105 | case 'image/png': |
||
| 106 | \imagepng($tmpimg, $this->endFile, 0); |
||
| 107 | break; |
||
| 108 | case 'image/jpeg': |
||
| 109 | \imagejpeg($tmpimg, $this->endFile, 100); |
||
| 110 | break; |
||
| 111 | case 'image/gif': |
||
| 112 | \imagegif($tmpimg, $this->endFile); |
||
| 113 | break; |
||
| 114 | } |
||
| 115 | |||
| 116 | // release the memory |
||
| 117 | \imagedestroy($tmpimg); |
||
| 118 | } else { |
||
| 119 | return 'copy'; |
||
| 120 | } |
||
| 121 | \imagedestroy($img); |
||
| 122 | |||
| 123 | return true; |
||
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @return bool|string |
||
| 128 | */ |
||
| 129 | public function resizeAndCrop() |
||
| 130 | { |
||
| 131 | // check file extension |
||
| 132 | switch ($this->imageMimetype) { |
||
| 133 | case 'image/png': |
||
| 134 | $original = \imagecreatefrompng($this->sourceFile); |
||
| 135 | break; |
||
| 136 | case 'image/jpeg': |
||
| 137 | $original = \imagecreatefromjpeg($this->sourceFile); |
||
| 138 | break; |
||
| 139 | case 'image/gif': |
||
| 140 | $original = \imagecreatefromgif($this->sourceFile); |
||
| 141 | break; |
||
| 142 | default: |
||
| 143 | return 'Unsupported format'; |
||
| 144 | } |
||
| 145 | |||
| 146 | if (!$original) { |
||
| 147 | return false; |
||
| 148 | } |
||
| 149 | // GET ORIGINAL IMAGE DIMENSIONS |
||
| 150 | [$original_w, $original_h] = \getimagesize($this->sourceFile); |
||
| 151 | |||
| 152 | // RESIZE IMAGE AND PRESERVE PROPORTIONS |
||
| 153 | $max_width_resize = $this->maxWidth; |
||
| 154 | $max_height_resize = $this->maxHeight; |
||
| 155 | if ($original_w > $original_h) { |
||
| 156 | $max_height_ratio = $this->maxHeight / $original_h; |
||
| 157 | $max_width_resize = (int)\round($original_w * $max_height_ratio); |
||
| 158 | } else { |
||
| 159 | $max_width_ratio = $this->maxWidth / $original_w; |
||
| 160 | $max_height_resize = (int)\round($original_h * $max_width_ratio); |
||
| 161 | } |
||
| 162 | if ($max_width_resize < $this->maxWidth) { |
||
| 163 | $max_height_ratio = $this->maxWidth / $max_width_resize; |
||
| 164 | $max_height_resize = (int)\round($this->maxHeight * $max_height_ratio); |
||
| 165 | $max_width_resize = $this->maxWidth; |
||
| 166 | } |
||
| 167 | |||
| 168 | // CREATE THE PROPORTIONAL IMAGE RESOURCE |
||
| 169 | $thumb = \imagecreatetruecolor($max_width_resize, $max_height_resize); |
||
| 170 | if (!\imagecopyresampled( |
||
| 171 | $thumb, |
||
| 172 | $original, |
||
| 173 | 0, |
||
| 174 | 0, |
||
| 175 | 0, |
||
| 176 | 0, |
||
| 177 | $max_width_resize, |
||
| 178 | $max_height_resize, |
||
| 179 | $original_w, |
||
| 180 | $original_h |
||
| 181 | )) { |
||
| 182 | return false; |
||
| 183 | } |
||
| 184 | // CREATE THE CENTERED CROPPED IMAGE TO THE SPECIFIED DIMENSIONS |
||
| 185 | $final = \imagecreatetruecolor( |
||
| 186 | $this->maxWidth, |
||
| 187 | $this->maxHeight |
||
| 188 | ); |
||
| 189 | |||
| 190 | $max_width_offset = 0; |
||
| 191 | $max_height_offset = 0; |
||
| 192 | if ($this->maxWidth < $max_width_resize) { |
||
| 193 | $max_width_offset = (int)\round(($max_width_resize - $this->maxWidth) / 2); |
||
| 194 | } else { |
||
| 195 | $max_height_offset = (int)\round(($max_height_resize - $this->maxHeight) / 2); |
||
| 196 | } |
||
| 197 | |||
| 198 | if (!\imagecopy( |
||
| 199 | $final, |
||
| 200 | $thumb, |
||
| 201 | 0, |
||
| 202 | 0, |
||
| 203 | $max_width_offset, |
||
| 204 | $max_height_offset, |
||
| 205 | $max_width_resize, |
||
| 206 | $max_height_resize |
||
| 207 | )) { |
||
| 208 | return false; |
||
| 209 | } |
||
| 210 | // STORE THE FINAL IMAGE - WILL OVERWRITE $this->endFile |
||
| 211 | if (!\imagejpeg( |
||
| 212 | $final, |
||
| 213 | $this->endFile, |
||
| 214 | $this->jpgQuality |
||
| 215 | )) { |
||
| 216 | return false; |
||
| 217 | } |
||
| 218 | |||
| 219 | return true; |
||
| 220 | } |
||
| 221 | |||
| 222 | public function mergeImage() |
||
| 223 | { |
||
| 224 | $dest = \imagecreatefromjpeg($this->endFile); |
||
| 225 | $src = \imagecreatefromjpeg($this->sourceFile); |
||
| 226 | if (4 === $this->mergeType) { |
||
| 227 | $imgWidth = (int)\round($this->maxWidth / 2 - 1); |
||
| 228 | $imgHeight = (int)\round($this->maxHeight / 2 - 1); |
||
| 229 | $posCol2 = (int)\round($this->maxWidth / 2 + 1); |
||
| 230 | $posRow2 = (int)\round($this->maxHeight / 2 + 1); |
||
| 231 | switch ($this->mergePos) { |
||
| 232 | case 1: |
||
| 233 | \imagecopy($dest, $src, 0, 0, 0, 0, $imgWidth, $imgHeight); //top left |
||
| 234 | break; |
||
| 235 | case 2: |
||
| 236 | \imagecopy($dest, $src, $posCol2, 0, 0, 0, $imgWidth, $imgHeight); //top right |
||
| 237 | break; |
||
| 238 | case 3: |
||
| 239 | \imagecopy($dest, $src, 0, $posRow2, 0, 0, $imgWidth, $imgHeight); //bottom left |
||
| 240 | break; |
||
| 241 | case 4: |
||
| 242 | \imagecopy($dest, $src, $posCol2, $posRow2, 0, 0, $imgWidth, $imgHeight); //bottom right |
||
| 243 | break; |
||
| 244 | } |
||
| 245 | } |
||
| 246 | if (6 === $this->mergeType) { |
||
| 247 | $imgWidth = (int)\round($this->maxWidth / 3 - 1); |
||
| 248 | $imgHeight = (int)\round($this->maxHeight / 2 - 1); |
||
| 249 | $posCol2 = (int)\round($this->maxWidth / 3 + 1); |
||
| 250 | $posCol3 = $posCol2 + (int)\round($this->maxWidth / 3 + 1); |
||
| 251 | $posRow2 = (int)\round($this->maxHeight / 2 + 1); |
||
| 252 | |||
| 253 | switch ($this->mergePos) { |
||
| 254 | case 1: |
||
| 255 | \imagecopy($dest, $src, 0, 0, 0, 0, $imgWidth, $imgHeight); //top left |
||
| 256 | break; |
||
| 257 | case 2: |
||
| 258 | \imagecopy($dest, $src, $posCol2, 0, 0, 0, $imgWidth, $imgHeight); //top center |
||
| 259 | break; |
||
| 260 | case 3: |
||
| 261 | \imagecopy($dest, $src, $posCol3, 0, 0, 0, $imgWidth, $imgHeight); //top right |
||
| 262 | break; |
||
| 263 | case 4: |
||
| 264 | \imagecopy($dest, $src, 0, $posRow2, 0, 0, $imgWidth, $imgHeight); //bottom left |
||
| 265 | break; |
||
| 266 | case 5: |
||
| 267 | \imagecopy($dest, $src, $posCol2, $posRow2, 0, 0, $imgWidth, $imgHeight); //bottom center |
||
| 268 | break; |
||
| 269 | case 6: |
||
| 270 | \imagecopy($dest, $src, $posCol3, $posRow2, 0, 0, $imgWidth, $imgHeight); //bottom right |
||
| 271 | break; |
||
| 272 | } |
||
| 273 | } |
||
| 274 | \imagejpeg($dest, $this->endFile); |
||
| 275 | |||
| 276 | \imagedestroy($src); |
||
| 277 | \imagedestroy($dest); |
||
| 278 | } |
||
| 279 | |||
| 280 | /** |
||
| 281 | * @return bool|string |
||
| 282 | */ |
||
| 283 | public function rotateImage() |
||
| 284 | { |
||
| 285 | // check file extension |
||
| 286 | switch ($this->imageMimetype) { |
||
| 287 | case 'image/png': |
||
| 288 | $original = \imagecreatefrompng($this->sourceFile); |
||
| 289 | break; |
||
| 290 | case 'image/jpeg': |
||
| 291 | $original = \imagecreatefromjpeg($this->sourceFile); |
||
| 292 | break; |
||
| 293 | case 'image/gif': |
||
| 294 | $original = \imagecreatefromgif($this->sourceFile); |
||
| 295 | break; |
||
| 296 | default: |
||
| 297 | return 'Unsupported format'; |
||
| 298 | } |
||
| 299 | |||
| 300 | if (!$original) { |
||
| 301 | return false; |
||
| 302 | } |
||
| 303 | // Rotate |
||
| 304 | $tmpimg = \imagerotate($original, $this->degrees, 0); |
||
| 305 | |||
| 306 | \unlink($this->endFile); |
||
| 307 | //compressing the file |
||
| 308 | switch ($this->imageMimetype) { |
||
| 309 | case 'image/png': |
||
| 310 | if (!\imagepng($tmpimg, $this->endFile, 0)) { |
||
| 311 | return false; |
||
| 312 | } |
||
| 313 | break; |
||
| 314 | case 'image/jpeg': |
||
| 315 | if (!\imagejpeg($tmpimg, $this->endFile, $this->jpgQuality)) { |
||
| 316 | return false; |
||
| 317 | } |
||
| 318 | break; |
||
| 319 | case 'image/gif': |
||
| 320 | if (!\imagegif($tmpimg, $this->endFile)) { |
||
| 321 | return false; |
||
| 322 | } |
||
| 323 | break; |
||
| 324 | } |
||
| 325 | |||
| 326 | // release the memory |
||
| 327 | \imagedestroy($tmpimg); |
||
| 328 | |||
| 329 | return true; |
||
| 330 | } |
||
| 331 | } |
||
| 332 |