| Total Complexity | 85 |
| Total Lines | 811 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like GdDriver often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use GdDriver, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 29 | class GdDriver extends AbstractDriver |
||
| 30 | {
|
||
| 31 | /** |
||
| 32 | * GdDriver::__destruct |
||
| 33 | */ |
||
| 34 | public function __destruct() |
||
| 35 | {
|
||
| 36 | if (is_resource($this->sourceImageResource)) {
|
||
| 37 | @imagedestroy($this->sourceImageResource); |
||
|
|
|||
| 38 | } |
||
| 39 | |||
| 40 | if (is_resource($this->resampleImageResource)) {
|
||
| 41 | @imagedestroy($this->resampleImageResource); |
||
| 42 | } |
||
| 43 | } |
||
| 44 | |||
| 45 | // ------------------------------------------------------------------------ |
||
| 46 | |||
| 47 | /** |
||
| 48 | * GdDriver::createFromString |
||
| 49 | * |
||
| 50 | * Create image resource from image string. |
||
| 51 | * |
||
| 52 | * @param string $imageString Image string. |
||
| 53 | * |
||
| 54 | * @return void |
||
| 55 | */ |
||
| 56 | public function createFromString($imageString) |
||
| 57 | {
|
||
| 58 | $this->sourceImageResource = imagecreatefromstring($imageString); |
||
| 59 | } |
||
| 60 | |||
| 61 | // ------------------------------------------------------------------------ |
||
| 62 | |||
| 63 | /** |
||
| 64 | * GdDriver::rotate |
||
| 65 | * |
||
| 66 | * Rotate an image with a given angle. |
||
| 67 | * |
||
| 68 | * @param float $degrees Image rotation degrees. |
||
| 69 | * |
||
| 70 | * @return void |
||
| 71 | */ |
||
| 72 | public function rotate($degrees) |
||
| 73 | {
|
||
| 74 | $resampleImageResource =& $this->getResampleImageResource(); |
||
| 75 | |||
| 76 | // Set the background color |
||
| 77 | // This won't work with transparent PNG files so we are |
||
| 78 | // going to have to figure out how to determine the color |
||
| 79 | // of the alpha channel in a future release. |
||
| 80 | $alphaChannel = imagecolorallocate($resampleImageResource, 255, 255, 255); |
||
| 81 | |||
| 82 | // Rotate it! |
||
| 83 | $this->resampleImageResource = imagerotate($resampleImageResource, $degrees, $alphaChannel); |
||
| 84 | } |
||
| 85 | |||
| 86 | // ------------------------------------------------------------------------ |
||
| 87 | |||
| 88 | /** |
||
| 89 | * GdDriver::flip |
||
| 90 | * |
||
| 91 | * Flip an image with a given axis. |
||
| 92 | * |
||
| 93 | * @param int $axis Flip axis. |
||
| 94 | * |
||
| 95 | * @return void |
||
| 96 | */ |
||
| 97 | public function flip($axis) |
||
| 98 | {
|
||
| 99 | $gdAxis = [ |
||
| 100 | 1 => IMG_FLIP_HORIZONTAL, |
||
| 101 | 2 => IMG_FLIP_VERTICAL, |
||
| 102 | 3 => IMG_FLIP_BOTH, |
||
| 103 | ]; |
||
| 104 | |||
| 105 | if (array_key_exists($axis, $gdAxis)) {
|
||
| 106 | $resampleImageResource =& $this->getResampleImageResource(); |
||
| 107 | imageflip($resampleImageResource, $axis); |
||
| 108 | } |
||
| 109 | } |
||
| 110 | |||
| 111 | // ------------------------------------------------------------------------ |
||
| 112 | |||
| 113 | /** |
||
| 114 | * GdDriver::resize |
||
| 115 | * |
||
| 116 | * Resize an image using the given new width and height. |
||
| 117 | * |
||
| 118 | * @param bool $crop Perform auto crop or not |
||
| 119 | * |
||
| 120 | * @return bool |
||
| 121 | */ |
||
| 122 | public function resize($crop = false) |
||
| 123 | {
|
||
| 124 | if ($crop) {
|
||
| 125 | return $this->resizeCrop(); |
||
| 126 | } else {
|
||
| 127 | $sourceDimension = $this->sourceImageFile->getDimension(); |
||
| 128 | $resampleDimension = $this->resampleImageFile->getDimension(); |
||
| 129 | |||
| 130 | if (($sourceDimension->getWidth() <= $resampleDimension->getWidth()) && ($sourceDimension->getHeight() <= $resampleDimension->getHeight())) {
|
||
| 131 | return true; |
||
| 132 | } //no resizing needed |
||
| 133 | |||
| 134 | //try max width first... |
||
| 135 | $resizeRatio = $resampleDimension->getWidth() / $sourceDimension->getWidth(); |
||
| 136 | $resizeWidth = $resampleDimension->getWidth(); |
||
| 137 | $resizeHeight = $sourceDimension->getHeight() * $resizeRatio; |
||
| 138 | |||
| 139 | //if that didn't work |
||
| 140 | if ($resizeHeight > $resampleDimension->getHeight()) {
|
||
| 141 | $resizeRatio = $resampleDimension->getHeight() / $sourceDimension->getHeight(); |
||
| 142 | $resizeHeight = $resampleDimension->getHeight(); |
||
| 143 | $resizeWidth = $sourceDimension->getWidth() * $resizeRatio; |
||
| 144 | } |
||
| 145 | |||
| 146 | if (function_exists('imagecreatetruecolor')) {
|
||
| 147 | $this->resampleImageResource = imagecreatetruecolor($resizeWidth, $resizeHeight); |
||
| 148 | |||
| 149 | imagealphablending($this->resampleImageResource, false); |
||
| 150 | imagesavealpha($this->resampleImageResource, true); |
||
| 151 | |||
| 152 | $transparent = imagecolorallocatealpha($this->resampleImageResource, 255, 255, 255, 127); |
||
| 153 | imagefilledrectangle($this->resampleImageResource, 0, 0, $resampleDimension->getWidth(), |
||
| 154 | $resampleDimension->getHeight(), $transparent); |
||
| 155 | |||
| 156 | return imagecopyresampled( |
||
| 157 | $this->resampleImageResource, |
||
| 158 | $this->sourceImageResource, |
||
| 159 | 0, |
||
| 160 | 0, |
||
| 161 | 0, |
||
| 162 | 0, |
||
| 163 | $resizeWidth, |
||
| 164 | $resizeHeight, |
||
| 165 | $sourceDimension->getWidth(), |
||
| 166 | $sourceDimension->getHeight() |
||
| 167 | ); |
||
| 168 | } else {
|
||
| 169 | $this->resampleImageResource = imagecreate($resampleDimension->getWidth(), |
||
| 170 | $resampleDimension->getHeight()); |
||
| 171 | |||
| 172 | imagealphablending($this->resampleImageResource, false); |
||
| 173 | imagesavealpha($this->resampleImageResource, true); |
||
| 174 | |||
| 175 | $transparent = imagecolorallocatealpha($this->resampleImageResource, 255, 255, 255, 127); |
||
| 176 | imagefilledrectangle($this->resampleImageResource, 0, 0, $resampleDimension->getWidth(), |
||
| 177 | $resampleDimension->getHeight(), $transparent); |
||
| 178 | |||
| 179 | return imagecopyresampled( |
||
| 180 | $this->resampleImageResource, |
||
| 181 | $this->sourceImageResource, |
||
| 182 | 0, |
||
| 183 | 0, |
||
| 184 | 0, |
||
| 185 | 0, |
||
| 186 | $resizeWidth, |
||
| 187 | $resizeHeight, |
||
| 188 | $sourceDimension->getWidth(), |
||
| 189 | $sourceDimension->getHeight() |
||
| 190 | ); |
||
| 191 | } |
||
| 192 | } |
||
| 193 | } |
||
| 194 | |||
| 195 | // ------------------------------------------------------------------------ |
||
| 196 | |||
| 197 | public function resizeCrop() |
||
| 198 | {
|
||
| 199 | $sourceDimension = $this->sourceImageFile->getDimension(); |
||
| 200 | $resampleDimension = $this->resampleImageFile->getDimension(); |
||
| 201 | |||
| 202 | if ($resampleDimension->getOrientation() === 'SQUARE') {
|
||
| 203 | if ($sourceDimension->getOrientation() === 'LANDSCAPE') {
|
||
| 204 | $sourceSquareSize = $sourceDimension->getHeight(); |
||
| 205 | |||
| 206 | $sourceDimension = new Dimension( |
||
| 207 | $sourceSquareSize, |
||
| 208 | $sourceSquareSize, |
||
| 209 | ($sourceDimension->getWidth() - $sourceDimension->getHeight()) / 2, |
||
| 210 | 0 |
||
| 211 | ); |
||
| 212 | } elseif ($sourceDimension->getOrientation() === 'PORTRAIT') {
|
||
| 213 | $sourceSquareSize = $sourceDimension->getWidth(); |
||
| 214 | |||
| 215 | $sourceDimension = new Dimension( |
||
| 216 | $sourceSquareSize, |
||
| 217 | $sourceSquareSize, |
||
| 218 | 0, |
||
| 219 | ($sourceDimension->getHeight() - $sourceDimension->getWidth()) / 2 |
||
| 220 | ); |
||
| 221 | } |
||
| 222 | } else {
|
||
| 223 | |||
| 224 | //try max width first... |
||
| 225 | $resizeRatio = $resampleDimension->getWidth() / $sourceDimension->getWidth(); |
||
| 226 | $resizeWidth = $resampleDimension->getWidth(); |
||
| 227 | $resizeHeight = $sourceDimension->getHeight() * $resizeRatio; |
||
| 228 | |||
| 229 | //if that didn't work |
||
| 230 | if ($resizeHeight > $resampleDimension->getHeight()) {
|
||
| 231 | $resizeRatio = $resampleDimension->getHeight() / $sourceDimension->getHeight(); |
||
| 232 | $resizeHeight = $resampleDimension->getHeight(); |
||
| 233 | $resizeWidth = $sourceDimension->getWidth() * $resizeRatio; |
||
| 234 | } |
||
| 235 | |||
| 236 | switch ($resampleDimension->getFocus()) {
|
||
| 237 | default: |
||
| 238 | case 'CENTER': |
||
| 239 | |||
| 240 | $sourceDimension = new Dimension( |
||
| 241 | $resizeWidth, |
||
| 242 | $resizeHeight, |
||
| 243 | ($sourceDimension->getWidth() / 2) - ($resizeWidth / 2), |
||
| 244 | ($sourceDimension->getHeight() / 2) - ($resizeHeight / 2) |
||
| 245 | ); |
||
| 246 | |||
| 247 | break; |
||
| 248 | case 'NORTH': |
||
| 249 | $sourceDimension = new Dimension( |
||
| 250 | $resizeWidth, |
||
| 251 | $resizeHeight, |
||
| 252 | ($sourceDimension->getWidth() - $resizeWidth) / 2, |
||
| 253 | 0 |
||
| 254 | ); |
||
| 255 | break; |
||
| 256 | case 'NORTHWEST': |
||
| 257 | $sourceDimension = new Dimension( |
||
| 258 | $resizeWidth, |
||
| 259 | $resizeHeight, |
||
| 260 | 0, |
||
| 261 | 0 |
||
| 262 | ); |
||
| 263 | break; |
||
| 264 | case 'NORTHEAST': |
||
| 265 | $sourceDimension = new Dimension( |
||
| 266 | $resizeWidth, |
||
| 267 | $resizeHeight, |
||
| 268 | $sourceDimension->getWidth() - $resizeWidth, |
||
| 269 | 0 |
||
| 270 | ); |
||
| 271 | break; |
||
| 272 | case 'SOUTH': |
||
| 273 | $sourceDimension = new Dimension( |
||
| 274 | $resizeWidth, |
||
| 275 | $resizeHeight, |
||
| 276 | ($sourceDimension->getWidth() - $resizeWidth) / 2, |
||
| 277 | $sourceDimension->getHeight() - $resizeHeight |
||
| 278 | ); |
||
| 279 | break; |
||
| 280 | case 'SOUTHWEST': |
||
| 281 | $sourceDimension = new Dimension( |
||
| 282 | $resizeWidth, |
||
| 283 | $resizeHeight, |
||
| 284 | 0, |
||
| 285 | $sourceDimension->getHeight() - $resizeHeight |
||
| 286 | ); |
||
| 287 | break; |
||
| 288 | case 'SOUTHEAST': |
||
| 289 | $sourceDimension = new Dimension( |
||
| 290 | $resizeWidth, |
||
| 291 | $resizeHeight, |
||
| 292 | $sourceDimension->getWidth() - $resizeWidth, |
||
| 293 | $sourceDimension->getHeight() - $resizeHeight |
||
| 294 | ); |
||
| 295 | break; |
||
| 296 | case 'WEST': |
||
| 297 | $sourceDimension = new Dimension( |
||
| 298 | $resizeWidth, |
||
| 299 | $resizeHeight, |
||
| 300 | 0, |
||
| 301 | ($sourceDimension->getHeight() - $resizeHeight) / 2 |
||
| 302 | ); |
||
| 303 | break; |
||
| 304 | case 'EAST': |
||
| 305 | $sourceDimension = new Dimension( |
||
| 306 | $resizeWidth, |
||
| 307 | $resizeHeight, |
||
| 308 | $sourceDimension->getWidth() - $resizeWidth, |
||
| 309 | ($sourceDimension->getHeight() - $resizeHeight) / 2 |
||
| 310 | ); |
||
| 311 | break; |
||
| 312 | } |
||
| 313 | } |
||
| 314 | |||
| 315 | $resampleAxis = $this->resampleImageFile->getDimension()->getAxis(); |
||
| 316 | $sourceAxis = $sourceDimension->getAxis(); |
||
| 317 | |||
| 318 | if (function_exists('imagecreatetruecolor')) {
|
||
| 319 | $this->resampleImageResource = imagecreatetruecolor( |
||
| 320 | $resampleDimension->getWidth(), |
||
| 321 | $resampleDimension->getHeight() |
||
| 322 | ); |
||
| 323 | |||
| 324 | imagealphablending($this->resampleImageResource, false); |
||
| 325 | imagesavealpha($this->resampleImageResource, true); |
||
| 326 | |||
| 327 | $transparent = imagecolorallocatealpha($this->resampleImageResource, 255, 255, 255, 127); |
||
| 328 | imagefilledrectangle($this->resampleImageResource, 0, 0, $resampleDimension->getWidth(), |
||
| 329 | $resampleDimension->getHeight(), $transparent); |
||
| 330 | |||
| 331 | return imagecopyresampled( |
||
| 332 | $this->resampleImageResource, |
||
| 333 | $this->sourceImageResource, |
||
| 334 | $resampleAxis->getX(), |
||
| 335 | $resampleAxis->getY(), |
||
| 336 | $sourceAxis->getX(), |
||
| 337 | $sourceAxis->getY(), |
||
| 338 | $resampleDimension->getWidth(), |
||
| 339 | $resampleDimension->getHeight(), |
||
| 340 | $sourceDimension->getWidth(), |
||
| 341 | $sourceDimension->getHeight() |
||
| 342 | ); |
||
| 343 | } else {
|
||
| 344 | $this->resampleImageResource = imagecreate($resampleDimension->getWidth(), |
||
| 345 | $resampleDimension->getHeight()); |
||
| 346 | |||
| 347 | imagealphablending($this->resampleImageResource, false); |
||
| 348 | imagesavealpha($this->resampleImageResource, true); |
||
| 349 | |||
| 350 | $transparent = imagecolorallocatealpha($this->resampleImageResource, 255, 255, 255, 127); |
||
| 351 | imagefilledrectangle($this->resampleImageResource, 0, 0, $resampleDimension->getWidth(), |
||
| 352 | $resampleDimension->getHeight(), $transparent); |
||
| 353 | |||
| 354 | return imagecopyresized( |
||
| 355 | $this->resampleImageResource, |
||
| 356 | $this->sourceImageResource, |
||
| 357 | $resampleAxis->getX(), |
||
| 358 | $resampleAxis->getY(), |
||
| 359 | $sourceAxis->getX(), |
||
| 360 | $sourceAxis->getY(), |
||
| 361 | $resampleDimension->getWidth(), |
||
| 362 | $resampleDimension->getHeight(), |
||
| 363 | $sourceDimension->getWidth(), |
||
| 364 | $sourceDimension->getHeight() |
||
| 365 | ); |
||
| 366 | } |
||
| 367 | } |
||
| 368 | |||
| 369 | // ------------------------------------------------------------------------ |
||
| 370 | |||
| 371 | /** |
||
| 372 | * GdDriver::crop |
||
| 373 | * |
||
| 374 | * Crop an image. |
||
| 375 | * |
||
| 376 | * @param \O2System\Image\Dimension $dimension |
||
| 377 | * |
||
| 378 | * @return bool |
||
| 379 | */ |
||
| 380 | public function crop(Dimension $dimension) |
||
| 381 | {
|
||
| 382 | $resampleImageResource =& $this->getResampleImageResource(); |
||
| 383 | |||
| 384 | if (false !== ($resampleCropImage = imagecrop($resampleImageResource, [ |
||
| 385 | 'x' => $dimension->getAxis()->getX(), |
||
| 386 | 'y' => $dimension->getAxis()->getY(), |
||
| 387 | 'width' => $dimension->getWidth(), |
||
| 388 | 'height' => $dimension->getHeight(), |
||
| 389 | ])) |
||
| 390 | ) {
|
||
| 391 | $resampleImageResource = $resampleCropImage; |
||
| 392 | |||
| 393 | return true; |
||
| 394 | } |
||
| 395 | |||
| 396 | return false; |
||
| 397 | } |
||
| 398 | |||
| 399 | /** |
||
| 400 | * GdDriver::watermark |
||
| 401 | * |
||
| 402 | * Watermark an image. |
||
| 403 | * |
||
| 404 | * @param \O2System\Image\Abstracts\AbstractWatermark $watermark |
||
| 405 | * |
||
| 406 | * @return bool |
||
| 407 | */ |
||
| 408 | public function watermark(AbstractWatermark $watermark) |
||
| 409 | {
|
||
| 410 | $resampleImageResource =& $this->getResampleImageResource(); |
||
| 411 | |||
| 412 | if ($watermark instanceof Text) {
|
||
| 413 | $textBox = imagettfbbox($watermark->getFontSize(), $watermark->getAngle(), $watermark->getFontPath(), |
||
| 414 | $watermark->getString()); |
||
| 415 | |||
| 416 | if ($textBox[ 0 ] < 0 and $textBox[ 6 ]) {
|
||
| 417 | $textBox[ 1 ] += $textBox[ 0 ]; |
||
| 418 | $textBox[ 3 ] += $textBox[ 0 ]; |
||
| 419 | $textBox[ 5 ] += $textBox[ 0 ]; |
||
| 420 | $textBox[ 7 ] += $textBox[ 0 ]; |
||
| 421 | } |
||
| 422 | |||
| 423 | $textBox = array_map('abs', $textBox);
|
||
| 424 | |||
| 425 | $watermarkImageWidth = max($textBox[ 0 ], $textBox[ 2 ], $textBox[ 4 ], $textBox[ 6 ]); |
||
| 426 | $watermarkImageHeight = max($textBox[ 1 ], $textBox[ 3 ], $textBox[ 5 ], $textBox[ 7 ]); |
||
| 427 | |||
| 428 | if (false !== ($watermarkAxis = $watermark->getAxis())) {
|
||
| 429 | $watermarkImageAxisX = $watermarkAxis->getX(); |
||
| 430 | $watermarkImageAxisY = $watermarkAxis->getY(); |
||
| 431 | } else {
|
||
| 432 | switch ($watermark->getPosition()) {
|
||
| 433 | default: |
||
| 434 | case 'MIDDLE_MIDDLE': |
||
| 435 | case 'MIDDLE': |
||
| 436 | case 'CENTER': |
||
| 437 | $watermarkImageAxisX = (imagesx($resampleImageResource) - $watermarkImageWidth) / 2; |
||
| 438 | $watermarkImageAxisY = (imagesy($resampleImageResource) - $watermarkImageHeight) / 2; |
||
| 439 | break; |
||
| 440 | |||
| 441 | case 'MIDDLE_LEFT': |
||
| 442 | $watermarkImageAxisX = $watermark->getPadding(); |
||
| 443 | $watermarkImageAxisY = (imagesy($resampleImageResource) - $watermarkImageHeight) / 2; |
||
| 444 | break; |
||
| 445 | |||
| 446 | case 'MIDDLE_RIGHT': |
||
| 447 | $watermarkImageAxisX = imagesx($resampleImageResource) - ($watermarkImageWidth + $watermark->getPadding()); |
||
| 448 | $watermarkImageAxisY = (imagesy($resampleImageResource) - $watermarkImageHeight) / 2; |
||
| 449 | break; |
||
| 450 | |||
| 451 | case 'MIDDLE_TOP': |
||
| 452 | $watermarkImageAxisX = (imagesx($resampleImageResource) - $watermarkImageWidth) / 2; |
||
| 453 | $watermarkImageAxisY = $watermarkImageHeight + $watermark->getPadding(); |
||
| 454 | break; |
||
| 455 | |||
| 456 | case 'MIDDLE_BOTTOM': |
||
| 457 | $watermarkImageAxisX = (imagesx($resampleImageResource) - $watermarkImageWidth) / 2; |
||
| 458 | $watermarkImageAxisY = imagesy($resampleImageResource) - ($watermarkImageHeight + $watermark->getPadding()); |
||
| 459 | break; |
||
| 460 | |||
| 461 | case 'TOP_LEFT': |
||
| 462 | $watermarkImageAxisX = $watermark->getPadding(); |
||
| 463 | $watermarkImageAxisY = $watermarkImageHeight + $watermark->getPadding(); |
||
| 464 | break; |
||
| 465 | |||
| 466 | case 'TOP_RIGHT': |
||
| 467 | $watermarkImageAxisX = imagesx($resampleImageResource) - ($watermarkImageWidth + $watermark->getPadding()); |
||
| 468 | $watermarkImageAxisY = $watermarkImageHeight + $watermark->getPadding(); |
||
| 469 | break; |
||
| 470 | |||
| 471 | case 'BOTTOM_LEFT': |
||
| 472 | $watermarkImageAxisX = $watermark->getPadding(); |
||
| 473 | $watermarkImageAxisY = imagesy($resampleImageResource) - $watermarkImageHeight + $watermark->getPadding(); |
||
| 474 | break; |
||
| 475 | |||
| 476 | case 'BOTTOM_RIGHT': |
||
| 477 | $watermarkImageAxisX = imagesx($resampleImageResource) - ($watermarkImageWidth + $watermark->getPadding()); |
||
| 478 | $watermarkImageAxisY = imagesy($resampleImageResource) - ($watermarkImageHeight + $watermark->getPadding()); |
||
| 479 | break; |
||
| 480 | } |
||
| 481 | } |
||
| 482 | |||
| 483 | /* Set RGB values for text |
||
| 484 | * |
||
| 485 | * First character is #, so we don't really need it. |
||
| 486 | * Get the rest of the string and split it into 2-length |
||
| 487 | * hex values: |
||
| 488 | */ |
||
| 489 | $textColor = str_split(substr($watermark->getFontColor(), 1, 6), 2); |
||
| 490 | $textColor = imagecolorclosest($resampleImageResource, hexdec($textColor[ 0 ]), |
||
| 491 | hexdec($textColor[ 1 ]), |
||
| 492 | hexdec($textColor[ 2 ])); |
||
| 493 | |||
| 494 | imagettftext( |
||
| 495 | $resampleImageResource, |
||
| 496 | $watermark->getFontSize(), |
||
| 497 | $watermark->getAngle(), |
||
| 498 | $watermarkImageAxisX, |
||
| 499 | $watermarkImageAxisY, |
||
| 500 | $textColor, |
||
| 501 | $watermark->getFontPath(), |
||
| 502 | $watermark->getString() |
||
| 503 | ); |
||
| 504 | |||
| 505 | return true; |
||
| 506 | } elseif ($watermark instanceof Overlay) {
|
||
| 507 | |||
| 508 | $watermarkImage = new self; |
||
| 509 | $watermarkImage->setSourceImage($watermark->getImagePath()); |
||
| 510 | $watermarkImage->createFromSource(); |
||
| 511 | |||
| 512 | $watermarkImageFile = $watermarkImage->getSourceImageFile(); |
||
| 513 | $watermarkImageDimension = $watermarkImageFile->getDimension(); |
||
| 514 | $watermarkImageDimension->maintainAspectRatio = true; |
||
| 515 | |||
| 516 | if (false === ($scale = $watermark->getImageScale())) {
|
||
| 517 | $scale = min( |
||
| 518 | round(((imagesx($resampleImageResource) / 2) / $watermarkImageDimension->getWidth()) * 100), |
||
| 519 | round(((imagesy($resampleImageResource) / 2) / $watermarkImageDimension->getHeight()) * 100) |
||
| 520 | ); |
||
| 521 | } |
||
| 522 | |||
| 523 | if ($scale > 0) {
|
||
| 524 | $watermarkImage->setResampleImage($watermarkImageFile->withDimension( |
||
| 525 | $watermarkImageDimension |
||
| 526 | ->withScale($scale) |
||
| 527 | )); |
||
| 528 | } |
||
| 529 | |||
| 530 | if ($watermarkImage->scale()) {
|
||
| 531 | $watermarkImageResource = $watermarkImage->getResampleImageResource(); |
||
| 532 | |||
| 533 | $watermarkImageWidth = imagesx($watermarkImageResource); |
||
| 534 | $watermarkImageHeight = imagesy($watermarkImageResource); |
||
| 535 | |||
| 536 | if (false !== ($watermarkAxis = $watermark->getAxis())) {
|
||
| 537 | $watermarkImageAxisX = $watermarkAxis->getX(); |
||
| 538 | $watermarkImageAxisY = $watermarkAxis->getY(); |
||
| 539 | } else {
|
||
| 540 | switch ($watermark->getPosition()) {
|
||
| 541 | default: |
||
| 542 | case 'MIDDLE_MIDDLE': |
||
| 543 | case 'MIDDLE': |
||
| 544 | case 'CENTER': |
||
| 545 | $watermarkImageAxisX = (imagesx($resampleImageResource) - $watermarkImageWidth) / 2; |
||
| 546 | $watermarkImageAxisY = (imagesy($resampleImageResource) - $watermarkImageHeight) / 2; |
||
| 547 | break; |
||
| 548 | |||
| 549 | case 'MIDDLE_LEFT': |
||
| 550 | $watermarkImageAxisX = $watermark->getPadding(); |
||
| 551 | $watermarkImageAxisY = (imagesy($resampleImageResource) - $watermarkImageHeight) / 2; |
||
| 552 | break; |
||
| 553 | |||
| 554 | case 'MIDDLE_RIGHT': |
||
| 555 | $watermarkImageAxisX = imagesx($resampleImageResource) - ($watermarkImageWidth + $watermark->getPadding()); |
||
| 556 | $watermarkImageAxisY = (imagesy($resampleImageResource) - $watermarkImageHeight) / 2; |
||
| 557 | break; |
||
| 558 | |||
| 559 | case 'MIDDLE_TOP': |
||
| 560 | $watermarkImageAxisX = (imagesx($resampleImageResource) - $watermarkImageWidth) / 2; |
||
| 561 | $watermarkImageAxisY = $watermarkImageHeight + $watermark->getPadding(); |
||
| 562 | break; |
||
| 563 | |||
| 564 | case 'MIDDLE_BOTTOM': |
||
| 565 | $watermarkImageAxisX = (imagesx($resampleImageResource) - $watermarkImageWidth) / 2; |
||
| 566 | $watermarkImageAxisY = imagesy($resampleImageResource) - ($watermarkImageHeight + $watermark->getPadding()); |
||
| 567 | break; |
||
| 568 | |||
| 569 | case 'TOP_LEFT': |
||
| 570 | $watermarkImageAxisX = $watermark->getPadding(); |
||
| 571 | $watermarkImageAxisY = $watermarkImageHeight + $watermark->getPadding(); |
||
| 572 | break; |
||
| 573 | |||
| 574 | case 'TOP_RIGHT': |
||
| 575 | $watermarkImageAxisX = imagesx($resampleImageResource) - ($watermarkImageWidth + $watermark->getPadding()); |
||
| 576 | $watermarkImageAxisY = $watermarkImageHeight + $watermark->getPadding(); |
||
| 577 | break; |
||
| 578 | |||
| 579 | case 'BOTTOM_LEFT': |
||
| 580 | $watermarkImageAxisX = $watermark->getPadding(); |
||
| 581 | $watermarkImageAxisY = imagesy($resampleImageResource) - $watermarkImageHeight + $watermark->getPadding(); |
||
| 582 | break; |
||
| 583 | |||
| 584 | case 'BOTTOM_RIGHT': |
||
| 585 | $watermarkImageAxisX = imagesx($resampleImageResource) - ($watermarkImageWidth + $watermark->getPadding()); |
||
| 586 | $watermarkImageAxisY = imagesy($resampleImageResource) - ($watermarkImageHeight + $watermark->getPadding()); |
||
| 587 | break; |
||
| 588 | } |
||
| 589 | } |
||
| 590 | |||
| 591 | return imagecopy( |
||
| 592 | $resampleImageResource, |
||
| 593 | $watermarkImageResource, |
||
| 594 | $watermarkImageAxisX, |
||
| 595 | $watermarkImageAxisY, |
||
| 596 | 0, |
||
| 597 | 0, |
||
| 598 | imagesx($watermarkImageResource), |
||
| 599 | imagesy($watermarkImageResource) |
||
| 600 | ); |
||
| 601 | } |
||
| 602 | } |
||
| 603 | |||
| 604 | return false; |
||
| 605 | } |
||
| 606 | |||
| 607 | // ------------------------------------------------------------------------ |
||
| 608 | |||
| 609 | /** |
||
| 610 | * GdDriver::createFromSource |
||
| 611 | * |
||
| 612 | * Create an image resource from source file. |
||
| 613 | * |
||
| 614 | * @return void |
||
| 615 | */ |
||
| 616 | public function createFromSource() |
||
| 617 | {
|
||
| 618 | /** |
||
| 619 | * A work-around for some improperly formatted, but |
||
| 620 | * usable JPEGs; known to be produced by Samsung |
||
| 621 | * smartphones' front-facing cameras. |
||
| 622 | * |
||
| 623 | * @see https://bugs.php.net/bug.php?id=72404 |
||
| 624 | */ |
||
| 625 | ini_set('gd.jpeg_ignore_warning', 1);
|
||
| 626 | |||
| 627 | $mime = $this->sourceImageFile->getMime(); |
||
| 628 | $mime = is_array($mime) ? $mime[ 0 ] : $mime; |
||
| 629 | |||
| 630 | try {
|
||
| 631 | switch ($mime) {
|
||
| 632 | case 'image/jpg': |
||
| 633 | case 'image/jpeg': |
||
| 634 | $this->sourceImageResource = imagecreatefromjpeg($this->sourceImageFile->getRealPath()); |
||
| 635 | break; |
||
| 636 | |||
| 637 | case 'image/gif': |
||
| 638 | $this->sourceImageResource = imagecreatefromgif($this->sourceImageFile->getRealPath()); |
||
| 639 | break; |
||
| 640 | |||
| 641 | case 'image/png': |
||
| 642 | case 'image/x-png': |
||
| 643 | $this->sourceImageResource = imagecreatefrompng($this->sourceImageFile->getRealPath()); |
||
| 644 | break; |
||
| 645 | } |
||
| 646 | |||
| 647 | // Convert pallete images to true color images |
||
| 648 | imagepalettetotruecolor($this->sourceImageResource); |
||
| 649 | } catch (\Exception $e) {
|
||
| 650 | |||
| 651 | } |
||
| 652 | } |
||
| 653 | |||
| 654 | // ------------------------------------------------------------------------ |
||
| 655 | |||
| 656 | /** |
||
| 657 | * GdDriver::scale |
||
| 658 | * |
||
| 659 | * Scale an image with a given scale. |
||
| 660 | * |
||
| 661 | * @return bool |
||
| 662 | */ |
||
| 663 | public function scale() |
||
| 664 | {
|
||
| 665 | $sourceDimension = $this->sourceImageFile->getDimension(); |
||
| 666 | $resampleDimension = $this->resampleImageFile->getDimension(); |
||
| 667 | |||
| 668 | $resampleAxis = $this->resampleImageFile->getDimension()->getAxis(); |
||
| 669 | $sourceAxis = $sourceDimension->getAxis(); |
||
| 670 | |||
| 671 | if (function_exists('imagecreatetruecolor')) {
|
||
| 672 | $this->resampleImageResource = imagecreatetruecolor( |
||
| 673 | $resampleDimension->getWidth(), |
||
| 674 | $resampleDimension->getHeight() |
||
| 675 | ); |
||
| 676 | |||
| 677 | imagealphablending($this->resampleImageResource, false); |
||
| 678 | imagesavealpha($this->resampleImageResource, true); |
||
| 679 | |||
| 680 | $transparent = imagecolorallocatealpha($this->resampleImageResource, 255, 255, 255, 127); |
||
| 681 | imagefilledrectangle($this->resampleImageResource, 0, 0, $resampleDimension->getWidth(), |
||
| 682 | $resampleDimension->getHeight(), $transparent); |
||
| 683 | |||
| 684 | return imagecopyresampled( |
||
| 685 | $this->resampleImageResource, |
||
| 686 | $this->sourceImageResource, |
||
| 687 | $resampleAxis->getX(), |
||
| 688 | $resampleAxis->getY(), |
||
| 689 | $sourceAxis->getX(), |
||
| 690 | $sourceAxis->getY(), |
||
| 691 | $resampleDimension->getWidth(), |
||
| 692 | $resampleDimension->getHeight(), |
||
| 693 | $sourceDimension->getWidth(), |
||
| 694 | $sourceDimension->getHeight() |
||
| 695 | ); |
||
| 696 | } else {
|
||
| 697 | $this->resampleImageResource = imagecreate($resampleDimension->getWidth(), |
||
| 698 | $resampleDimension->getHeight()); |
||
| 699 | |||
| 700 | imagealphablending($this->resampleImageResource, false); |
||
| 701 | imagesavealpha($this->resampleImageResource, true); |
||
| 702 | |||
| 703 | $transparent = imagecolorallocatealpha($this->resampleImageResource, 255, 255, 255, 127); |
||
| 704 | imagefilledrectangle($this->resampleImageResource, 0, 0, $resampleDimension->getWidth(), |
||
| 705 | $resampleDimension->getHeight(), $transparent); |
||
| 706 | |||
| 707 | return imagecopyresized( |
||
| 708 | $this->resampleImageResource, |
||
| 709 | $this->sourceImageResource, |
||
| 710 | $resampleAxis->getX(), |
||
| 711 | $resampleAxis->getY(), |
||
| 712 | $sourceAxis->getX(), |
||
| 713 | $sourceAxis->getY(), |
||
| 714 | $resampleDimension->getWidth(), |
||
| 715 | $resampleDimension->getHeight(), |
||
| 716 | $sourceDimension->getWidth(), |
||
| 717 | $sourceDimension->getHeight() |
||
| 718 | ); |
||
| 719 | } |
||
| 720 | } |
||
| 721 | |||
| 722 | // ------------------------------------------------------------------------ |
||
| 723 | |||
| 724 | /** |
||
| 725 | * GdDriver::display |
||
| 726 | * |
||
| 727 | * Display an image. |
||
| 728 | * |
||
| 729 | * @return void |
||
| 730 | */ |
||
| 731 | public function display($quality = 100, $mime = null) |
||
| 732 | {
|
||
| 733 | $filename = pathinfo($this->sourceImageFile->getBasename(), PATHINFO_FILENAME); |
||
| 734 | $extension = pathinfo($this->sourceImageFile->getBasename(), PATHINFO_EXTENSION); |
||
| 735 | |||
| 736 | if (empty($mime)) {
|
||
| 737 | $mime = $this->sourceImageFile->getMime(); |
||
| 738 | $mime = is_array($mime) ? $mime[ 0 ] : $mime; |
||
| 739 | |||
| 740 | $extensions = [ |
||
| 741 | 'image/gif' => 'gif', |
||
| 742 | 'image/jpg' => 'jpg', |
||
| 743 | 'image/jpeg' => 'jpeg', |
||
| 744 | 'image/png' => 'png', |
||
| 745 | 'image/webp' => 'webp', |
||
| 746 | ]; |
||
| 747 | |||
| 748 | $extension = $extensions[ $mime ]; |
||
| 749 | } |
||
| 750 | |||
| 751 | header('Content-Disposition: filename=' . $filename . '.' . $extension);
|
||
| 752 | header('Content-Transfer-Encoding: binary');
|
||
| 753 | header('Last-Modified: ' . gmdate('D, d M Y H:i:s', time()) . ' GMT');
|
||
| 754 | header('Content-Type: ' . $mime);
|
||
| 755 | |||
| 756 | $blob = $this->blob($quality, $mime); |
||
| 757 | |||
| 758 | header('ETag: ' . md5($blob));
|
||
| 759 | |||
| 760 | echo $blob; |
||
| 761 | |||
| 762 | exit(0); |
||
| 763 | } |
||
| 764 | |||
| 765 | // ------------------------------------------------------------------------ |
||
| 766 | |||
| 767 | /** |
||
| 768 | * GdDriver::blob |
||
| 769 | * |
||
| 770 | * Returns image as string blob. |
||
| 771 | * |
||
| 772 | * @return string |
||
| 773 | */ |
||
| 774 | public function blob($quality = 100, $mime = null) |
||
| 797 | } |
||
| 798 | |||
| 799 | // ------------------------------------------------------------------------ |
||
| 800 | |||
| 801 | /** |
||
| 802 | * GdDriver::save |
||
| 803 | * |
||
| 804 | * Save an image. |
||
| 805 | * |
||
| 806 | * @param string $imageTargetFilePath |
||
| 807 | * @param int $quality |
||
| 808 | * |
||
| 809 | * @return bool |
||
| 810 | */ |
||
| 811 | public function save($imageTargetFilePath, $quality = 100) |
||
| 840 | } |
||
| 841 | } |
If you suppress an error, we recommend checking for the error condition explicitly: