|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Jackal\ImageMerge\Command\Effect; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use InvalidArgumentException; |
|
7
|
|
|
use Jackal\BinLocator\BinLocator; |
|
8
|
|
|
use Jackal\ImageMerge\Command\Options\MultiCoordinateCommandOption; |
|
9
|
|
|
use Jackal\ImageMerge\Utils\GeometryUtils; |
|
10
|
|
|
use Jackal\ImageMerge\ValueObject\Coordinate; |
|
11
|
|
|
use Jackal\ImageMerge\Model\File\Filename; |
|
12
|
|
|
use Jackal\ImageMerge\Model\File\FileTempObject; |
|
13
|
|
|
use Jackal\ImageMerge\Model\Image; |
|
14
|
|
|
use Symfony\Component\Process\Exception\ProcessFailedException; |
|
15
|
|
|
use Symfony\Component\Process\Process; |
|
16
|
|
|
|
|
17
|
|
|
class Distortion extends AbstractImageMagickCommand |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* Distortion constructor. |
|
21
|
|
|
* @param MultiCoordinateCommandOption $options |
|
22
|
|
|
*/ |
|
23
|
|
|
public function __construct(MultiCoordinateCommandOption $options) |
|
24
|
|
|
{ |
|
25
|
|
|
parent::__construct($options); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param Image $image |
|
30
|
|
|
* @return Image |
|
31
|
|
|
* @throws Exception |
|
32
|
|
|
*/ |
|
33
|
|
|
public function execute(Image $image) |
|
34
|
|
|
{ |
|
35
|
|
|
$originImage = $image; |
|
36
|
|
|
|
|
37
|
|
|
$outputFilepathname = Filename::createTempFilename(); |
|
38
|
|
|
|
|
39
|
|
|
$inputFile = FileTempObject::fromString($originImage->toPNG()->getContent()); |
|
40
|
|
|
|
|
41
|
|
|
/** @var MultiCoordinateCommandOption $options */ |
|
42
|
|
|
$options = $this->options; |
|
43
|
|
|
|
|
44
|
|
|
if (!$options->isQuadrilateral()) { |
|
45
|
|
|
throw new InvalidArgumentException('Coordinates must represent a quadrilateral shape'); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
$options = GeometryUtils::getClockwiseOrder($options); |
|
49
|
|
|
|
|
50
|
|
|
/** @var Coordinate[] $coordinates */ |
|
51
|
|
|
$coordinates = $options->toArray(); |
|
52
|
|
|
|
|
53
|
|
|
$width = $originImage->getWidth(); |
|
54
|
|
|
$height = $originImage->getHeight(); |
|
55
|
|
|
|
|
56
|
|
|
$locator = new BinLocator('convert'); |
|
57
|
|
|
|
|
58
|
|
|
$process = $locator->getProcess([ |
|
59
|
|
|
$inputFile->getPathname(), |
|
60
|
|
|
'-matte -virtual-pixel black -distort Perspective', |
|
61
|
|
|
sprintf( |
|
62
|
|
|
"'%s,%s 0,0 %s,%s %s,0 %s,%s %s,%s %s,%s 0,%s' %s", |
|
63
|
|
|
$coordinates[0], |
|
64
|
|
|
$coordinates[1], |
|
65
|
|
|
$coordinates[2], |
|
66
|
|
|
$coordinates[3], |
|
67
|
|
|
$width, |
|
68
|
|
|
$coordinates[4], |
|
69
|
|
|
$coordinates[5], |
|
70
|
|
|
$width, |
|
71
|
|
|
$height, |
|
72
|
|
|
$coordinates[6], |
|
73
|
|
|
$coordinates[7], |
|
74
|
|
|
$height, |
|
75
|
|
|
$outputFilepathname |
|
76
|
|
|
), |
|
77
|
|
|
]); |
|
78
|
|
|
|
|
79
|
|
|
$process->run(); |
|
80
|
|
|
|
|
81
|
|
|
if (!$process->isSuccessful()) { |
|
82
|
|
|
throw new ProcessFailedException($process); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
//just to delete file after process |
|
86
|
|
|
$tempfile = new FileTempObject($outputFilepathname); |
|
87
|
|
|
|
|
88
|
|
|
return Image::fromFile($tempfile); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|