1
|
|
|
<?php |
2
|
|
|
namespace Staticus\Resources\Middlewares\Image; |
3
|
|
|
|
4
|
|
|
use Staticus\Exceptions\WrongRequestException; |
5
|
|
|
use Staticus\Resources\Image\CropImageDO; |
6
|
|
|
use Staticus\Resources\Image\ResourceImageDO; |
7
|
|
|
use Staticus\Resources\Image\ResourceImageDOInterface; |
8
|
|
|
use Staticus\Resources\Middlewares\PrepareResourceMiddlewareAbstract; |
9
|
|
|
|
10
|
|
|
abstract class PrepareImageMiddlewareAbstract extends PrepareResourceMiddlewareAbstract |
11
|
|
|
{ |
12
|
|
|
protected function fillResourceSpecialFields() |
13
|
|
|
{ |
14
|
|
|
$size = static::getParamFromRequest('size', $this->request); |
15
|
|
|
$this->parseSizeParameter($size); |
16
|
|
|
$crop = static::getParamFromRequest('crop', $this->request); |
17
|
|
|
$this->parseCropParameter($crop); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
protected function parseCropParameter($crop) |
21
|
|
|
{ |
22
|
|
|
if ($crop) { |
23
|
|
|
/* @var ResourceImageDOInterface $resource */ |
24
|
|
|
$resource = $this->resourceDO; |
25
|
|
|
$crop = explode('x', $crop); |
26
|
|
|
if (count($crop) != 4) { |
27
|
|
|
throw new WrongRequestException( |
28
|
|
|
'Crop parameter has to consist of four parts, concatenated by "x" char.' |
29
|
|
|
); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
$cropObject = new CropImageDO(); |
33
|
|
|
$cropObject->setX((int) $crop[0]); |
34
|
|
|
$cropObject->setY((int) $crop[1]); |
35
|
|
|
$cropObject->setWidth((int) $crop[2]); |
36
|
|
|
$cropObject->setHeight((int) $crop[3]); |
37
|
|
|
|
38
|
|
|
if (!$resource->getWidth() || !$cropObject->getHeight()) { |
39
|
|
|
throw new WrongRequestException( |
40
|
|
|
'You should send the size=[X]x[Y] parameter together with the crop parameter' |
41
|
|
|
); |
42
|
|
|
} |
43
|
|
|
if ($cropObject->getX() < 0 || $cropObject->getY() < 0 || |
44
|
|
|
$cropObject->getWidth() < 1 || $cropObject->getHeight() < 1 |
45
|
|
|
) { |
46
|
|
|
throw new WrongRequestException( |
47
|
|
|
'Crop parameters can not be less than zero' |
48
|
|
|
); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$resource->setCrop($cropObject); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
protected function parseSizeParameter($size) |
56
|
|
|
{ |
57
|
|
|
$width = ResourceImageDO::DEFAULT_WIDTH; |
58
|
|
|
$height = ResourceImageDO::DEFAULT_HEIGHT; |
59
|
|
|
$resource = $this->resourceDO; |
60
|
|
|
if ($size) { |
61
|
|
|
$size = explode('x', $size); |
62
|
|
|
if (!empty($size[0]) && !empty($size[1])) { |
63
|
|
|
$width = (int)$size[0]; |
64
|
|
|
$height = (int)$size[1]; |
65
|
|
|
if ($width < 0 || $height < 0) { |
66
|
|
|
throw new WrongRequestException('Sizes can not be less than zero'); |
67
|
|
|
} |
68
|
|
|
if ($width && $height) { |
69
|
|
|
$allowedSizes = $this->config->get('staticus.images.sizes'); |
70
|
|
|
if (!in_array([$width, $height], $allowedSizes)) { |
71
|
|
|
throw new WrongRequestException('Resource size is not allowed: ' . $width . 'x' . $height); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
/** @var ResourceImageDOInterface $resource */ |
77
|
|
|
$resource->setWidth($width); |
78
|
|
|
$resource->setHeight($height); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|