File indexing completed on 2025-01-26 05:29:14
0001 <?php 0002 0003 namespace Intervention\Image\Imagick\Commands; 0004 0005 use Intervention\Image\Image; 0006 use Intervention\Image\Imagick\Decoder; 0007 use Intervention\Image\Imagick\Color; 0008 0009 class FillCommand extends \Intervention\Image\Commands\AbstractCommand 0010 { 0011 /** 0012 * Fills image with color or pattern 0013 * 0014 * @param \Intervention\Image\Image $image 0015 * @return boolean 0016 */ 0017 public function execute($image) 0018 { 0019 $filling = $this->argument(0)->value(); 0020 $x = $this->argument(1)->type('digit')->value(); 0021 $y = $this->argument(2)->type('digit')->value(); 0022 0023 $imagick = $image->getCore(); 0024 0025 try { 0026 // set image filling 0027 $source = new Decoder; 0028 $filling = $source->init($filling); 0029 0030 } catch (\Intervention\Image\Exception\NotReadableException $e) { 0031 0032 // set solid color filling 0033 $filling = new Color($filling); 0034 } 0035 0036 // flood fill if coordinates are set 0037 if (is_int($x) && is_int($y)) { 0038 0039 // flood fill with texture 0040 if ($filling instanceof Image) { 0041 0042 // create tile 0043 $tile = clone $image->getCore(); 0044 0045 // mask away color at position 0046 $tile->transparentPaintImage($tile->getImagePixelColor($x, $y), 0, 0, false); 0047 0048 // create canvas 0049 $canvas = clone $image->getCore(); 0050 0051 // fill canvas with texture 0052 $canvas = $canvas->textureImage($filling->getCore()); 0053 0054 // merge canvas and tile 0055 $canvas->compositeImage($tile, \Imagick::COMPOSITE_DEFAULT, 0, 0); 0056 0057 // replace image core 0058 $image->setCore($canvas); 0059 0060 // flood fill with color 0061 } elseif ($filling instanceof Color) { 0062 0063 // create canvas with filling 0064 $canvas = new \Imagick; 0065 $canvas->newImage($image->getWidth(), $image->getHeight(), $filling->getPixel(), 'png'); 0066 0067 // create tile to put on top 0068 $tile = clone $image->getCore(); 0069 0070 // mask away color at pos. 0071 $tile->transparentPaintImage($tile->getImagePixelColor($x, $y), 0, 0, false); 0072 0073 // save alpha channel of original image 0074 $alpha = clone $image->getCore(); 0075 0076 // merge original with canvas and tile 0077 $image->getCore()->compositeImage($canvas, \Imagick::COMPOSITE_DEFAULT, 0, 0); 0078 $image->getCore()->compositeImage($tile, \Imagick::COMPOSITE_DEFAULT, 0, 0); 0079 0080 // restore alpha channel of original image 0081 $image->getCore()->compositeImage($alpha, \Imagick::COMPOSITE_COPYOPACITY, 0, 0); 0082 } 0083 0084 } else { 0085 0086 if ($filling instanceof Image) { 0087 0088 // fill whole image with texture 0089 $image->setCore($image->getCore()->textureImage($filling->getCore())); 0090 0091 } elseif ($filling instanceof Color) { 0092 0093 // fill whole image with color 0094 $draw = new \ImagickDraw(); 0095 $draw->setFillColor($filling->getPixel()); 0096 $draw->rectangle(0, 0, $image->getWidth(), $image->getHeight()); 0097 $image->getCore()->drawImage($draw); 0098 } 0099 } 0100 0101 return true; 0102 } 0103 }