|
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 ImageStripMiddlewareAbstract extends ImagePostProcessingMiddlewareAbstract |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var ConfigInterface |
|
14
|
|
|
*/ |
|
15
|
|
|
public $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->mustBeStripped()) { |
|
39
|
|
|
$this->stripImage($this->resourceDO->getFilePath()); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
return $next($request, $response); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function stripImage($sourcePath) |
|
46
|
|
|
{ |
|
47
|
|
|
$imagick = $this->getImagick($sourcePath); |
|
48
|
|
|
$imagick->stripImage(); |
|
49
|
|
|
$imagick->writeImage($sourcePath); |
|
50
|
|
|
$imagick->clear(); |
|
51
|
|
|
$imagick->destroy(); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @return bool |
|
56
|
|
|
*/ |
|
57
|
|
|
protected function mustBeStripped() |
|
58
|
|
|
{ |
|
59
|
|
|
return $this->config->get('staticus.images.exif.strip', false) |
|
60
|
|
|
&& ( |
|
61
|
|
|
$this->resourceDO->isNew() // For the POST method |
|
62
|
|
|
|| $this->resourceDO->isRecreate() // For the POST method |
|
63
|
|
|
) |
|
64
|
|
|
&& $this->filesystem->has($this->resourceDO->getFilePath()); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|