1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Jackal\ImageMerge\Command; |
4
|
|
|
|
5
|
|
|
use Jackal\ImageMerge\Command\Options\DimensionCommandOption; |
6
|
|
|
use Jackal\ImageMerge\Model\Image; |
7
|
|
|
use Jackal\ImageMerge\ValueObject\Dimention; |
8
|
|
|
|
9
|
|
|
class ResizeCommand extends AbstractCommand |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* ResizeCommand constructor. |
13
|
|
|
* @param DimensionCommandOption $options |
14
|
|
|
*/ |
15
|
|
|
public function __construct(DimensionCommandOption $options) |
16
|
|
|
{ |
17
|
|
|
parent::__construct($options); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param Image $image |
22
|
|
|
* @return Image |
23
|
|
|
*/ |
24
|
|
|
public function execute(Image $image) |
25
|
|
|
{ |
26
|
|
|
if (!$this->options->getDimention()->getWidth()) { |
|
|
|
|
27
|
|
|
$this->options->add('dimention', new Dimention(round($image->getAspectRatio() * $this->options->getDimention()->getHeight()), $this->options->getDimention()->getHeight())); |
|
|
|
|
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
if (!$this->options->getDimention()->getHeight()) { |
31
|
|
|
$this->options->add('dimention', new Dimention($this->options->getDimention()->getWidth(), round($this->options->getDimention()->getWidth() / $image->getAspectRatio()))); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
$width = $this->options->getDimention()->getWidth(); |
35
|
|
|
$height = $this->options->getDimention()->getHeight(); |
36
|
|
|
|
37
|
|
|
if ($image->getWidth() != $width or $image->getHeight() != $height) { |
38
|
|
|
$resourceResized = imagecreatetruecolor($width, $height); |
39
|
|
|
imagealphablending($resourceResized, false); |
|
|
|
|
40
|
|
|
imagesavealpha($resourceResized, true); |
|
|
|
|
41
|
|
|
$transparent = imagecolorallocatealpha($resourceResized, 255, 255, 255, 127); |
|
|
|
|
42
|
|
|
imagecolortransparent($resourceResized, $transparent); |
|
|
|
|
43
|
|
|
imagecopyresampled($resourceResized, $image->getResource(), 0, 0, 0, 0, $width, $height, $image->getWidth(), $image->getHeight()); |
|
|
|
|
44
|
|
|
|
45
|
|
|
return $image->assignResource($resourceResized); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
return $image; |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|