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
|
|
|
use Zend\Expressive\Container\Exception\NotFoundException; |
10
|
|
|
|
11
|
|
|
abstract class ImageResizeMiddlewareAbstract extends ImagePostProcessingMiddlewareAbstract |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var ConfigInterface |
15
|
|
|
*/ |
16
|
|
|
public $config; |
17
|
|
|
|
18
|
|
|
public function __construct( |
19
|
|
|
ResourceDOInterface $resourceDO |
20
|
|
|
, FilesystemInterface $filesystem |
21
|
|
|
, ConfigInterface $config |
22
|
|
|
) |
23
|
|
|
{ |
24
|
|
|
parent::__construct($resourceDO, $filesystem); |
25
|
|
|
$this->config = $config; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function __invoke( |
29
|
|
|
ServerRequestInterface $request, |
30
|
|
|
ResponseInterface $response, |
31
|
|
|
callable $next = null |
32
|
|
|
) |
33
|
|
|
{ |
34
|
|
|
parent::__invoke($request, $response, $next); |
35
|
|
|
if (!$this->isSupportedResponse($response)) { |
36
|
|
|
|
37
|
|
|
return $next($request, $response); |
38
|
|
|
} |
39
|
|
|
if ($this->resourceDO->getDimension()) { |
40
|
|
|
$path = $this->resourceDO->getFilePath(); |
41
|
|
|
|
42
|
|
|
// (POST) Resource just created or re-created |
43
|
|
|
if ($this->resourceDO->isNew() |
44
|
|
|
|| $this->resourceDO->isRecreate() |
45
|
|
|
) { |
46
|
|
|
/** |
47
|
|
|
* Explanation: If it's just an artifact that is left from the previous file after re-creation |
48
|
|
|
* than you need to remove it exact in recreation moment |
49
|
|
|
* @see \Staticus\Resources\Middlewares\Image\SaveImageMiddlewareAbstract::afterSave |
50
|
|
|
*/ |
51
|
|
|
// Some of previous middlewares already created this file size |
52
|
|
|
if ($this->filesystem->has($path)) { |
53
|
|
|
$this->resizeImage($path, $path, $this->resourceDO->getWidth(), $this->resourceDO->getHeight()); |
54
|
|
|
} else { |
55
|
|
|
$modelResourceDO = $this->getResourceWithoutSizes(); |
56
|
|
|
$this->resizeImage( |
57
|
|
|
$modelResourceDO->getFilePath() |
58
|
|
|
, $path |
59
|
|
|
, $this->resourceDO->getWidth() |
60
|
|
|
, $this->resourceDO->getHeight() |
61
|
|
|
); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
// (GET) Resource should be exist, just check if this size wasn't created before |
65
|
|
|
} else if (!$this->filesystem->has($path)) { |
66
|
|
|
$modelResourceDO = $this->getResourceWithoutSizes(); |
67
|
|
|
$this->resizeImage( |
68
|
|
|
$modelResourceDO->getFilePath() |
69
|
|
|
, $path |
70
|
|
|
, $this->resourceDO->getWidth() |
71
|
|
|
, $this->resourceDO->getHeight() |
72
|
|
|
); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $next($request, $response); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function resizeImage($sourcePath, $destinationPath, $width, $height) |
80
|
|
|
{ |
81
|
|
|
if (!$this->filesystem->has($sourcePath)) { |
82
|
|
|
throw new NotFoundException('Can not resize. Resource is not found'); |
83
|
|
|
} |
84
|
|
|
$this->createDirectory(dirname($destinationPath)); |
85
|
|
|
$imagick = $this->getImagick($sourcePath); |
86
|
|
|
if ($this->config->get('staticus.images.resize.autocrop', false)) { |
87
|
|
|
$imagick->cropThumbnailImage($width, $height); |
88
|
|
|
} else { |
89
|
|
|
$imagick->adaptiveResizeImage($width, $height, true); |
90
|
|
|
} |
91
|
|
|
$imagick->writeImage($destinationPath); |
92
|
|
|
$imagick->clear(); |
93
|
|
|
$imagick->destroy(); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|