|
1
|
|
|
<?php |
|
2
|
|
|
namespace Staticus\Resources\Middlewares\Image; |
|
3
|
|
|
|
|
4
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
5
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
6
|
|
|
use Staticus\Resources\Image\CropImageDOInterface; |
|
7
|
|
|
use Zend\Expressive\Container\Exception\NotFoundException; |
|
8
|
|
|
|
|
9
|
|
|
abstract class ImageCropMiddlewareAbstract extends ImagePostProcessingMiddlewareAbstract |
|
10
|
|
|
{ |
|
11
|
|
|
public function __invoke( |
|
12
|
|
|
ServerRequestInterface $request, |
|
13
|
|
|
ResponseInterface $response, |
|
14
|
|
|
callable $next = null |
|
15
|
|
|
) |
|
16
|
|
|
{ |
|
17
|
|
|
parent::__invoke($request, $response, $next); |
|
18
|
|
|
if (!$this->isSupportedResponse($response)) { |
|
19
|
|
|
|
|
20
|
|
|
return $next($request, $response); |
|
21
|
|
|
} |
|
22
|
|
|
$crop = $this->resourceDO->getCrop(); |
|
23
|
|
|
if ($this->resourceDO->getDimension() && $crop) { |
|
24
|
|
|
$path = $this->resourceDO->getFilePath(); |
|
25
|
|
|
|
|
26
|
|
|
// (POST) Resource just created or re-created |
|
27
|
|
|
if ($this->resourceDO->isNew() |
|
28
|
|
|
|| $this->resourceDO->isRecreate() |
|
29
|
|
|
) { |
|
30
|
|
|
/** |
|
31
|
|
|
* Explanation: If it's just an artifact that is left from the previous file after re-creation |
|
32
|
|
|
* than you need to remove it exact in recreation moment |
|
33
|
|
|
* @see \Staticus\Resources\Middlewares\Image\SaveImageMiddlewareAbstract::afterSave |
|
34
|
|
|
*/ |
|
35
|
|
|
// Some of previous middlewares already created this file size |
|
36
|
|
|
if ($this->filesystem->has($path)) { |
|
37
|
|
|
$this->cropImage($path, $path, $crop); |
|
38
|
|
|
} else { |
|
39
|
|
|
$modelResourceDO = $this->getResourceWithoutSizes(); |
|
40
|
|
|
$this->cropImage($modelResourceDO->getFilePath(), $path, $crop); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
// (GET) Resource should be exist, just check if this size wasn't created before |
|
44
|
|
|
} else if (!$this->filesystem->has($path)) { |
|
45
|
|
|
$modelResourceDO = $this->getResourceWithoutSizes(); |
|
46
|
|
|
$this->cropImage($modelResourceDO->getFilePath(), $path, $crop); |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
return $next($request, $response); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function cropImage($sourcePath, $destinationPath, CropImageDOInterface $crop) |
|
54
|
|
|
{ |
|
55
|
|
|
if (!$this->filesystem->has($sourcePath)) { |
|
56
|
|
|
throw new NotFoundException('Can not crop. Resource is not found'); |
|
57
|
|
|
} |
|
58
|
|
|
$this->createDirectory(dirname($destinationPath)); |
|
59
|
|
|
$imagick = $this->getImagick($sourcePath); |
|
60
|
|
|
$imagick->cropImage( |
|
61
|
|
|
$crop->getWidth(), |
|
62
|
|
|
$crop->getHeight(), |
|
63
|
|
|
$crop->getX(), |
|
64
|
|
|
$crop->getY() |
|
65
|
|
|
); |
|
66
|
|
|
$imagick->writeImage($destinationPath); |
|
67
|
|
|
$imagick->clear(); |
|
68
|
|
|
$imagick->destroy(); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|