1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace tkanstantsin\fileupload\formatter; |
5
|
|
|
|
6
|
|
|
use Imagine\Image\Box; |
7
|
|
|
use Imagine\Image\ImageInterface; |
8
|
|
|
use Imagine\Imagick\Imagine; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class ImageProcessor |
12
|
|
|
*/ |
13
|
|
|
class Image extends File |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Like background `cover` in css. |
17
|
|
|
*/ |
18
|
|
|
public const RESIZE_OUTBOUND = \Imagine\Image\ImageInterface::THUMBNAIL_OUTBOUND; |
19
|
|
|
/** |
20
|
|
|
* Like background `contain` in css. |
21
|
|
|
*/ |
22
|
|
|
public const RESIZE_INSET = \Imagine\Image\ImageInterface::THUMBNAIL_INSET; |
23
|
|
|
/** |
24
|
|
|
* Means that image may be smaller than defined in config, never bigger. |
25
|
|
|
*/ |
26
|
|
|
public const RESIZE_INSET_KEEP_RATIO = 'inset_keep_ratio'; |
27
|
|
|
|
28
|
|
|
public const DEFAULT_EXTENSION = 'jpg'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var int |
32
|
|
|
*/ |
33
|
|
|
public $width; |
34
|
|
|
/** |
35
|
|
|
* @var int |
36
|
|
|
*/ |
37
|
|
|
public $height; |
38
|
|
|
/** |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
public $mode = self::RESIZE_INSET; |
42
|
|
|
/** |
43
|
|
|
* Whether image must keep aspect ration when used inset mote. |
44
|
|
|
* Means that image would may be smaller than smaller |
45
|
|
|
* @var bool |
46
|
|
|
*/ |
47
|
|
|
public $keepRatio = true; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var Imagine |
51
|
|
|
*/ |
52
|
|
|
protected $imagine; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @inheritdoc |
56
|
|
|
* @throws \UnexpectedValueException |
57
|
|
|
* @throws \Imagine\Exception\InvalidArgumentException |
58
|
|
|
* @throws \Imagine\Exception\RuntimeException |
59
|
|
|
*/ |
60
|
|
|
protected function getContentInternal() |
61
|
|
|
{ |
62
|
|
|
$this->imagine = new Imagine(); |
63
|
|
|
$image = $this->imagine->read(parent::getContentInternal()); |
64
|
|
|
$image = $this->format($image); |
65
|
|
|
|
66
|
|
|
return $image->get($this->file->getExtension() ?? self::DEFAULT_EXTENSION); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param ImageInterface $image |
71
|
|
|
* @return ImageInterface |
72
|
|
|
* @throws \UnexpectedValueException |
73
|
|
|
* @throws \Imagine\Exception\RuntimeException |
74
|
|
|
* @throws \Imagine\Exception\InvalidArgumentException |
75
|
|
|
*/ |
76
|
|
|
protected function format(ImageInterface $image): ImageInterface |
77
|
|
|
{ |
78
|
|
|
$actualBox = $image->getSize(); |
79
|
|
|
if ($this->width !== null |
80
|
|
|
&& $this->height !== null |
81
|
|
|
) { |
82
|
|
|
$box = new Box($this->width, $this->height); |
83
|
|
|
} elseif ($this->width !== null |
84
|
|
|
&& $this->height === null |
85
|
|
|
) { |
86
|
|
|
$box = $actualBox->widen($this->width); |
87
|
|
|
} elseif ($this->width === null |
88
|
|
|
&& $this->height !== null |
89
|
|
|
) { |
90
|
|
|
$box = $actualBox->heighten($this->height); |
91
|
|
|
} else { // both are null |
92
|
|
|
return $image; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
switch ($this->mode) { |
96
|
|
|
case self::RESIZE_OUTBOUND: |
97
|
|
|
case self::RESIZE_INSET: |
98
|
|
|
return $image->thumbnail($box, $this->mode); |
99
|
|
|
case self::RESIZE_INSET_KEEP_RATIO: |
100
|
|
|
// TODO: implement new resize mode. |
101
|
|
|
throw new \UnexpectedValueException(sprintf('Resize mode `%s` not supported yet', $this->mode)); |
102
|
|
|
default: |
103
|
|
|
throw new \UnexpectedValueException(sprintf('Image resize mode `%s` not defined', $this->mode)); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
} |