1 | <?php |
||
2 | |||
3 | namespace Jackal\ImageMerge\Command; |
||
4 | |||
5 | use Jackal\ImageMerge\Command\Options\LevelCommandOption; |
||
6 | use Jackal\ImageMerge\Model\Image; |
||
7 | |||
8 | /** |
||
9 | * Class PixelCommand |
||
10 | * @package Jackal\ImageMerge\Command |
||
11 | */ |
||
12 | class PixelCommand extends AbstractCommand |
||
13 | { |
||
14 | /** |
||
15 | * PixelCommand constructor. |
||
16 | * @param LevelCommandOption $options |
||
17 | */ |
||
18 | public function __construct(LevelCommandOption $options) |
||
19 | { |
||
20 | parent::__construct($options); |
||
21 | } |
||
22 | |||
23 | /** |
||
24 | * @param Image $image |
||
25 | * @return Image |
||
26 | */ |
||
27 | public function execute(Image $image) |
||
28 | { |
||
29 | $level = $this->options->getLevel(); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
30 | if (!$level) { |
||
31 | return $image; |
||
32 | } |
||
33 | |||
34 | $resource = $image->getResource(); |
||
35 | |||
36 | // start from the top-left pixel and keep looping until we have the desired effect |
||
37 | for ($y = 0;$y < $image->getHeight();$y += $level + 1) { |
||
38 | for ($x = 0;$x < $image->getWidth();$x += $level + 1) { |
||
39 | // get the color for current pixel |
||
40 | $rgb = imagecolorsforindex($resource, imagecolorat($resource, $x, $y)); |
||
41 | |||
42 | // get the closest color from palette |
||
43 | $color = imagecolorclosest($resource, $rgb['red'], $rgb['green'], $rgb['blue']); |
||
44 | imagefilledrectangle($resource, $x, $y, $x + $level, $y + $level, $color); |
||
45 | } |
||
46 | } |
||
47 | |||
48 | return $image; |
||
49 | } |
||
50 | } |
||
51 |