Просмотр файла vendor/intervention/image/src/Intervention/Image/Imagick/Commands/FillCommand.php

Размер файла: 3.31Kb
  1. <?php
  2.  
  3. namespace Intervention\Image\Imagick\Commands;
  4.  
  5. use Intervention\Image\Commands\AbstractCommand;
  6. use Intervention\Image\Exception\NotReadableException;
  7. use Intervention\Image\Image;
  8. use Intervention\Image\Imagick\Color;
  9. use Intervention\Image\Imagick\Decoder;
  10.  
  11. class FillCommand extends AbstractCommand
  12. {
  13. /**
  14. * Fills image with color or pattern
  15. *
  16. * @param \Intervention\Image\Image $image
  17. * @return boolean
  18. */
  19. public function execute($image)
  20. {
  21. $filling = $this->argument(0)->value();
  22. $x = $this->argument(1)->type('digit')->value();
  23. $y = $this->argument(2)->type('digit')->value();
  24.  
  25. $imagick = $image->getCore();
  26.  
  27. try {
  28. // set image filling
  29. $source = new Decoder;
  30. $filling = $source->init($filling);
  31.  
  32. } catch (NotReadableException $e) {
  33.  
  34. // set solid color filling
  35. $filling = new Color($filling);
  36. }
  37.  
  38. // flood fill if coordinates are set
  39. if (is_int($x) && is_int($y)) {
  40.  
  41. // flood fill with texture
  42. if ($filling instanceof Image) {
  43.  
  44. // create tile
  45. $tile = clone $image->getCore();
  46.  
  47. // mask away color at position
  48. $tile->transparentPaintImage($tile->getImagePixelColor($x, $y), 0, 0, false);
  49.  
  50. // create canvas
  51. $canvas = clone $image->getCore();
  52.  
  53. // fill canvas with texture
  54. $canvas = $canvas->textureImage($filling->getCore());
  55.  
  56. // merge canvas and tile
  57. $canvas->compositeImage($tile, \Imagick::COMPOSITE_DEFAULT, 0, 0);
  58.  
  59. // replace image core
  60. $image->setCore($canvas);
  61.  
  62. // flood fill with color
  63. } elseif ($filling instanceof Color) {
  64.  
  65. // create canvas with filling
  66. $canvas = new \Imagick;
  67. $canvas->newImage($image->getWidth(), $image->getHeight(), $filling->getPixel(), 'png');
  68.  
  69. // create tile to put on top
  70. $tile = clone $image->getCore();
  71.  
  72. // mask away color at pos.
  73. $tile->transparentPaintImage($tile->getImagePixelColor($x, $y), 0, 0, false);
  74.  
  75. // save alpha channel of original image
  76. $alpha = clone $image->getCore();
  77.  
  78. // merge original with canvas and tile
  79. $image->getCore()->compositeImage($canvas, \Imagick::COMPOSITE_DEFAULT, 0, 0);
  80. $image->getCore()->compositeImage($tile, \Imagick::COMPOSITE_DEFAULT, 0, 0);
  81.  
  82. // restore alpha channel of original image
  83. $image->getCore()->compositeImage($alpha, \Imagick::COMPOSITE_COPYOPACITY, 0, 0);
  84. }
  85.  
  86. } else {
  87.  
  88. if ($filling instanceof Image) {
  89.  
  90. // fill whole image with texture
  91. $image->setCore($image->getCore()->textureImage($filling->getCore()));
  92.  
  93. } elseif ($filling instanceof Color) {
  94.  
  95. // fill whole image with color
  96. $draw = new \ImagickDraw();
  97. $draw->setFillColor($filling->getPixel());
  98. $draw->rectangle(0, 0, $image->getWidth(), $image->getHeight());
  99. $image->getCore()->drawImage($draw);
  100. }
  101. }
  102.  
  103. return true;
  104. }
  105. }