File indexing completed on 2025-01-26 05:29:14

0001 <?php
0002 
0003 namespace Intervention\Image\Gd\Commands;
0004 
0005 use Intervention\Image\Gd\Decoder;
0006 use Intervention\Image\Gd\Color;
0007 
0008 class FillCommand extends \Intervention\Image\Commands\AbstractCommand
0009 {
0010     /**
0011      * Fills image with color or pattern
0012      *
0013      * @param  \Intervention\Image\Image $image
0014      * @return boolean
0015      */
0016     public function execute($image)
0017     {
0018         $filling = $this->argument(0)->value();
0019         $x = $this->argument(1)->type('digit')->value();
0020         $y = $this->argument(2)->type('digit')->value();
0021 
0022         $width = $image->getWidth();
0023         $height = $image->getHeight();
0024         $resource = $image->getCore();
0025 
0026         try {
0027 
0028             // set image tile filling
0029             $source = new Decoder;
0030             $tile = $source->init($filling);
0031             imagesettile($image->getCore(), $tile->getCore());
0032             $filling = IMG_COLOR_TILED;
0033 
0034         } catch (\Intervention\Image\Exception\NotReadableException $e) {
0035 
0036             // set solid color filling
0037             $color = new Color($filling);
0038             $filling = $color->getInt();
0039         }
0040 
0041         imagealphablending($resource, true);
0042 
0043         if (is_int($x) && is_int($y)) {
0044 
0045             // resource should be visible through transparency
0046             $base = $image->getDriver()->newImage($width, $height)->getCore();
0047             imagecopy($base, $resource, 0, 0, 0, 0, $width, $height);
0048 
0049             // floodfill if exact position is defined
0050             imagefill($resource, $x, $y, $filling);
0051 
0052             // copy filled original over base
0053             imagecopy($base, $resource, 0, 0, 0, 0, $width, $height);
0054 
0055             // set base as new resource-core
0056             $image->setCore($base);
0057             imagedestroy($resource);
0058 
0059         } else {
0060             // fill whole image otherwise
0061             imagefilledrectangle($resource, 0, 0, $width - 1, $height - 1, $filling);
0062         }
0063 
0064         isset($tile) ? imagedestroy($tile->getCore()) : null;
0065 
0066         return true;
0067     }
0068 }