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