1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Rvdlee\ZfImageResizer\Controller; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Rvdlee\ZfImageResizer\Exception\InvalidArgumentException; |
7
|
|
|
use Rvdlee\ZfImageResizer\Model\Image; |
8
|
|
|
use Rvdlee\ZfImageResizer\Service\ImageResizerService; |
9
|
|
|
use Zend\Mvc\Controller\AbstractActionController; |
10
|
|
|
|
11
|
|
|
class ConsoleController extends AbstractActionController |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var ImageResizerService |
15
|
|
|
*/ |
16
|
|
|
protected $imageResizerService; |
17
|
|
|
|
18
|
|
|
public function __construct(ImageResizerService $imageResizerService) |
19
|
|
|
{ |
20
|
|
|
$this->setImageResizerService($imageResizerService); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Console wrapper for service |
25
|
|
|
* |
26
|
|
|
* @throws Exception |
27
|
|
|
*/ |
28
|
|
|
public function resizeImageAction() |
29
|
|
|
{ |
30
|
|
|
/** @var string $image */ |
31
|
|
|
$image = $this->params()->fromRoute('image', null); |
32
|
|
|
if ( ! file_exists($image)) { |
33
|
|
|
throw new InvalidArgumentException('You need the --image|-i param to be an existing image.'); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** @var int $width */ |
37
|
|
|
$width = (int) $this->params()->fromRoute('width', 100); |
38
|
|
|
/** @var int $height */ |
39
|
|
|
$height = (int) $this->params()->fromRoute('height', 100); |
40
|
|
|
/** @var string $modus */ |
41
|
|
|
$modus = $this->params()->fromRoute('modus', Image::ONLY_RESIZE_MODUS); |
|
|
|
|
42
|
|
|
/** @var string $cropModus */ |
43
|
|
|
$cropModus = $this->params()->fromRoute('crop-modus', Image::CENTERED_CROP); |
|
|
|
|
44
|
|
|
/** @var int $x */ |
45
|
|
|
$x = (int) $this->params()->fromRoute('x', 0); |
|
|
|
|
46
|
|
|
/** @var int $y */ |
47
|
|
|
$y = (int) $this->params()->fromRoute('y', 0); |
|
|
|
|
48
|
|
|
|
49
|
|
|
try { |
50
|
|
|
$this->getImageResizerService()->resizeImage($image, $width, $height); |
51
|
|
|
} catch (Exception $exception) { |
52
|
|
|
throw $exception; |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return ImageResizerService |
58
|
|
|
*/ |
59
|
|
|
public function getImageResizerService(): ImageResizerService |
60
|
|
|
{ |
61
|
|
|
return $this->imageResizerService; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param ImageResizerService $imageResizerService |
66
|
|
|
* @return ConsoleController |
67
|
|
|
*/ |
68
|
|
|
public function setImageResizerService(ImageResizerService $imageResizerService): ConsoleController |
69
|
|
|
{ |
70
|
|
|
$this->imageResizerService = $imageResizerService; |
71
|
|
|
return $this; |
72
|
|
|
} |
73
|
|
|
} |