Complex classes like imageLib 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 imageLib, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 163 | class imageLib |
||
| 164 | { |
||
| 165 | |||
| 166 | private $fileName; |
||
| 167 | private $image; |
||
| 168 | protected $imageResized; |
||
| 169 | private $widthOriginal; # Always be the original width |
||
| 170 | private $heightOriginal; |
||
| 171 | private $width; # Current width (width after resize) |
||
| 172 | private $height; |
||
| 173 | private $imageSize; |
||
| 174 | private $fileExtension; |
||
| 175 | |||
| 176 | private $debug = true; |
||
| 177 | private $errorArray = array(); |
||
| 178 | |||
| 179 | private $forceStretch = true; |
||
| 180 | private $aggresiveSharpening = false; |
||
| 181 | |||
| 182 | private $transparentArray = array('.png', '.gif'); |
||
| 183 | private $keepTransparency = true; |
||
| 184 | private $fillColorArray = array('r'=>255, 'g'=>255, 'b'=>255); |
||
| 185 | |||
| 186 | private $sharpenArray = array('jpg'); |
||
| 187 | |||
| 188 | private $psdReaderPath; |
||
| 189 | private $filterOverlayPath; |
||
| 190 | |||
| 191 | private $isInterlace; |
||
| 192 | |||
| 193 | private $captionBoxPositionArray = array(); |
||
| 194 | |||
| 195 | private $fontDir = 'fonts'; |
||
| 196 | |||
| 197 | private $cropFromTopPercent = 10; |
||
| 198 | |||
| 199 | ## -------------------------------------------------------- |
||
| 200 | |||
| 201 | function __construct($fileName) |
||
| 202 | # Author: Jarrod Oberto |
||
| 203 | # Date: 27-02-08 |
||
| 204 | # Purpose: Constructor |
||
| 205 | # Param in: $fileName: File name and path. |
||
| 206 | # Param out: n/a |
||
| 207 | # Reference: |
||
| 208 | # Notes: |
||
| 209 | # |
||
| 210 | { |
||
| 211 | if (!$this->testGDInstalled()) { if ($this->debug) { throw new Exception('The GD Library is not installed.'); }else{ throw new Exception(); }}; |
||
| 212 | |||
| 213 | $this->initialise(); |
||
| 214 | |||
| 215 | // *** Save the image file name. Only store this incase you want to display it |
||
| 216 | $this->fileName = $fileName; |
||
| 217 | $this->fileExtension = fix_strtolower(strrchr($fileName, '.')); |
||
| 218 | |||
| 219 | // *** Open up the file |
||
| 220 | $this->image = $this->openImage($fileName); |
||
| 221 | |||
| 222 | // *** Assign here so we don't modify the original |
||
| 223 | $this->imageResized = $this->image; |
||
| 224 | |||
| 225 | // *** If file is an image |
||
| 226 | if ($this->testIsImage($this->image)) |
||
| 227 | { |
||
| 228 | // *** Get width and height |
||
| 229 | $this->width = imagesx($this->image); |
||
| 230 | $this->widthOriginal = imagesx($this->image); |
||
| 231 | $this->height = imagesy($this->image); |
||
| 232 | $this->heightOriginal = imagesy($this->image); |
||
| 233 | |||
| 234 | /* Added 15-09-08 |
||
| 235 | * Get the filesize using this build in method. |
||
| 236 | * Stores an array of size |
||
| 237 | * |
||
| 238 | * $this->imageSize[1] = width |
||
| 239 | * $this->imageSize[2] = height |
||
| 240 | * $this->imageSize[3] = width x height |
||
| 241 | * |
||
| 242 | */ |
||
| 243 | $this->imageSize = getimagesize($this->fileName); |
||
| 244 | |||
| 245 | } else { |
||
| 246 | $this->errorArray[] = 'File is not an image'; |
||
| 247 | } |
||
| 248 | } |
||
| 249 | |||
| 250 | ## -------------------------------------------------------- |
||
| 251 | |||
| 252 | private function initialise () { |
||
| 260 | |||
| 261 | /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*-*-*-*-*-*-*-*-*- |
||
| 262 | Resize |
||
| 263 | *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*-*-*-*-*-*-*-*-*/ |
||
| 264 | |||
| 265 | public function resizeImage($newWidth, $newHeight, $option = 0, $sharpen = false, $autoRotate = false) |
||
| 266 | # Author: Jarrod Oberto |
||
| 267 | # Date: 27-02-08 |
||
| 268 | # Purpose: Resizes the image |
||
| 269 | # Param in: $newWidth: |
||
| 270 | # $newHeight: |
||
| 271 | # $option: 0 / exact = defined size; |
||
| 272 | # 1 / portrait = keep aspect set height; |
||
| 273 | # 2 / landscape = keep aspect set width; |
||
| 274 | # 3 / auto = auto; |
||
| 275 | # 4 / crop= resize and crop; |
||
| 276 | # |
||
| 277 | # $option can also be an array containing options for |
||
| 278 | # cropping. E.G., array('crop', 'r') |
||
| 279 | # |
||
| 280 | # This array only applies to 'crop' and the 'r' refers to |
||
| 281 | # "crop right". Other value include; tl, t, tr, l, m (default), |
||
| 282 | # r, bl, b, br, or you can specify your own co-ords (which |
||
| 283 | # isn't recommended. |
||
| 284 | # |
||
| 285 | # $sharpen: true: sharpen (jpg only); |
||
| 286 | # false: don't sharpen |
||
| 287 | # Param out: n/a |
||
| 288 | # Reference: |
||
| 289 | # Notes: To clarify the $option input: |
||
| 290 | # 0 = The exact height and width dimensions you set. |
||
| 291 | # 1 = Whatever height is passed in will be the height that |
||
| 292 | # is set. The width will be calculated and set automatically |
||
| 293 | # to a the value that keeps the original aspect ratio. |
||
| 294 | # 2 = The same but based on the width. We try make the image the |
||
| 295 | # biggest size we can while stil fitting inside the box size |
||
| 296 | # 3 = Depending whether the image is landscape or portrait, this |
||
| 297 | # will automatically determine whether to resize via |
||
| 298 | # dimension 1,2 or 0 |
||
| 299 | # 4 = Will resize and then crop the image for best fit |
||
| 300 | # |
||
| 301 | # forceStretch can be applied to options 1,2,3 and 4 |
||
| 302 | # |
||
| 303 | { |
||
| 304 | |||
| 305 | // *** We can pass in an array of options to change the crop position |
||
| 306 | $cropPos = 'm'; |
||
| 307 | if (is_array($option) && fix_strtolower($option[0]) == 'crop') { |
||
| 308 | $cropPos = $option[1]; # get the crop option |
||
| 309 | } else if (strpos($option, '-') !== false) { |
||
| 310 | // *** Or pass in a hyphen seperated option |
||
| 311 | $optionPiecesArray = explode('-', $option); |
||
| 312 | $cropPos = end($optionPiecesArray); |
||
| 313 | } |
||
| 314 | |||
| 315 | // *** Check the option is valid |
||
| 316 | $option = $this->prepOption($option); |
||
| 317 | |||
| 318 | // *** Make sure the file passed in is valid |
||
| 319 | if (!$this->image) { if ($this->debug) { throw new Exception('file ' . $this->getFileName() .' is missing or invalid'); }else{ throw new Exception(); }}; |
||
| 320 | |||
| 321 | // *** Get optimal width and height - based on $option |
||
| 322 | $dimensionsArray = $this->getDimensions($newWidth, $newHeight, $option); |
||
| 323 | |||
| 324 | $optimalWidth = $dimensionsArray['optimalWidth']; |
||
| 325 | $optimalHeight = $dimensionsArray['optimalHeight']; |
||
| 326 | |||
| 327 | // *** Resample - create image canvas of x, y size |
||
| 328 | $this->imageResized = imagecreatetruecolor($optimalWidth, $optimalHeight); |
||
| 329 | $this->keepTransparancy($optimalWidth, $optimalHeight, $this->imageResized); |
||
| 330 | imagecopyresampled($this->imageResized, $this->image, 0, 0, 0, 0, $optimalWidth, $optimalHeight, $this->width, $this->height); |
||
| 331 | |||
| 332 | // *** If '4', then crop too |
||
| 333 | if ($option == 4 || $option == 'crop') { |
||
| 334 | |||
| 335 | if (($optimalWidth >= $newWidth && $optimalHeight >= $newHeight)) { |
||
| 336 | $this->crop($optimalWidth, $optimalHeight, $newWidth, $newHeight, $cropPos); |
||
| 337 | } |
||
| 338 | } |
||
| 339 | |||
| 340 | // *** If Rotate. |
||
| 341 | if ($autoRotate) { |
||
| 342 | |||
| 343 | $exifData = $this->getExif(false); |
||
| 344 | if (count($exifData) > 0) { |
||
| 345 | |||
| 346 | switch($exifData['orientation']) { |
||
| 347 | case 8: |
||
| 348 | $this->imageResized = imagerotate($this->imageResized,90,0); |
||
| 349 | break; |
||
| 350 | case 3: |
||
| 351 | $this->imageResized = imagerotate($this->imageResized,180,0); |
||
| 352 | break; |
||
| 353 | case 6: |
||
| 354 | $this->imageResized = imagerotate($this->imageResized,-90,0); |
||
| 355 | break; |
||
| 356 | } |
||
| 357 | } |
||
| 358 | } |
||
| 359 | |||
| 360 | // *** Sharpen image (if jpg and the user wishes to do so) |
||
| 361 | if ($sharpen && in_array($this->fileExtension, $this->sharpenArray)) { |
||
| 362 | |||
| 363 | // *** Sharpen |
||
| 364 | $this->sharpen(); |
||
| 365 | } |
||
| 366 | } |
||
| 367 | |||
| 368 | ## -------------------------------------------------------- |
||
| 369 | |||
| 370 | public function cropImage($newWidth, $newHeight, $cropPos = 'm') |
||
| 371 | # Author: Jarrod Oberto |
||
| 372 | # Date: 08-09-11 |
||
| 373 | # Purpose: Crops the image |
||
| 374 | # Param in: $newWidth: crop with |
||
| 375 | # $newHeight: crop height |
||
| 376 | # $cropPos: Can be any of the following: |
||
| 377 | # tl, t, tr, l, m, r, bl, b, br, auto |
||
| 378 | # Or: |
||
| 379 | # a custom position such as '30x50' |
||
| 380 | # Param out: n/a |
||
| 381 | # Reference: |
||
| 382 | # Notes: |
||
| 383 | # |
||
| 384 | { |
||
| 385 | |||
| 386 | // *** Make sure the file passed in is valid |
||
| 387 | if (!$this->image) { if ($this->debug) { throw new Exception('file ' . $this->getFileName() .' is missing or invalid'); }else{ throw new Exception(); }}; |
||
| 388 | |||
| 389 | $this->imageResized = $this->image; |
||
| 390 | $this->crop($this->width, $this->height, $newWidth, $newHeight, $cropPos); |
||
| 391 | |||
| 392 | } |
||
| 393 | |||
| 394 | ## -------------------------------------------------------- |
||
| 395 | |||
| 396 | private function keepTransparancy($width, $height, $im) |
||
| 417 | |||
| 418 | ## -------------------------------------------------------- |
||
| 419 | |||
| 420 | private function crop($optimalWidth, $optimalHeight, $newWidth, $newHeight, $cropPos) |
||
| 449 | |||
| 450 | ## -------------------------------------------------------- |
||
| 451 | |||
| 452 | private function getCropPlacing($optimalWidth, $optimalHeight, $newWidth, $newHeight, $pos='m') |
||
| 453 | # |
||
| 454 | # Author: Jarrod Oberto |
||
| 455 | # Date: July 11 |
||
| 456 | # Purpose: Set the cropping area. |
||
| 457 | # Params in: |
||
| 458 | # Params out: (array) the crop x and y co-ordinates. |
||
| 459 | # Notes: When specifying the exact pixel crop position (eg 10x15), be |
||
| 460 | # very careful as it's easy to crop out of the image leaving |
||
| 461 | # black borders. |
||
| 462 | # |
||
| 463 | { |
||
| 464 | $pos = fix_strtolower($pos); |
||
| 465 | |||
| 466 | // *** If co-ords have been entered |
||
| 467 | if (strstr($pos, 'x')) { |
||
| 468 | $pos = str_replace(' ', '', $pos); |
||
| 469 | |||
| 470 | $xyArray = explode('x', $pos); |
||
| 471 | list($cropStartX, $cropStartY) = $xyArray; |
||
| 472 | |||
| 473 | } else { |
||
| 474 | |||
| 475 | switch ($pos) { |
||
| 476 | case 'tl': |
||
| 477 | $cropStartX = 0; |
||
| 478 | $cropStartY = 0; |
||
| 479 | break; |
||
| 480 | |||
| 481 | case 't': |
||
| 482 | $cropStartX = ( $optimalWidth / 2) - ( $newWidth /2 ); |
||
| 483 | $cropStartY = 0; |
||
| 484 | break; |
||
| 485 | |||
| 486 | case 'tr': |
||
| 487 | $cropStartX = $optimalWidth - $newWidth; |
||
| 488 | $cropStartY = 0; |
||
| 489 | break; |
||
| 490 | |||
| 491 | case 'l': |
||
| 492 | $cropStartX = 0; |
||
| 493 | $cropStartY = ( $optimalHeight/ 2) - ( $newHeight/2 ); |
||
| 494 | break; |
||
| 495 | |||
| 496 | case 'm': |
||
| 497 | $cropStartX = ( $optimalWidth / 2) - ( $newWidth /2 ); |
||
| 498 | $cropStartY = ( $optimalHeight/ 2) - ( $newHeight/2 ); |
||
| 499 | break; |
||
| 500 | |||
| 501 | case 'r': |
||
| 502 | $cropStartX = $optimalWidth - $newWidth; |
||
| 503 | $cropStartY = ( $optimalHeight/ 2) - ( $newHeight/2 ); |
||
| 504 | break; |
||
| 505 | |||
| 506 | case 'bl': |
||
| 507 | $cropStartX = 0; |
||
| 508 | $cropStartY = $optimalHeight - $newHeight; |
||
| 509 | break; |
||
| 510 | |||
| 511 | case 'b': |
||
| 512 | $cropStartX = ( $optimalWidth / 2) - ( $newWidth /2 ); |
||
| 513 | $cropStartY = $optimalHeight - $newHeight; |
||
| 514 | break; |
||
| 515 | |||
| 516 | case 'br': |
||
| 517 | $cropStartX = $optimalWidth - $newWidth; |
||
| 518 | $cropStartY = $optimalHeight - $newHeight; |
||
| 519 | break; |
||
| 520 | |||
| 521 | case 'auto': |
||
| 522 | // *** If image is a portrait crop from top, not center. v1.5 |
||
| 523 | if ($optimalHeight > $optimalWidth) { |
||
| 524 | $cropStartX = ( $optimalWidth / 2) - ( $newWidth /2 ); |
||
| 525 | $cropStartY = ($this->cropFromTopPercent /100) * $optimalHeight; |
||
| 526 | } else { |
||
| 527 | |||
| 528 | // *** Else crop from the center |
||
| 529 | $cropStartX = ( $optimalWidth / 2) - ( $newWidth /2 ); |
||
| 530 | $cropStartY = ( $optimalHeight/ 2) - ( $newHeight/2 ); |
||
| 531 | } |
||
| 532 | break; |
||
| 533 | |||
| 534 | default: |
||
| 535 | // *** Default to center |
||
| 536 | $cropStartX = ( $optimalWidth / 2) - ( $newWidth /2 ); |
||
| 537 | $cropStartY = ( $optimalHeight/ 2) - ( $newHeight/2 ); |
||
| 538 | break; |
||
| 539 | } |
||
| 540 | } |
||
| 541 | |||
| 542 | return array('x' => $cropStartX, 'y' => $cropStartY); |
||
| 543 | } |
||
| 544 | |||
| 545 | ## -------------------------------------------------------- |
||
| 546 | |||
| 547 | private function getDimensions($newWidth, $newHeight, $option) |
||
| 548 | # Author: Jarrod Oberto |
||
| 549 | # Date: 17-11-09 |
||
| 550 | # Purpose: Get new image dimensions based on user specificaions |
||
| 551 | # Param in: $newWidth: |
||
| 552 | # $newHeight: |
||
| 553 | # Param out: Array of new width and height values |
||
| 554 | # Reference: |
||
| 555 | # Notes: If $option = 3 then this function is call recursivly |
||
| 556 | # |
||
| 557 | # To clarify the $option input: |
||
| 558 | # 0 = The exact height and width dimensions you set. |
||
| 559 | # 1 = Whatever height is passed in will be the height that |
||
| 560 | # is set. The width will be calculated and set automatically |
||
| 561 | # to a the value that keeps the original aspect ratio. |
||
| 562 | # 2 = The same but based on the width. |
||
| 563 | # 3 = Depending whether the image is landscape or portrait, this |
||
| 564 | # will automatically determine whether to resize via |
||
| 565 | # dimension 1,2 or 0. |
||
| 566 | # 4 = Resize the image as much as possible, then crop the |
||
| 567 | # remainder. |
||
| 568 | { |
||
| 569 | |||
| 570 | switch (strval($option)) |
||
| 571 | { |
||
| 572 | case '0': |
||
| 573 | case 'exact': |
||
| 574 | $optimalWidth = $newWidth; |
||
| 575 | $optimalHeight= $newHeight; |
||
| 576 | break; |
||
| 577 | case '1': |
||
| 578 | case 'portrait': |
||
| 579 | $dimensionsArray = $this->getSizeByFixedHeight($newWidth, $newHeight); |
||
| 580 | $optimalWidth = $dimensionsArray['optimalWidth']; |
||
| 581 | $optimalHeight = $dimensionsArray['optimalHeight']; |
||
| 582 | break; |
||
| 583 | case '2': |
||
| 584 | case 'landscape': |
||
| 585 | $dimensionsArray = $this->getSizeByFixedWidth($newWidth, $newHeight); |
||
| 586 | $optimalWidth = $dimensionsArray['optimalWidth']; |
||
| 587 | $optimalHeight = $dimensionsArray['optimalHeight']; |
||
| 588 | break; |
||
| 589 | case '3': |
||
| 590 | case 'auto': |
||
| 591 | $dimensionsArray = $this->getSizeByAuto($newWidth, $newHeight); |
||
| 592 | $optimalWidth = $dimensionsArray['optimalWidth']; |
||
| 593 | $optimalHeight = $dimensionsArray['optimalHeight']; |
||
| 594 | break; |
||
| 595 | case '4': |
||
| 596 | case 'crop': |
||
| 597 | $dimensionsArray = $this->getOptimalCrop($newWidth, $newHeight); |
||
| 598 | $optimalWidth = $dimensionsArray['optimalWidth']; |
||
| 599 | $optimalHeight = $dimensionsArray['optimalHeight']; |
||
| 600 | break; |
||
| 601 | } |
||
| 602 | |||
| 603 | return array('optimalWidth' => $optimalWidth, 'optimalHeight' => $optimalHeight); |
||
| 604 | } |
||
| 605 | |||
| 606 | ## -------------------------------------------------------- |
||
| 607 | |||
| 608 | private function getSizeByFixedHeight($newWidth, $newHeight) |
||
| 609 | { |
||
| 610 | // *** If forcing is off... |
||
| 611 | if (!$this->forceStretch) { |
||
| 612 | |||
| 613 | // *** ...check if actual height is less than target height |
||
| 614 | if ($this->height < $newHeight) { |
||
| 615 | return array('optimalWidth' => $this->width, 'optimalHeight' => $this->height); |
||
| 616 | } |
||
| 617 | } |
||
| 618 | |||
| 619 | $ratio = $this->width / $this->height; |
||
| 620 | |||
| 621 | $newWidth = $newHeight * $ratio; |
||
| 622 | |||
| 623 | //return $newWidth; |
||
| 624 | return array('optimalWidth' => $newWidth, 'optimalHeight' => $newHeight); |
||
| 625 | } |
||
| 626 | |||
| 627 | ## -------------------------------------------------------- |
||
| 628 | |||
| 629 | private function getSizeByFixedWidth($newWidth, $newHeight) |
||
| 630 | { |
||
| 631 | // *** If forcing is off... |
||
| 632 | if (!$this->forceStretch) { |
||
| 633 | |||
| 634 | // *** ...check if actual width is less than target width |
||
| 635 | if ($this->width < $newWidth) { |
||
| 636 | return array('optimalWidth' => $this->width, 'optimalHeight' => $this->height); |
||
| 637 | } |
||
| 638 | } |
||
| 639 | |||
| 640 | $ratio = $this->height / $this->width; |
||
| 641 | |||
| 642 | $newHeight = $newWidth * $ratio; |
||
| 643 | |||
| 644 | //return $newHeight; |
||
| 645 | return array('optimalWidth' => $newWidth, 'optimalHeight' => $newHeight); |
||
| 646 | } |
||
| 647 | |||
| 648 | ## -------------------------------------------------------- |
||
| 649 | |||
| 650 | private function getSizeByAuto($newWidth, $newHeight) |
||
| 712 | |||
| 713 | ## -------------------------------------------------------- |
||
| 714 | |||
| 715 | private function getOptimalCrop($newWidth, $newHeight) |
||
| 770 | |||
| 771 | ## -------------------------------------------------------- |
||
| 772 | |||
| 773 | private function sharpen() |
||
| 816 | |||
| 817 | ## -------------------------------------------------------- |
||
| 818 | |||
| 819 | private function sharpen2($level) |
||
| 828 | |||
| 829 | ## -------------------------------------------------------- |
||
| 830 | |||
| 831 | private function findSharp($orig, $final) |
||
| 849 | |||
| 850 | ## -------------------------------------------------------- |
||
| 851 | |||
| 852 | private function prepOption($option) |
||
| 877 | |||
| 878 | /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*-*-*-*-*-*-*-*-*- |
||
| 879 | Presets |
||
| 880 | *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*-*-*-*-*-*-*-*-*/ |
||
| 881 | |||
| 882 | # |
||
| 883 | # Preset are pre-defined templates you can apply to your image. |
||
| 884 | # |
||
| 885 | # These are inteded to be applied to thumbnail images. |
||
| 886 | # |
||
| 887 | |||
| 888 | |||
| 889 | public function borderPreset($preset) |
||
| 905 | |||
| 906 | /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*-*-*-*-*-*-*-*-*- |
||
| 907 | Draw border |
||
| 908 | *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*-*-*-*-*-*-*-*-*/ |
||
| 909 | |||
| 910 | public function addBorder($thickness = 1, $rgbArray = array(255, 255, 255)) |
||
| 939 | |||
| 940 | /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*-*-*-*-*-*-*-*-*- |
||
| 941 | Gray Scale |
||
| 942 | *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*-*-*-*-*-*-*-*-*/ |
||
| 943 | |||
| 944 | public function greyScale() |
||
| 959 | |||
| 960 | ## -------------------------------------------------------- |
||
| 961 | |||
| 962 | public function greyScaleEnhanced() |
||
| 963 | # Author: Jarrod Oberto |
||
| 964 | # Date: 07-05-2011 |
||
| 965 | # Purpose: Make image greyscale |
||
| 966 | # Param in: n/a |
||
| 967 | # Param out: |
||
| 968 | # Reference: |
||
| 969 | # Notes: |
||
| 970 | # |
||
| 971 | { |
||
| 972 | if ($this->imageResized) { |
||
| 973 | imagefilter($this->imageResized, IMG_FILTER_GRAYSCALE); |
||
| 974 | imagefilter($this->imageResized, IMG_FILTER_CONTRAST, -15); |
||
| 975 | imagefilter($this->imageResized, IMG_FILTER_BRIGHTNESS, 2); |
||
| 976 | $this->sharpen($this->width); |
||
| 977 | } |
||
| 978 | } |
||
| 979 | |||
| 980 | ## -------------------------------------------------------- |
||
| 981 | |||
| 982 | public function greyScaleDramatic() |
||
| 987 | |||
| 988 | /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*-*-*-*-*-*-*-*-*- |
||
| 989 | Black 'n White |
||
| 990 | *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*-*-*-*-*-*-*-*-*/ |
||
| 991 | |||
| 992 | public function blackAndWhite() |
||
| 1009 | |||
| 1010 | /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*-*-*-*-*-*-*-*-*- |
||
| 1011 | Negative |
||
| 1012 | *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*-*-*-*-*-*-*-*-*/ |
||
| 1013 | |||
| 1014 | public function negative() |
||
| 1030 | |||
| 1031 | /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*-*-*-*-*-*-*-*-*- |
||
| 1032 | Sepia |
||
| 1033 | *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*-*-*-*-*-*-*-*-*/ |
||
| 1034 | |||
| 1035 | public function sepia() |
||
| 1052 | |||
| 1053 | ## -------------------------------------------------------- |
||
| 1054 | |||
| 1055 | public function sepia2() |
||
| 1071 | |||
| 1072 | /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*-*-*-*-*-*-*-*-*- |
||
| 1073 | Vintage |
||
| 1074 | *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*-*-*-*-*-*-*-*-*/ |
||
| 1075 | |||
| 1076 | public function vintage() |
||
| 1081 | |||
| 1082 | /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*-*-*-*-*-*-*-*-*- |
||
| 1083 | Presets By Marc Hibbins |
||
| 1084 | *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*-*-*-*-*-*-*-*-*/ |
||
| 1085 | |||
| 1086 | /** Apply 'Monopin' preset */ |
||
| 1087 | public function gd_filter_monopin() |
||
| 1088 | { |
||
| 1089 | |||
| 1090 | if ($this->imageResized) { |
||
| 1091 | imagefilter($this->imageResized, IMG_FILTER_GRAYSCALE); |
||
| 1092 | imagefilter($this->imageResized, IMG_FILTER_BRIGHTNESS, -15); |
||
| 1093 | imagefilter($this->imageResized, IMG_FILTER_CONTRAST, -15); |
||
| 1094 | $this->imageResized = $this->gd_apply_overlay($this->imageResized, 'vignette', 100); |
||
| 1095 | } |
||
| 1096 | } |
||
| 1097 | |||
| 1098 | ## -------------------------------------------------------- |
||
| 1099 | |||
| 1100 | public function gd_filter_vintage() |
||
| 1111 | |||
| 1112 | ## -------------------------------------------------------- |
||
| 1113 | |||
| 1114 | /** Apply a PNG overlay */ |
||
| 1115 | private function gd_apply_overlay($im, $type, $amount) |
||
| 1149 | |||
| 1150 | /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*-*-*-*-*-*-*-*-*- |
||
| 1151 | Colorise |
||
| 1152 | *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*-*-*-*-*-*-*-*-*/ |
||
| 1153 | |||
| 1154 | public function image_colorize($rgb) { |
||
| 1174 | |||
| 1175 | /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*-*-*-*-*-*-*-*-*- |
||
| 1176 | Reflection |
||
| 1177 | *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*-*-*-*-*-*-*-*-*/ |
||
| 1178 | |||
| 1179 | public function addReflection($reflectionHeight = 50, $startingTransparency = 30, $inside = false, $bgColor = '#fff', $stretch=false, $divider = 0) |
||
| 1256 | |||
| 1257 | /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*-*-*-*-*-*-*-*-*- |
||
| 1258 | Rotate |
||
| 1259 | *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*-*-*-*-*-*-*-*-*/ |
||
| 1260 | |||
| 1261 | public function rotate($value = 90, $bgColor = 'transparent') |
||
| 1325 | |||
| 1326 | /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*-*-*-*-*-*-*-*-*- |
||
| 1327 | Round corners |
||
| 1328 | *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*-*-*-*-*-*-*-*-*/ |
||
| 1329 | |||
| 1330 | public function roundCorners($radius = 5, $bgColor = 'transparent') |
||
| 1408 | |||
| 1409 | /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*-*-*-*-*-*-*-*-*- |
||
| 1410 | Shadow |
||
| 1411 | *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*-*-*-*-*-*-*-*-*/ |
||
| 1412 | |||
| 1413 | public function addShadow($shadowAngle=45, $blur=15, $bgColor='transparent') |
||
| 1547 | |||
| 1548 | /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*-*-*-*-*-*-*-*-*- |
||
| 1549 | Add Caption Box |
||
| 1550 | *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*-*-*-*-*-*-*-*-*/ |
||
| 1551 | |||
| 1552 | public function addCaptionBox($side='b', $thickness=50, $padding=0, $bgColor='#000', $transaprencyAmount=30) |
||
| 1583 | |||
| 1584 | ## -------------------------------------------------------- |
||
| 1585 | |||
| 1586 | public function addTextToCaptionBox($text, $fontColor='#fff', $fontSize = 12, $angle = 0, $font = null) |
||
| 1587 | # |
||
| 1588 | # Author: Jarrod Oberto |
||
| 1589 | # Date: 03 Aug 11 |
||
| 1590 | # Purpose: Simplify adding text to a caption box by automatically |
||
| 1591 | # locating the center of the caption box |
||
| 1592 | # Params in: The usually text paams (less a couple) |
||
| 1593 | # Params out: n/a |
||
| 1594 | # Notes: |
||
| 1595 | # |
||
| 1596 | { |
||
| 1597 | |||
| 1598 | // *** Get the caption box measurements |
||
| 1599 | if (count($this->captionBoxPositionArray) == 4) { |
||
| 1600 | $x1 = $this->captionBoxPositionArray['x1']; |
||
| 1601 | $x2 = $this->captionBoxPositionArray['x2']; |
||
| 1602 | $y1 = $this->captionBoxPositionArray['y1']; |
||
| 1603 | $y2 = $this->captionBoxPositionArray['y2']; |
||
| 1604 | } else { |
||
| 1605 | if ($this->debug) { throw new Exception('No caption box found.'); }else{ return false; } |
||
| 1606 | } |
||
| 1607 | |||
| 1608 | // *** Get text font |
||
| 1609 | $font = $this->getTextFont($font); |
||
| 1610 | |||
| 1611 | // *** Get text size |
||
| 1612 | $textSizeArray = $this->getTextSize($fontSize, $angle, $font, $text); |
||
| 1613 | $textWidth = $textSizeArray['width']; |
||
| 1614 | $textHeight = $textSizeArray['height']; |
||
| 1615 | |||
| 1616 | // *** Find the width/height middle points |
||
| 1617 | $boxXMiddle = (($x2 - $x1) / 2); |
||
| 1618 | $boxYMiddle = (($y2 - $y1) / 2); |
||
| 1619 | |||
| 1620 | // *** Box middle - half the text width/height |
||
| 1621 | $xPos = ($x1 + $boxXMiddle) - ($textWidth/2); |
||
| 1622 | $yPos = ($y1 + $boxYMiddle) - ($textHeight/2); |
||
| 1623 | |||
| 1624 | $pos = $xPos . 'x' . $yPos; |
||
| 1625 | |||
| 1626 | $this->addText($text, $pos, $padding = 0, $fontColor, $fontSize, $angle, $font); |
||
| 1627 | |||
| 1628 | } |
||
| 1629 | |||
| 1630 | ## -------------------------------------------------------- |
||
| 1631 | |||
| 1632 | private function calculateCaptionBoxPosition($side, $thickness, $padding) |
||
| 1633 | { |
||
| 1634 | $positionArray = array(); |
||
| 1635 | |||
| 1636 | switch ($side) { |
||
| 1637 | case 't': |
||
| 1638 | $positionArray['x1'] = 0; |
||
| 1639 | $positionArray['y1'] = $padding; |
||
| 1640 | $positionArray['x2'] = $this->width; |
||
| 1641 | $positionArray['y2'] = $thickness + $padding; |
||
| 1642 | break; |
||
| 1643 | case 'r': |
||
| 1644 | $positionArray['x1'] = $this->width - $thickness - $padding; |
||
| 1645 | $positionArray['y1'] = 0; |
||
| 1646 | $positionArray['x2'] = $this->width - $padding; |
||
| 1647 | $positionArray['y2'] = $this->height; |
||
| 1648 | break; |
||
| 1649 | case 'b': |
||
| 1650 | $positionArray['x1'] = 0; |
||
| 1651 | $positionArray['y1'] = $this->height - $thickness - $padding; |
||
| 1652 | $positionArray['x2'] = $this->width; |
||
| 1653 | $positionArray['y2'] = $this->height - $padding; |
||
| 1654 | break; |
||
| 1655 | case 'l': |
||
| 1656 | $positionArray['x1'] = $padding; |
||
| 1657 | $positionArray['y1'] = 0; |
||
| 1658 | $positionArray['x2'] = $thickness + $padding; |
||
| 1659 | $positionArray['y2'] = $this->height; |
||
| 1660 | break; |
||
| 1661 | |||
| 1662 | default: |
||
| 1663 | break; |
||
| 1664 | } |
||
| 1665 | |||
| 1666 | return $positionArray; |
||
| 1667 | |||
| 1668 | } |
||
| 1669 | |||
| 1670 | /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*-*-*-*-*-*-*-*-*- |
||
| 1671 | Get EXIF Data |
||
| 1672 | *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*-*-*-*-*-*-*-*-*/ |
||
| 1673 | |||
| 1674 | public function getExif($debug=false) |
||
| 1675 | # Author: Jarrod Oberto |
||
| 1676 | # Date: 07-05-2011 |
||
| 1677 | # Purpose: Get image EXIF data |
||
| 1678 | # Param in: n/a |
||
| 1679 | # Param out: An associate array of EXIF data |
||
| 1680 | # Reference: |
||
| 1681 | # Notes: |
||
| 1682 | # 23 May 13 : added orientation flag -jco |
||
| 1683 | # |
||
| 1684 | { |
||
| 1685 | |||
| 1686 | if (!$this->debug || !$debug) { $debug = false; } |
||
| 1687 | |||
| 1688 | // *** Check all is good - check the EXIF library exists and the file exists, too. |
||
| 1689 | if (!$this->testEXIFInstalled()) { if ($debug) { throw new Exception('The EXIF Library is not installed.'); }else{ return array(); }}; |
||
| 1690 | if (!file_exists($this->fileName)) { if ($debug) { throw new Exception('Image not found.'); }else{ return array(); }}; |
||
| 1691 | if ($this->fileExtension != '.jpg') { if ($debug) { throw new Exception('Metadata not supported for this image type.'); }else{ return array(); }}; |
||
| 1692 | $exifData = exif_read_data($this->fileName, 'IFD0'); |
||
| 1693 | |||
| 1694 | // *** Format the apperture value |
||
| 1695 | $ev = $exifData['ApertureValue']; |
||
| 1696 | $apPeicesArray = explode('/', $ev); |
||
| 1697 | if (count($apPeicesArray) == 2) { |
||
| 1698 | $apertureValue = round($apPeicesArray[0] / $apPeicesArray[1], 2, PHP_ROUND_HALF_DOWN) . ' EV'; |
||
| 1699 | } else { $apertureValue = '';} |
||
| 1700 | |||
| 1701 | // *** Format the focal length |
||
| 1702 | $focalLength = $exifData['FocalLength']; |
||
| 1703 | $flPeicesArray = explode('/', $focalLength); |
||
| 1704 | if (count($flPeicesArray) == 2) { |
||
| 1705 | $focalLength = $flPeicesArray[0] / $flPeicesArray[1] . '.0 mm'; |
||
| 1706 | } else { $focalLength = '';} |
||
| 1707 | |||
| 1708 | // *** Format fNumber |
||
| 1709 | $fNumber = $exifData['FNumber']; |
||
| 1710 | $fnPeicesArray = explode('/', $fNumber); |
||
| 1711 | if (count($fnPeicesArray) == 2) { |
||
| 1712 | $fNumber = $fnPeicesArray[0] / $fnPeicesArray[1]; |
||
| 1713 | } else { $fNumber = '';} |
||
| 1714 | |||
| 1715 | // *** Resolve ExposureProgram |
||
| 1716 | if (isset($exifData['ExposureProgram'])) { $ep = $exifData['ExposureProgram']; } |
||
| 1717 | if (isset($ep)) { $ep = $this->resolveExposureProgram($ep); } |
||
| 1718 | |||
| 1719 | // *** Resolve MeteringMode |
||
| 1720 | $mm = $exifData['MeteringMode']; |
||
| 1721 | $mm = $this->resolveMeteringMode($mm); |
||
| 1722 | |||
| 1723 | // *** Resolve Flash |
||
| 1724 | $flash = $exifData['Flash']; |
||
| 1725 | $flash = $this->resolveFlash($flash); |
||
| 1726 | |||
| 1727 | if (isset($exifData['Make'])) { |
||
| 1728 | $exifDataArray['make'] = $exifData['Make']; |
||
| 1729 | } else { $exifDataArray['make'] = ''; } |
||
| 1730 | |||
| 1731 | if (isset($exifData['Model'])) { |
||
| 1732 | $exifDataArray['model'] = $exifData['Model']; |
||
| 1733 | } else { $exifDataArray['model'] = ''; } |
||
| 1734 | |||
| 1735 | if (isset($exifData['DateTime'])) { |
||
| 1736 | $exifDataArray['date'] = $exifData['DateTime']; |
||
| 1737 | } else { $exifDataArray['date'] = ''; } |
||
| 1738 | |||
| 1739 | if (isset($exifData['ExposureTime'])) { |
||
| 1740 | $exifDataArray['exposure time'] = $exifData['ExposureTime'] . ' sec.'; |
||
| 1741 | } else { $exifDataArray['exposure time'] = ''; } |
||
| 1742 | |||
| 1743 | if ($apertureValue != '') { |
||
| 1744 | $exifDataArray['aperture value'] = $apertureValue; |
||
| 1745 | } else { $exifDataArray['aperture value'] = ''; } |
||
| 1746 | |||
| 1747 | if (isset($exifData['COMPUTED']['ApertureFNumber'])) { |
||
| 1748 | $exifDataArray['f-stop'] = $exifData['COMPUTED']['ApertureFNumber']; |
||
| 1749 | } else { $exifDataArray['f-stop'] = ''; } |
||
| 1750 | |||
| 1751 | if (isset($exifData['FNumber'])) { |
||
| 1752 | $exifDataArray['fnumber'] = $exifData['FNumber']; |
||
| 1753 | } else { $exifDataArray['fnumber'] = ''; } |
||
| 1754 | |||
| 1755 | if ($fNumber != '') { |
||
| 1756 | $exifDataArray['fnumber value'] = $fNumber; |
||
| 1757 | } else { $exifDataArray['fnumber value'] = ''; } |
||
| 1758 | |||
| 1759 | if (isset($exifData['ISOSpeedRatings'])) { |
||
| 1760 | $exifDataArray['iso'] = $exifData['ISOSpeedRatings']; |
||
| 1761 | } else { $exifDataArray['iso'] = ''; } |
||
| 1762 | |||
| 1763 | if ($focalLength != '') { |
||
| 1764 | $exifDataArray['focal length'] = $focalLength; |
||
| 1765 | } else { $exifDataArray['focal length'] = ''; } |
||
| 1766 | |||
| 1767 | if (isset($ep)) { |
||
| 1768 | $exifDataArray['exposure program'] = $ep; |
||
| 1769 | } else { $exifDataArray['exposure program'] = ''; } |
||
| 1770 | |||
| 1771 | if ($mm != '') { |
||
| 1772 | $exifDataArray['metering mode'] = $mm; |
||
| 1773 | } else { $exifDataArray['metering mode'] = ''; } |
||
| 1774 | |||
| 1775 | if ($flash != '') { |
||
| 1776 | $exifDataArray['flash status'] = $flash; |
||
| 1777 | } else { $exifDataArray['flash status'] = ''; } |
||
| 1778 | |||
| 1779 | if (isset($exifData['Artist'])) { |
||
| 1780 | $exifDataArray['creator'] = $exifData['Artist'] ; |
||
| 1781 | } else { $exifDataArray['creator'] = ''; } |
||
| 1782 | |||
| 1783 | if (isset($exifData['Copyright'])) { |
||
| 1784 | $exifDataArray['copyright'] = $exifData['Copyright']; |
||
| 1785 | } else { $exifDataArray['copyright'] = ''; } |
||
| 1786 | |||
| 1787 | // *** Orientation |
||
| 1788 | if (isset($exifData['Orientation'])) { |
||
| 1789 | $exifDataArray['orientation'] = $exifData['Orientation']; |
||
| 1790 | } else { $exifDataArray['orientation'] = ''; } |
||
| 1791 | |||
| 1792 | return $exifDataArray; |
||
| 1793 | } |
||
| 1794 | |||
| 1795 | ## -------------------------------------------------------- |
||
| 1796 | |||
| 1797 | private function resolveExposureProgram($ep) |
||
| 1834 | |||
| 1835 | ## -------------------------------------------------------- |
||
| 1836 | |||
| 1837 | private function resolveMeteringMode($mm) |
||
| 1871 | |||
| 1872 | ## -------------------------------------------------------- |
||
| 1873 | |||
| 1874 | private function resolveFlash($flash) |
||
| 1951 | |||
| 1952 | /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*-*-*-*-*-*-*-*-*- |
||
| 1953 | Get IPTC Data |
||
| 1954 | *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*-*-*-*-*-*-*-*-*/ |
||
| 1955 | |||
| 1956 | /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*-*-*-*-*-*-*-*-*- |
||
| 1957 | Write IPTC Data |
||
| 1958 | *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*-*-*-*-*-*-*-*-*/ |
||
| 1959 | |||
| 1960 | public function writeIPTCcaption($value) |
||
| 1965 | |||
| 1966 | ## -------------------------------------------------------- |
||
| 1967 | |||
| 1968 | public function writeIPTCwriter($value) |
||
| 1972 | |||
| 1973 | ## -------------------------------------------------------- |
||
| 1974 | |||
| 1975 | private function writeIPTC($dat, $value) |
||
| 1984 | |||
| 1985 | ## -------------------------------------------------------- |
||
| 1986 | |||
| 1987 | private function iptc_maketag($rec,$dat,$val) |
||
| 2015 | |||
| 2016 | /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*-*-*-*-*-*-*-*-*- |
||
| 2017 | Write XMP Data |
||
| 2018 | *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*-*-*-*-*-*-*-*-*/ |
||
| 2019 | |||
| 2020 | //http://xmpphptoolkit.sourceforge.net/ |
||
| 2021 | |||
| 2022 | |||
| 2023 | /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*-*-*-*-*-*-*-*-*- |
||
| 2024 | Add Text |
||
| 2025 | *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*-*-*-*-*-*-*-*-*/ |
||
| 2026 | |||
| 2027 | public function addText($text, $pos = '20x20', $padding = 0, $fontColor='#fff', $fontSize = 12, $angle = 0, $font = null) |
||
| 2028 | # Author: Jarrod Oberto |
||
| 2029 | # Date: 18-11-09 |
||
| 2030 | # Purpose: Add text to an image |
||
| 2031 | # Param in: |
||
| 2032 | # Param out: |
||
| 2033 | # Reference: http://php.net/manual/en/function.imagettftext.php |
||
| 2034 | # Notes: Make sure you supply the font. |
||
| 2035 | # |
||
| 2036 | { |
||
| 2037 | |||
| 2038 | // *** Convert color |
||
| 2039 | $rgbArray = $this->formatColor($fontColor); |
||
| 2040 | $r = $rgbArray['r']; |
||
| 2041 | $g = $rgbArray['g']; |
||
| 2042 | $b = $rgbArray['b']; |
||
| 2043 | |||
| 2044 | // *** Get text font |
||
| 2045 | $font = $this->getTextFont($font); |
||
| 2046 | |||
| 2047 | // *** Get text size |
||
| 2048 | $textSizeArray = $this->getTextSize($fontSize, $angle, $font, $text); |
||
| 2049 | $textWidth = $textSizeArray['width']; |
||
| 2050 | $textHeight = $textSizeArray['height']; |
||
| 2051 | |||
| 2052 | // *** Find co-ords to place text |
||
| 2053 | $posArray = $this->calculatePosition($pos, $padding, $textWidth, $textHeight, false); |
||
| 2054 | $x = $posArray['width']; |
||
| 2055 | $y = $posArray['height']; |
||
| 2056 | |||
| 2057 | $fontColor = imagecolorallocate($this->imageResized, $r, $g, $b); |
||
| 2058 | |||
| 2059 | // *** Add text |
||
| 2060 | imagettftext($this->imageResized, $fontSize, $angle, $x, $y, $fontColor, $font, $text); |
||
| 2061 | } |
||
| 2062 | |||
| 2063 | ## -------------------------------------------------------- |
||
| 2064 | |||
| 2065 | private function getTextFont($font) |
||
| 2090 | |||
| 2091 | ## -------------------------------------------------------- |
||
| 2092 | |||
| 2093 | private function getTextSize($fontSize, $angle, $font, $text) |
||
| 2107 | |||
| 2108 | /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*-*-*-*-*-*-*-*-*- |
||
| 2109 | Add Watermark |
||
| 2110 | *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*-*-*-*-*-*-*-*-*/ |
||
| 2111 | |||
| 2112 | public function addWatermark($watermarkImage, $pos, $padding = 0, $opacity = 0) |
||
| 2113 | # Author: Jarrod Oberto |
||
| 2114 | # Date: 18-11-09 |
||
| 2115 | # Purpose: Add watermark image |
||
| 2116 | # Param in: (str) $watermark: The watermark image |
||
| 2117 | # (str) $pos: Could be a pre-determined position such as: |
||
| 2118 | # tl = top left, |
||
| 2119 | # t = top (middle), |
||
| 2120 | # tr = top right, |
||
| 2121 | # l = left, |
||
| 2122 | # m = middle, |
||
| 2123 | # r = right, |
||
| 2124 | # bl = bottom left, |
||
| 2125 | # b = bottom (middle), |
||
| 2126 | # br = bottom right |
||
| 2127 | # Or, it could be a co-ordinate position such as: 50x100 |
||
| 2128 | # |
||
| 2129 | # (int) $padding: If using a pre-determined position you can |
||
| 2130 | # adjust the padding from the edges by passing an amount |
||
| 2131 | # in pixels. If using co-ordinates, this value is ignored. |
||
| 2132 | # Param out: |
||
| 2133 | # Reference: http://www.php.net/manual/en/image.examples-watermark.php |
||
| 2134 | # Notes: Based on example in reference. |
||
| 2135 | # |
||
| 2136 | # |
||
| 2137 | { |
||
| 2138 | |||
| 2139 | // Load the stamp and the photo to apply the watermark to |
||
| 2140 | $stamp = $this->openImage ($watermarkImage); # stamp |
||
| 2141 | $im = $this->imageResized; # photo |
||
| 2142 | |||
| 2143 | // *** Get stamps width and height |
||
| 2144 | $sx = imagesx($stamp); |
||
| 2145 | $sy = imagesy($stamp); |
||
| 2146 | |||
| 2147 | // *** Find co-ords to place image |
||
| 2148 | $posArray = $this->calculatePosition($pos, $padding, $sx, $sy); |
||
| 2149 | $x = $posArray['width']; |
||
| 2150 | $y = $posArray['height']; |
||
| 2151 | |||
| 2152 | // *** Set watermark opacity |
||
| 2153 | if (fix_strtolower(strrchr($watermarkImage, '.')) == '.png') { |
||
| 2154 | |||
| 2155 | $opacity = $this->invertTransparency($opacity, 100); |
||
| 2156 | $this->filterOpacity($stamp, $opacity); |
||
| 2157 | } |
||
| 2158 | |||
| 2159 | // Copy the watermark image onto our photo |
||
| 2160 | imagecopy($im, $stamp, $x, $y, 0, 0, imagesx($stamp), imagesy($stamp)); |
||
| 2161 | |||
| 2162 | } |
||
| 2163 | |||
| 2164 | ## -------------------------------------------------------- |
||
| 2165 | |||
| 2166 | private function calculatePosition($pos, $padding, $assetWidth, $assetHeight, $upperLeft = true) |
||
| 2167 | # |
||
| 2168 | # Author: Jarrod Oberto |
||
| 2169 | # Date: 08-05-11 |
||
| 2170 | # Purpose: Calculate the x, y pixel cordinates of the asset to place |
||
| 2171 | # Params in: (str) $pos: Either something like: "tl", "l", "br" or an |
||
| 2172 | # exact position like: "100x50" |
||
| 2173 | # (int) $padding: The amount of padding from the edge. Only |
||
| 2174 | # used for the predefined $pos. |
||
| 2175 | # (int) $assetWidth: The width of the asset to add to the image |
||
| 2176 | # (int) $assetHeight: The height of the asset to add to the image |
||
| 2177 | # (bol) $upperLeft: if true, the asset will be positioned based |
||
| 2178 | # on the upper left x, y coords. If false, it means you're |
||
| 2179 | # using the lower left as the basepoint and this will |
||
| 2180 | # convert it to the upper left position |
||
| 2181 | # Params out: |
||
| 2182 | # NOTE: this is done from the UPPER left corner!! But will convert lower |
||
| 2183 | # left basepoints to upper left if $upperleft is set to false |
||
| 2184 | # |
||
| 2185 | # |
||
| 2186 | { |
||
| 2187 | $pos = fix_strtolower($pos); |
||
| 2188 | |||
| 2189 | // *** If co-ords have been entered |
||
| 2190 | if (strstr($pos, 'x')) { |
||
| 2191 | $pos = str_replace(' ', '', $pos); |
||
| 2192 | |||
| 2193 | $xyArray = explode('x', $pos); |
||
| 2194 | list($width, $height) = $xyArray; |
||
| 2195 | |||
| 2196 | } else { |
||
| 2197 | |||
| 2198 | switch ($pos) { |
||
| 2199 | case 'tl': |
||
| 2200 | $width = 0 + $padding; |
||
| 2201 | $height = 0 + $padding; |
||
| 2202 | break; |
||
| 2203 | |||
| 2204 | case 't': |
||
| 2205 | $width = ($this->width / 2) - ($assetWidth / 2); |
||
| 2206 | $height = 0 + $padding; |
||
| 2207 | break; |
||
| 2208 | |||
| 2209 | case 'tr': |
||
| 2210 | $width = $this->width - $assetWidth - $padding; |
||
| 2211 | $height = 0 + $padding;; |
||
| 2212 | break; |
||
| 2213 | |||
| 2214 | case 'l': |
||
| 2215 | $width = 0 + $padding; |
||
| 2216 | $height = ($this->height / 2) - ($assetHeight / 2); |
||
| 2217 | break; |
||
| 2218 | |||
| 2219 | case 'm': |
||
| 2220 | $width = ($this->width / 2) - ($assetWidth / 2); |
||
| 2221 | $height = ($this->height / 2) - ($assetHeight / 2); |
||
| 2222 | break; |
||
| 2223 | |||
| 2224 | case 'r': |
||
| 2225 | $width = $this->width - $assetWidth - $padding; |
||
| 2226 | $height = ($this->height / 2) - ($assetHeight / 2); |
||
| 2227 | break; |
||
| 2228 | |||
| 2229 | case 'bl': |
||
| 2230 | $width = 0 + $padding; |
||
| 2231 | $height = $this->height - $assetHeight - $padding; |
||
| 2232 | break; |
||
| 2233 | |||
| 2234 | case 'b': |
||
| 2235 | $width = ($this->width / 2) - ($assetWidth / 2); |
||
| 2236 | $height = $this->height - $assetHeight - $padding; |
||
| 2237 | break; |
||
| 2238 | |||
| 2239 | case 'br': |
||
| 2240 | $width = $this->width - $assetWidth - $padding; |
||
| 2241 | $height = $this->height - $assetHeight - $padding; |
||
| 2242 | break; |
||
| 2243 | |||
| 2244 | default: |
||
| 2245 | $width = 0; |
||
| 2246 | $height = 0; |
||
| 2247 | break; |
||
| 2248 | } |
||
| 2249 | } |
||
| 2250 | |||
| 2251 | if (!$upperLeft) { |
||
| 2252 | $height = $height + $assetHeight; |
||
| 2253 | } |
||
| 2254 | |||
| 2255 | return array('width' => $width, 'height' => $height); |
||
| 2256 | } |
||
| 2257 | |||
| 2258 | ## -------------------------------------------------------- |
||
| 2259 | |||
| 2260 | private function filterOpacity(&$img, $opacity = 75) |
||
| 2325 | |||
| 2326 | ## -------------------------------------------------------- |
||
| 2327 | |||
| 2328 | private function openImage($file) |
||
| 2372 | |||
| 2373 | ## -------------------------------------------------------- |
||
| 2374 | |||
| 2375 | public function reset() |
||
| 2376 | # |
||
| 2377 | # Author: Jarrod Oberto |
||
| 2378 | # Date: 30-08-11 |
||
| 2379 | # Purpose: Reset the resource (allow further editing) |
||
| 2380 | # Params in: |
||
| 2381 | # Params out: |
||
| 2382 | # Notes: |
||
| 2383 | # |
||
| 2384 | { |
||
| 2385 | $this->__construct($this->fileName); |
||
| 2386 | } |
||
| 2387 | |||
| 2388 | ## -------------------------------------------------------- |
||
| 2389 | |||
| 2390 | public function saveImage($savePath, $imageQuality="100") |
||
| 2391 | # Author: Jarrod Oberto |
||
| 2392 | # Date: 27-02-08 |
||
| 2393 | # Purpose: Saves the image |
||
| 2394 | # Param in: $savePath: Where to save the image including filename: |
||
| 2395 | # $imageQuality: image quality you want the image saved at 0-100 |
||
| 2396 | # Param out: n/a |
||
| 2397 | # Reference: |
||
| 2398 | # Notes: * gif doesn't have a quality parameter |
||
| 2399 | # * jpg has a quality setting 0-100 (100 being the best) |
||
| 2400 | # * png has a quality setting 0-9 (0 being the best) |
||
| 2401 | # |
||
| 2402 | # * bmp files have no native support for bmp files. We use a |
||
| 2403 | # third party class to save as bmp. |
||
| 2404 | { |
||
| 2405 | |||
| 2406 | // *** Perform a check or two. |
||
| 2407 | if (!is_resource($this->imageResized)) { if ($this->debug) { throw new Exception('saveImage: This is not a resource.'); }else{ throw new Exception(); }} |
||
| 2408 | $fileInfoArray = pathInfo($savePath); |
||
| 2409 | clearstatcache(); |
||
| 2410 | if (!is_writable($fileInfoArray['dirname'])) { if ($this->debug) { throw new Exception('The path is not writable. Please check your permissions.'); }else{ throw new Exception(); }} |
||
| 2411 | |||
| 2412 | // *** Get extension |
||
| 2413 | $extension = strrchr($savePath, '.'); |
||
| 2414 | $extension = fix_strtolower($extension); |
||
| 2415 | |||
| 2416 | $error = ''; |
||
| 2417 | |||
| 2418 | switch($extension) |
||
| 2419 | { |
||
| 2420 | case '.jpg': |
||
| 2421 | case '.jpeg': |
||
| 2422 | $this->checkInterlaceImage($this->isInterlace); |
||
| 2423 | if (imagetypes() & IMG_JPG) { |
||
| 2424 | imagejpeg($this->imageResized, $savePath, $imageQuality); |
||
| 2425 | } else { $error = 'jpg'; } |
||
| 2426 | break; |
||
| 2427 | |||
| 2428 | case '.gif': |
||
| 2429 | $this->checkInterlaceImage($this->isInterlace); |
||
| 2430 | if (imagetypes() & IMG_GIF) { |
||
| 2431 | imagegif($this->imageResized, $savePath); |
||
| 2432 | } else { $error = 'gif'; } |
||
| 2433 | break; |
||
| 2434 | |||
| 2435 | case '.png': |
||
| 2436 | // *** Scale quality from 0-100 to 0-9 |
||
| 2437 | $scaleQuality = round(($imageQuality/100) * 9); |
||
| 2438 | |||
| 2439 | // *** Invert qualit setting as 0 is best, not 9 |
||
| 2440 | $invertScaleQuality = 9 - $scaleQuality; |
||
| 2441 | |||
| 2442 | $this->checkInterlaceImage($this->isInterlace); |
||
| 2443 | if (imagetypes() & IMG_PNG) { |
||
| 2444 | imagepng($this->imageResized, $savePath, $invertScaleQuality); |
||
| 2445 | } else { $error = 'png'; } |
||
| 2446 | break; |
||
| 2447 | |||
| 2448 | case '.bmp': |
||
| 2449 | file_put_contents($savePath, $this->GD2BMPstring($this->imageResized)); |
||
| 2450 | break; |
||
| 2451 | |||
| 2452 | // ... etc |
||
| 2453 | |||
| 2454 | default: |
||
| 2455 | // *** No extension - No save. |
||
| 2456 | $this->errorArray[] = 'This file type (' . $extension . ') is not supported. File not saved.'; |
||
| 2457 | break; |
||
| 2458 | } |
||
| 2459 | |||
| 2460 | //imagedestroy($this->imageResized); |
||
| 2461 | |||
| 2462 | // *** Display error if a file type is not supported. |
||
| 2463 | if ($error != '') { |
||
| 2464 | $this->errorArray[] = $error . ' support is NOT enabled. File not saved.'; |
||
| 2465 | } |
||
| 2466 | } |
||
| 2467 | |||
| 2468 | ## -------------------------------------------------------- |
||
| 2469 | |||
| 2470 | public function displayImage($fileType = 'jpg', $imageQuality="100") |
||
| 2471 | # Author: Jarrod Oberto |
||
| 2472 | # Date: 18-11-09 |
||
| 2473 | # Purpose: Display images directly to the browser |
||
| 2474 | # Param in: The image type you want to display |
||
| 2475 | # Param out: |
||
| 2476 | # Reference: |
||
| 2477 | # Notes: |
||
| 2478 | # |
||
| 2479 | { |
||
| 2480 | |||
| 2481 | if (!is_resource($this->imageResized)) { if ($this->debug) { throw new Exception('saveImage: This is not a resource.'); }else{ throw new Exception(); }} |
||
| 2482 | |||
| 2483 | switch($fileType) |
||
| 2484 | { |
||
| 2485 | case 'jpg': |
||
| 2486 | case 'jpeg': |
||
| 2487 | header('Content-type: image/jpeg'); |
||
| 2488 | imagejpeg($this->imageResized, '', $imageQuality); |
||
| 2489 | break; |
||
| 2490 | case 'gif': |
||
| 2491 | header('Content-type: image/gif'); |
||
| 2492 | imagegif($this->imageResized); |
||
| 2493 | break; |
||
| 2494 | case 'png': |
||
| 2495 | header('Content-type: image/png'); |
||
| 2496 | |||
| 2497 | // *** Scale quality from 0-100 to 0-9 |
||
| 2498 | $scaleQuality = round(($imageQuality/100) * 9); |
||
| 2499 | |||
| 2500 | // *** Invert qualit setting as 0 is best, not 9 |
||
| 2501 | $invertScaleQuality = 9 - $scaleQuality; |
||
| 2502 | |||
| 2503 | imagepng($this->imageResized, '', $invertScaleQuality); |
||
| 2504 | break; |
||
| 2505 | case 'bmp': |
||
| 2506 | echo 'bmp file format is not supported.'; |
||
| 2507 | break; |
||
| 2508 | |||
| 2509 | // ... etc |
||
| 2510 | |||
| 2511 | default: |
||
| 2512 | // *** No extension - No save. |
||
| 2513 | break; |
||
| 2514 | } |
||
| 2515 | |||
| 2516 | //imagedestroy($this->imageResized); |
||
| 2517 | } |
||
| 2518 | |||
| 2519 | ## -------------------------------------------------------- |
||
| 2520 | |||
| 2521 | public function setTransparency($bool) |
||
| 2526 | |||
| 2527 | ## -------------------------------------------------------- |
||
| 2528 | |||
| 2529 | public function setFillColor($value) |
||
| 2544 | |||
| 2545 | ## -------------------------------------------------------- |
||
| 2546 | |||
| 2547 | public function setCropFromTop($value) |
||
| 2552 | |||
| 2553 | ## -------------------------------------------------------- |
||
| 2554 | |||
| 2555 | public function testGDInstalled() |
||
| 2576 | |||
| 2577 | ## -------------------------------------------------------- |
||
| 2578 | |||
| 2579 | public function testEXIFInstalled() |
||
| 2600 | |||
| 2601 | ## -------------------------------------------------------- |
||
| 2602 | |||
| 2603 | public function testIsImage($image) |
||
| 2604 | # Author: Jarrod Oberto |
||
| 2605 | # Date: 27-02-08 |
||
| 2606 | # Purpose: Test if file is an image |
||
| 2607 | # Param in: n/a |
||
| 2608 | # Param out: n/a |
||
| 2609 | # Reference: |
||
| 2610 | # Notes: |
||
| 2611 | # |
||
| 2612 | { |
||
| 2613 | if ($image) |
||
| 2614 | { |
||
| 2615 | $fileIsImage = true; |
||
| 2616 | } |
||
| 2617 | else |
||
| 2618 | { |
||
| 2619 | $fileIsImage = false; |
||
| 2620 | } |
||
| 2621 | |||
| 2622 | return $fileIsImage; |
||
| 2623 | } |
||
| 2624 | |||
| 2625 | ## -------------------------------------------------------- |
||
| 2626 | |||
| 2627 | public function testFunct() |
||
| 2639 | |||
| 2640 | ## -------------------------------------------------------- |
||
| 2641 | |||
| 2642 | public function setForceStretch($value) |
||
| 2654 | |||
| 2655 | ## -------------------------------------------------------- |
||
| 2656 | |||
| 2657 | public function setFile($fileName) |
||
| 2658 | # Author: Jarrod Oberto |
||
| 2659 | # Date: 28-02-08 |
||
| 2660 | # Purpose: |
||
| 2661 | # Param in: n/a |
||
| 2662 | # Param out: n/a |
||
| 2663 | # Reference: |
||
| 2664 | # Notes: |
||
| 2665 | # |
||
| 2666 | { |
||
| 2667 | self::__construct($fileName); |
||
| 2668 | } |
||
| 2669 | |||
| 2670 | ## -------------------------------------------------------- |
||
| 2671 | |||
| 2672 | public function getFileName() |
||
| 2684 | |||
| 2685 | ## -------------------------------------------------------- |
||
| 2686 | |||
| 2687 | public function getHeight() |
||
| 2691 | |||
| 2692 | ## -------------------------------------------------------- |
||
| 2693 | |||
| 2694 | public function getWidth() |
||
| 2698 | |||
| 2699 | ## -------------------------------------------------------- |
||
| 2700 | |||
| 2701 | public function getOriginalHeight() |
||
| 2705 | |||
| 2706 | ## -------------------------------------------------------- |
||
| 2707 | |||
| 2708 | public function getOriginalWidth() |
||
| 2712 | |||
| 2713 | ## -------------------------------------------------------- |
||
| 2714 | |||
| 2715 | public function getErrors() |
||
| 2727 | |||
| 2728 | ## -------------------------------------------------------- |
||
| 2729 | |||
| 2730 | private function checkInterlaceImage($isEnabled) |
||
| 2737 | |||
| 2738 | ## -------------------------------------------------------- |
||
| 2739 | |||
| 2740 | protected function formatColor($value) |
||
| 2782 | |||
| 2783 | ## -------------------------------------------------------- |
||
| 2784 | |||
| 2785 | function hex2dec($hex) |
||
| 2803 | |||
| 2804 | ## -------------------------------------------------------- |
||
| 2805 | |||
| 2806 | private function createImageColor ($colorArray) |
||
| 2814 | |||
| 2815 | ## -------------------------------------------------------- |
||
| 2816 | |||
| 2817 | private function testColorExists($colorArray) |
||
| 2829 | |||
| 2830 | ## -------------------------------------------------------- |
||
| 2831 | |||
| 2832 | private function findUnusedGreen() |
||
| 2833 | # Purpose: We find a green color suitable to use like green-screen effect. |
||
| 2834 | # Therefore, the color must not exist in the image. |
||
| 2835 | { |
||
| 2836 | $green = 255; |
||
| 2837 | |||
| 2838 | do { |
||
| 2839 | |||
| 2840 | $greenChroma = array(0, $green, 0); |
||
| 2841 | $colorArray = $this->formatColor($greenChroma); |
||
| 2842 | $match = $this->testColorExists($colorArray); |
||
| 2843 | $green--; |
||
| 2844 | |||
| 2845 | } while ($match == false && $green > 0); |
||
| 2846 | |||
| 2847 | // *** If no match, just bite the bullet and use green value of 255 |
||
| 2848 | if (!$match) { |
||
| 2849 | $greenChroma = array(0, $green, 0); |
||
| 2850 | } |
||
| 2851 | |||
| 2852 | return $greenChroma; |
||
| 2853 | } |
||
| 2854 | |||
| 2855 | ## -------------------------------------------------------- |
||
| 2856 | |||
| 2857 | private function findUnusedBlue() |
||
| 2858 | # Purpose: We find a green color suitable to use like green-screen effect. |
||
| 2859 | # Therefore, the color must not exist in the image. |
||
| 2860 | { |
||
| 2861 | $blue = 255; |
||
| 2862 | |||
| 2863 | do { |
||
| 2864 | |||
| 2865 | $blueChroma = array(0, 0, $blue); |
||
| 2866 | $colorArray = $this->formatColor($blueChroma); |
||
| 2867 | $match = $this->testColorExists($colorArray); |
||
| 2868 | $blue--; |
||
| 2869 | |||
| 2870 | } while ($match == false && $blue > 0); |
||
| 2871 | |||
| 2872 | // *** If no match, just bite the bullet and use blue value of 255 |
||
| 2873 | if (!$match) { |
||
| 2874 | $blueChroma = array(0, 0, $blue); |
||
| 2875 | } |
||
| 2876 | |||
| 2877 | return $blueChroma; |
||
| 2878 | } |
||
| 2879 | |||
| 2880 | ## -------------------------------------------------------- |
||
| 2881 | |||
| 2882 | private function invertTransparency($value, $originalMax, $invert=true) |
||
| 2904 | |||
| 2905 | ## -------------------------------------------------------- |
||
| 2906 | |||
| 2907 | private function transparentImage($src) |
||
| 2932 | |||
| 2933 | ## -------------------------------------------------------- |
||
| 2934 | |||
| 2935 | function checkStringStartsWith($needle, $haystack) |
||
| 2940 | |||
| 2941 | /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*-*-*-*-*-*-*-*-*- |
||
| 2942 | BMP SUPPORT (SAVING) - James Heinrich |
||
| 2943 | *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*-*-*-*-*-*-*-*-*/ |
||
| 2944 | |||
| 2945 | private function GD2BMPstring(&$gd_image) |
||
| 2995 | |||
| 2996 | ## -------------------------------------------------------- |
||
| 2997 | |||
| 2998 | private function GetPixelColor(&$img, $x, $y) |
||
| 3013 | |||
| 3014 | ## -------------------------------------------------------- |
||
| 3015 | |||
| 3016 | private function LittleEndian2String($number, $minbytes=1) |
||
| 3033 | |||
| 3034 | /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*-*-*-*-*-*-*-*-*- |
||
| 3035 | BMP SUPPORT (READING) |
||
| 3036 | *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*-*-*-*-*-*-*-*-*/ |
||
| 3037 | |||
| 3038 | private function ImageCreateFromBMP($filename) |
||
| 3039 | # Author: DHKold |
||
| 3040 | # Date: The 15th of June 2005 |
||
| 3041 | # Version: 2.0B |
||
| 3042 | # Purpose: To create an image from a BMP file. |
||
| 3043 | # Param in: BMP file to open. |
||
| 3044 | # Param out: Return a resource like the other ImageCreateFrom functions |
||
| 3045 | # Reference: http://us3.php.net/manual/en/function.imagecreate.php#53879 |
||
| 3046 | # Bug fix: Author: domelca at terra dot es |
||
| 3047 | # Date: 06 March 2008 |
||
| 3048 | # Fix: Correct 16bit BMP support |
||
| 3049 | # Notes: |
||
| 3050 | # |
||
| 3051 | { |
||
| 3052 | |||
| 3053 | //Ouverture du fichier en mode binaire |
||
| 3054 | if (! $f1 = fopen($filename,"rb")) return FALSE; |
||
| 3055 | |||
| 3056 | //1 : Chargement des ent�tes FICHIER |
||
| 3057 | $FILE = unpack("vfile_type/Vfile_size/Vreserved/Vbitmap_offset", fread($f1,14)); |
||
| 3058 | if ($FILE['file_type'] != 19778) return FALSE; |
||
| 3059 | |||
| 3060 | //2 : Chargement des ent�tes BMP |
||
| 3061 | $BMP = unpack('Vheader_size/Vwidth/Vheight/vplanes/vbits_per_pixel'. |
||
| 3062 | '/Vcompression/Vsize_bitmap/Vhoriz_resolution'. |
||
| 3063 | '/Vvert_resolution/Vcolors_used/Vcolors_important', fread($f1,40)); |
||
| 3064 | $BMP['colors'] = pow(2,$BMP['bits_per_pixel']); |
||
| 3065 | |||
| 3066 | if ($BMP['size_bitmap'] == 0) $BMP['size_bitmap'] = $FILE['file_size'] - $FILE['bitmap_offset']; |
||
| 3067 | |||
| 3068 | $BMP['bytes_per_pixel'] = $BMP['bits_per_pixel']/8; |
||
| 3069 | $BMP['bytes_per_pixel2'] = ceil($BMP['bytes_per_pixel']); |
||
| 3070 | $BMP['decal'] = ($BMP['width']*$BMP['bytes_per_pixel']/4); |
||
| 3071 | $BMP['decal'] -= floor($BMP['width']*$BMP['bytes_per_pixel']/4); |
||
| 3072 | $BMP['decal'] = 4-(4*$BMP['decal']); |
||
| 3073 | |||
| 3074 | if ($BMP['decal'] == 4) $BMP['decal'] = 0; |
||
| 3075 | |||
| 3076 | //3 : Chargement des couleurs de la palette |
||
| 3077 | $PALETTE = array(); |
||
| 3078 | if ($BMP['colors'] < 16777216) |
||
| 3079 | { |
||
| 3080 | $PALETTE = unpack('V'.$BMP['colors'], fread($f1,$BMP['colors']*4)); |
||
| 3081 | } |
||
| 3082 | |||
| 3083 | //4 : Cr�ation de l'image |
||
| 3084 | $IMG = fread($f1,$BMP['size_bitmap']); |
||
| 3085 | $VIDE = chr(0); |
||
| 3086 | |||
| 3087 | $res = imagecreatetruecolor($BMP['width'],$BMP['height']); |
||
| 3088 | $P = 0; |
||
| 3089 | $Y = $BMP['height']-1; |
||
| 3090 | while ($Y >= 0) |
||
| 3091 | { |
||
| 3092 | $X=0; |
||
| 3093 | while ($X < $BMP['width']) |
||
| 3094 | { |
||
| 3095 | if ($BMP['bits_per_pixel'] == 24) |
||
| 3096 | $COLOR = unpack("V",substr($IMG,$P,3).$VIDE); |
||
| 3097 | elseif ($BMP['bits_per_pixel'] == 16) |
||
| 3098 | { |
||
| 3099 | |||
| 3100 | /* |
||
| 3101 | * BMP 16bit fix |
||
| 3102 | * ================= |
||
| 3103 | * |
||
| 3104 | * Ref: http://us3.php.net/manual/en/function.imagecreate.php#81604 |
||
| 3105 | * |
||
| 3106 | * Notes: |
||
| 3107 | * "don't work with bmp 16 bits_per_pixel. change pixel |
||
| 3108 | * generator for this." |
||
| 3109 | * |
||
| 3110 | */ |
||
| 3111 | |||
| 3112 | // *** Original code (don't work) |
||
| 3113 | //$COLOR = unpack("n",substr($IMG,$P,2)); |
||
| 3114 | //$COLOR[1] = $PALETTE[$COLOR[1]+1]; |
||
| 3115 | |||
| 3116 | $COLOR = unpack("v",substr($IMG,$P,2)); |
||
| 3117 | $blue = ($COLOR[1] & 0x001f) << 3; |
||
| 3118 | $green = ($COLOR[1] & 0x07e0) >> 3; |
||
| 3119 | $red = ($COLOR[1] & 0xf800) >> 8; |
||
| 3120 | $COLOR[1] = $red * 65536 + $green * 256 + $blue; |
||
| 3121 | |||
| 3122 | } |
||
| 3123 | elseif ($BMP['bits_per_pixel'] == 8) |
||
| 3124 | { |
||
| 3125 | $COLOR = unpack("n",$VIDE.substr($IMG,$P,1)); |
||
| 3126 | $COLOR[1] = $PALETTE[$COLOR[1]+1]; |
||
| 3127 | } |
||
| 3128 | elseif ($BMP['bits_per_pixel'] == 4) |
||
| 3129 | { |
||
| 3130 | $COLOR = unpack("n",$VIDE.substr($IMG,floor($P),1)); |
||
| 3131 | if (($P*2)%2 == 0) $COLOR[1] = ($COLOR[1] >> 4) ; else $COLOR[1] = ($COLOR[1] & 0x0F); |
||
| 3132 | $COLOR[1] = $PALETTE[$COLOR[1]+1]; |
||
| 3133 | } |
||
| 3134 | elseif ($BMP['bits_per_pixel'] == 1) |
||
| 3135 | { |
||
| 3136 | $COLOR = unpack("n",$VIDE.substr($IMG,floor($P),1)); |
||
| 3137 | if (($P*8)%8 == 0) $COLOR[1] = $COLOR[1] >>7; |
||
| 3138 | elseif (($P*8)%8 == 1) $COLOR[1] = ($COLOR[1] & 0x40)>>6; |
||
| 3139 | elseif (($P*8)%8 == 2) $COLOR[1] = ($COLOR[1] & 0x20)>>5; |
||
| 3140 | elseif (($P*8)%8 == 3) $COLOR[1] = ($COLOR[1] & 0x10)>>4; |
||
| 3141 | elseif (($P*8)%8 == 4) $COLOR[1] = ($COLOR[1] & 0x8)>>3; |
||
| 3142 | elseif (($P*8)%8 == 5) $COLOR[1] = ($COLOR[1] & 0x4)>>2; |
||
| 3143 | elseif (($P*8)%8 == 6) $COLOR[1] = ($COLOR[1] & 0x2)>>1; |
||
| 3144 | elseif (($P*8)%8 == 7) $COLOR[1] = ($COLOR[1] & 0x1); |
||
| 3145 | $COLOR[1] = $PALETTE[$COLOR[1]+1]; |
||
| 3146 | } |
||
| 3147 | else |
||
| 3148 | return FALSE; |
||
| 3149 | |||
| 3150 | imagesetpixel($res,$X,$Y,$COLOR[1]); |
||
| 3151 | ++$X; |
||
| 3152 | $P += $BMP['bytes_per_pixel']; |
||
| 3153 | } |
||
| 3154 | |||
| 3155 | $Y--; |
||
| 3156 | $P+=$BMP['decal']; |
||
| 3157 | } |
||
| 3158 | //Fermeture du fichier |
||
| 3159 | fclose($f1); |
||
| 3160 | |||
| 3161 | return $res; |
||
| 3162 | } |
||
| 3163 | |||
| 3164 | /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*-*-*-*-*-*-*-*-*- |
||
| 3165 | PSD SUPPORT (READING) |
||
| 3166 | *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*-*-*-*-*-*-*-*-*/ |
||
| 3167 | |||
| 3168 | private function imagecreatefrompsd($fileName) |
||
| 3190 | |||
| 3191 | ## -------------------------------------------------------- |
||
| 3192 | |||
| 3193 | public function __destruct() { |
||
| 3198 | |||
| 3199 | ## -------------------------------------------------------- |
||
| 3200 | |||
| 3201 | } |
||
| 3202 | |||
| 3203 | /* |
||
| 3204 | * Example with some API calls (outdated): |
||
| 3205 | * |
||
| 3206 | * |
||
| 3207 | * =============================== |
||
| 3208 | * Compulsary |
||
| 3209 | * =============================== |
||
| 3210 | * |
||
| 3211 | * include("classes/resize_class.php"); |
||
| 3212 | * |
||
| 3213 | * // *** Initialise object |
||
| 3214 | * $magicianObj = new resize('images/cars/large/a.jpg'); |
||
| 3215 | * |
||
| 3216 | * // *** Turn off stretching (optional) |
||
| 3217 | * $magicianObj -> setForceStretch(false); |
||
| 3218 | * |
||
| 3219 | * // *** Resize object |
||
| 3220 | * $magicianObj -> resizeImage(150, 100, 0); |
||
| 3221 | * |
||
| 3257 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: