FillCommand.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. namespace Intervention\Image\Gd\Commands;
  3. use Intervention\Image\Commands\AbstractCommand;
  4. use Intervention\Image\Gd\Color;
  5. use Intervention\Image\Gd\Decoder;
  6. class FillCommand extends AbstractCommand
  7. {
  8. /**
  9. * Fills image with color or pattern
  10. *
  11. * @param \Intervention\Image\Image $image
  12. * @return boolean
  13. */
  14. public function execute($image)
  15. {
  16. $filling = $this->argument(0)->value();
  17. $x = $this->argument(1)->type('digit')->value();
  18. $y = $this->argument(2)->type('digit')->value();
  19. $width = $image->getWidth();
  20. $height = $image->getHeight();
  21. $resource = $image->getCore();
  22. try {
  23. // set image tile filling
  24. $source = new Decoder;
  25. $tile = $source->init($filling);
  26. imagesettile($image->getCore(), $tile->getCore());
  27. $filling = IMG_COLOR_TILED;
  28. } catch (\Intervention\Image\Exception\NotReadableException $e) {
  29. // set solid color filling
  30. $color = new Color($filling);
  31. $filling = $color->getInt();
  32. }
  33. imagealphablending($resource, true);
  34. if (is_int($x) && is_int($y)) {
  35. // resource should be visible through transparency
  36. $base = $image->getDriver()->newImage($width, $height)->getCore();
  37. imagecopy($base, $resource, 0, 0, 0, 0, $width, $height);
  38. // floodfill if exact position is defined
  39. imagefill($resource, $x, $y, $filling);
  40. // copy filled original over base
  41. imagecopy($base, $resource, 0, 0, 0, 0, $width, $height);
  42. // set base as new resource-core
  43. $image->setCore($base);
  44. imagedestroy($resource);
  45. } else {
  46. // fill whole image otherwise
  47. imagefilledrectangle($resource, 0, 0, $width - 1, $height - 1, $filling);
  48. }
  49. isset($tile) ? imagedestroy($tile->getCore()) : null;
  50. return true;
  51. }
  52. }