FillCommand.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace Intervention\Image\Imagick\Commands;
  3. use \Intervention\Image\Image;
  4. use \Intervention\Image\Imagick\Decoder;
  5. use \Intervention\Image\Imagick\Color;
  6. class FillCommand extends \Intervention\Image\Commands\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. $imagick = $image->getCore();
  20. try {
  21. // set image filling
  22. $source = new Decoder;
  23. $filling = $source->init($filling);
  24. } catch (\Intervention\Image\Exception\NotReadableException $e) {
  25. // set solid color filling
  26. $filling = new Color($filling);
  27. }
  28. // flood fill if coordinates are set
  29. if (is_int($x) && is_int($y)) {
  30. // flood fill with texture
  31. if ($filling instanceof Image) {
  32. // create tile
  33. $tile = clone $image->getCore();
  34. // mask away color at position
  35. $tile->transparentPaintImage($tile->getImagePixelColor($x, $y), 0, 0, false);
  36. // create canvas
  37. $canvas = clone $image->getCore();
  38. // fill canvas with texture
  39. $canvas = $canvas->textureImage($filling->getCore());
  40. // merge canvas and tile
  41. $canvas->compositeImage($tile, \Imagick::COMPOSITE_DEFAULT, 0, 0);
  42. // replace image core
  43. $image->setCore($canvas);
  44. // flood fill with color
  45. } elseif ($filling instanceof Color) {
  46. // create canvas with filling
  47. $canvas = new \Imagick;
  48. $canvas->newImage($image->getWidth(), $image->getHeight(), $filling->getPixel(), 'png');
  49. // create tile to put on top
  50. $tile = clone $image->getCore();
  51. // mask away color at pos.
  52. $tile->transparentPaintImage($tile->getImagePixelColor($x, $y), 0, 0, false);
  53. // save alpha channel of original image
  54. $alpha = clone $image->getCore();
  55. // merge original with canvas and tile
  56. $image->getCore()->compositeImage($canvas, \Imagick::COMPOSITE_DEFAULT, 0, 0);
  57. $image->getCore()->compositeImage($tile, \Imagick::COMPOSITE_DEFAULT, 0, 0);
  58. // restore alpha channel of original image
  59. $image->getCore()->compositeImage($alpha, \Imagick::COMPOSITE_COPYOPACITY, 0, 0);
  60. }
  61. } else {
  62. if ($filling instanceof Image) {
  63. // fill whole image with texture
  64. $image->setCore($image->getCore()->textureImage($filling->getCore()));
  65. } elseif ($filling instanceof Color) {
  66. // fill whole image with color
  67. $draw = new \ImagickDraw();
  68. $draw->setFillColor($filling->getPixel());
  69. $draw->rectangle(0, 0, $image->getWidth(), $image->getHeight());
  70. $image->getCore()->drawImage($draw);
  71. }
  72. }
  73. return true;
  74. }
  75. }