|
1
|
|
|
<?php |
|
2
|
|
|
namespace Staticus\Resources\Middlewares\Image; |
|
3
|
|
|
|
|
4
|
|
|
use League\Flysystem\FilesystemInterface; |
|
5
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
6
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
7
|
|
|
use Staticus\Config\ConfigInterface; |
|
8
|
|
|
use Staticus\Resources\ResourceDOInterface; |
|
9
|
|
|
|
|
10
|
|
|
abstract class ImageCompressMiddlewareAbstract extends ImagePostProcessingMiddlewareAbstract |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var ConfigInterface |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $config; |
|
16
|
|
|
|
|
17
|
|
|
public function __construct( |
|
18
|
|
|
ResourceDOInterface $resourceDO |
|
19
|
|
|
, FilesystemInterface $filesystem |
|
20
|
|
|
, ConfigInterface $config |
|
21
|
|
|
) |
|
22
|
|
|
{ |
|
23
|
|
|
parent::__construct($resourceDO, $filesystem); |
|
24
|
|
|
$this->config = $config; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
public function __invoke( |
|
28
|
|
|
ServerRequestInterface $request, |
|
29
|
|
|
ResponseInterface $response, |
|
30
|
|
|
callable $next = null |
|
31
|
|
|
) |
|
32
|
|
|
{ |
|
33
|
|
|
parent::__invoke($request, $response, $next); |
|
34
|
|
|
if (!$this->isSupportedResponse($response)) { |
|
35
|
|
|
|
|
36
|
|
|
return $next($request, $response); |
|
37
|
|
|
} |
|
38
|
|
|
if ($this->isAllowed()) { |
|
39
|
|
|
$interlacing = $this->config->get('staticus.images.compress.interlace', false); |
|
40
|
|
|
$quality = $this->config->get('staticus.images.compress.quality', false); |
|
41
|
|
|
$maxWidth = $this->config->get('staticus.images.compress.maxWidth', false); |
|
42
|
|
|
$maxHeight = $this->config->get('staticus.images.compress.maxHeight', false); |
|
43
|
|
|
$this->compress($this->resourceDO->getFilePath(), $interlacing, $quality, $maxWidth, $maxHeight); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
return $next($request, $response); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function compress($sourcePath, $interlacing = null, $quality = null, $maxWidth = null, $maxHeight = null) |
|
50
|
|
|
{ |
|
51
|
|
|
if (!$interlacing && !$quality && (!$maxWidth || !$maxHeight)) { |
|
52
|
|
|
|
|
53
|
|
|
return; |
|
54
|
|
|
} |
|
55
|
|
|
$imagick = $this->getImagick($sourcePath); |
|
56
|
|
|
if ($interlacing) { |
|
57
|
|
|
$imagick->setInterlaceScheme($interlacing); |
|
58
|
|
|
} |
|
59
|
|
|
if ($quality) { |
|
60
|
|
|
$imagick->setImageCompressionQuality($quality); |
|
61
|
|
|
} |
|
62
|
|
|
if ( |
|
63
|
|
|
// if width and height is already set in resourceDO, this middleware MUST not call default resizing |
|
64
|
|
|
!$this->resourceDO->getDimension() |
|
65
|
|
|
&& $maxWidth |
|
66
|
|
|
&& $imagick->getImageWidth() > $maxWidth |
|
67
|
|
|
&& $maxHeight |
|
68
|
|
|
&& $imagick->getImageHeight() > $maxHeight |
|
69
|
|
|
) { |
|
70
|
|
|
$imagick->adaptiveResizeImage($maxWidth, $maxHeight, true); |
|
71
|
|
|
} |
|
72
|
|
|
$imagick->writeImage($sourcePath); |
|
73
|
|
|
$imagick->clear(); |
|
74
|
|
|
$imagick->destroy(); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @return bool |
|
79
|
|
|
*/ |
|
80
|
|
|
protected function isAllowed() |
|
81
|
|
|
{ |
|
82
|
|
|
return $this->config->get('staticus.images.compress.compress', false) |
|
83
|
|
|
&& ( |
|
84
|
|
|
$this->resourceDO->isNew() // For the POST method |
|
85
|
|
|
|| $this->resourceDO->isRecreate() // For the POST method |
|
86
|
|
|
) |
|
87
|
|
|
&& $this->filesystem->has($this->resourceDO->getFilePath()); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|