|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Jackal\ImageMerge\Command; |
|
4
|
|
|
|
|
5
|
|
|
use Jackal\ImageMerge\Builder\ImageBuilder; |
|
6
|
|
|
use Jackal\ImageMerge\Command\Options\MultiCoordinateCommandOption; |
|
7
|
|
|
use Jackal\ImageMerge\Exception\InvalidColorException; |
|
8
|
|
|
use Jackal\ImageMerge\Model\Color; |
|
9
|
|
|
use Jackal\ImageMerge\Model\Image; |
|
10
|
|
|
use Jackal\ImageMerge\Utils\ColorUtils; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class CropPolygonCommand |
|
14
|
|
|
* @package Jackal\ImageMerge\Command |
|
15
|
|
|
*/ |
|
16
|
|
|
class CropPolygonCommand extends AbstractCommand |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* CropPolygonCommand constructor. |
|
20
|
|
|
* @param MultiCoordinateCommandOption|null $options |
|
21
|
|
|
*/ |
|
22
|
|
|
public function __construct(MultiCoordinateCommandOption $options = null) |
|
23
|
|
|
{ |
|
24
|
|
|
parent::__construct($options); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param Image $image |
|
29
|
|
|
* @return Image |
|
30
|
|
|
* @throws InvalidColorException |
|
31
|
|
|
*/ |
|
32
|
|
|
public function execute(Image $image) |
|
33
|
|
|
{ |
|
34
|
|
|
/** @var MultiCoordinateCommandOption $options */ |
|
35
|
|
|
$options = $this->options; |
|
36
|
|
|
|
|
37
|
|
|
$builder = new ImageBuilder($image); |
|
38
|
|
|
|
|
39
|
|
|
// Setup the merge image from the source image with scaling |
|
40
|
|
|
$mergeImage = ImageCreateTrueColor($image->getWidth(), $image->getHeight()); |
|
41
|
|
|
imagecopyresampled($mergeImage, $image->getResource(), 0, 0, 0, 0, $image->getWidth(), $image->getHeight(), imagesx($image->getResource()), imagesy($image->getResource())); |
|
|
|
|
|
|
42
|
|
|
|
|
43
|
|
|
$maskPolygon = imagecreatetruecolor($image->getWidth(), $image->getHeight()); |
|
44
|
|
|
$borderColor = ColorUtils::colorIdentifier($maskPolygon, new Color('01feff')); |
|
45
|
|
|
imagefill($maskPolygon, 0, 0, $borderColor); |
|
|
|
|
|
|
46
|
|
|
|
|
47
|
|
|
// Add the transparent polygon mask |
|
48
|
|
|
$transparency = imagecolortransparent($maskPolygon, ColorUtils::colorIdentifier($maskPolygon, new Color('ff01fe'))); |
|
|
|
|
|
|
49
|
|
|
imagesavealpha($maskPolygon, true); |
|
|
|
|
|
|
50
|
|
|
imagefilledpolygon($maskPolygon, $options->toArray(), $options->countPoints(), $transparency); |
|
|
|
|
|
|
51
|
|
|
|
|
52
|
|
|
// Apply the mask |
|
53
|
|
|
imagesavealpha($mergeImage, true); |
|
54
|
|
|
imagecopymerge($mergeImage, $maskPolygon, 0, 0, 0, 0, $image->getWidth(), $image->getHeight(), 100); |
|
|
|
|
|
|
55
|
|
|
|
|
56
|
|
|
// Create the final image |
|
57
|
|
|
$destImage = ImageCreateTrueColor($image->getWidth(), $image->getHeight()); |
|
58
|
|
|
imagesavealpha($destImage, true); |
|
59
|
|
|
imagealphablending($destImage, true); |
|
|
|
|
|
|
60
|
|
|
imagecopy($destImage, $mergeImage, |
|
|
|
|
|
|
61
|
|
|
0, 0, |
|
62
|
|
|
0, 0, |
|
63
|
|
|
$image->getWidth(), $image->getHeight()); |
|
64
|
|
|
|
|
65
|
|
|
// Make the the border transparent (we're assuming there's a 2px buffer on all sides) |
|
66
|
|
|
|
|
67
|
|
|
$borderTransparency = ColorUtils::colorIdentifier($destImage, new Color('ff01fe'), true); |
|
68
|
|
|
imagesavealpha($destImage, true); |
|
69
|
|
|
imagealphablending($destImage, true); |
|
70
|
|
|
imagefill($destImage, 0, 0, $borderTransparency); |
|
71
|
|
|
|
|
72
|
|
|
$image->assignResource($destImage); |
|
73
|
|
|
|
|
74
|
|
|
$builder->crop($options->getMinX(), $options->getMinY(), $options->getCropDimention()->getWidth(), $options->getCropDimention()->getHeight()); |
|
75
|
|
|
|
|
76
|
|
|
return $image; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|