FillCommand.php 1.9 KB

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