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(ResourceDOInterface $resourceDO, FilesystemInterface $filesystem, ConfigInterface $config) |
18
|
|
|
{ |
19
|
|
|
parent::__construct($resourceDO, $filesystem); |
20
|
|
|
$this->config = $config; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function __invoke( |
24
|
|
|
ServerRequestInterface $request, |
25
|
|
|
ResponseInterface $response, |
26
|
|
|
callable $next = null |
27
|
|
|
) |
28
|
|
|
{ |
29
|
|
|
parent::__invoke($request, $response, $next); |
30
|
|
|
if (!$this->isSupportedResponse($response)) { |
31
|
|
|
|
32
|
|
|
return $next($request, $response); |
33
|
|
|
} |
34
|
|
|
if ($this->isAllowed()) { |
35
|
|
|
$interlacing = $this->config->get('staticus.images.compress.interlace', false); |
36
|
|
|
$quality = $this->config->get('staticus.images.compress.quality', false); |
37
|
|
|
$maxWidth = $this->config->get('staticus.images.compress.maxWidth', false); |
38
|
|
|
$maxHeight = $this->config->get('staticus.images.compress.maxHeight', false); |
39
|
|
|
$this->compress($this->resourceDO->getFilePath(), $interlacing, $quality, $maxWidth, $maxHeight); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
return $next($request, $response); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function compress($sourcePath, $interlacing = null, $quality = null, $maxWidth = null, $maxHeight = null) |
46
|
|
|
{ |
47
|
|
|
if (!$interlacing && !$quality && (!$maxWidth || !$maxHeight)) { |
48
|
|
|
|
49
|
|
|
return; |
50
|
|
|
} |
51
|
|
|
$imagick = $this->getImagick($sourcePath); |
52
|
|
|
if ($interlacing) { |
53
|
|
|
$imagick->setInterlaceScheme($interlacing); |
54
|
|
|
} |
55
|
|
|
if ($quality) { |
56
|
|
|
$imagick->setImageCompressionQuality($quality); |
57
|
|
|
} |
58
|
|
|
if ( |
59
|
|
|
// if width and height is already set in resourceDO, this middleware MUST not call default resizing |
60
|
|
|
!$this->resourceDO->getDimension() |
61
|
|
|
&& $maxWidth |
62
|
|
|
&& $imagick->getImageWidth() > $maxWidth |
63
|
|
|
&& $maxHeight |
64
|
|
|
&& $imagick->getImageHeight() > $maxHeight |
65
|
|
|
) { |
66
|
|
|
$imagick->adaptiveResizeImage($maxWidth, $maxHeight, true); |
67
|
|
|
} |
68
|
|
|
$imagick->writeImage($sourcePath); |
69
|
|
|
$imagick->clear(); |
70
|
|
|
$imagick->destroy(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return bool |
75
|
|
|
*/ |
76
|
|
View Code Duplication |
protected function isAllowed() |
|
|
|
|
77
|
|
|
{ |
78
|
|
|
return $this->config->get('staticus.images.compress.compress', false) |
79
|
|
|
&& ( |
80
|
|
|
$this->resourceDO->isNew() // For the POST method |
81
|
|
|
|| $this->resourceDO->isRecreate() // For the POST method |
82
|
|
|
) |
83
|
|
|
&& $this->filesystem->has($this->resourceDO->getFilePath()); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.