-
Notifications
You must be signed in to change notification settings - Fork 7
/
SVGImageMagickAdapter.php
171 lines (140 loc) · 4 KB
/
SVGImageMagickAdapter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
namespace IMI\SVGProductImages;
use Magento\Framework\File\Mime;
use Magento\Framework\Image\Adapter\ImageMagick;
class SVGImageMagickAdapter extends ImageMagick
{
/**
* @var Mime
*/
private $mime;
public function __construct(
\Magento\Framework\Filesystem $filesystem,
Mime $mime,
\Psr\Log\LoggerInterface $logger, array $data = []
)
{
parent::__construct($filesystem, $logger, $data);
$this->mime = $mime;
}
/**
* Add basic SVG validation
*
* @param string $filePath
*
* @return bool
*/
public function validateUploadFile($filePath)
{
//Bug in getMimeType method found: Returns 'image/svg' in case of no file extension. See \Magento\Framework\File\Mime::getMimeType
if ($this->mime->getMimeType($filePath) === 'image/svg+xml' || $this->mime->getMimeType($filePath) === 'image/svg') {
return true;
}
return parent::validateUploadFile($filePath);
}
private function getNotImplementedMessage($method)
{
return sprintf(
'%s is intentionally not implemented for SVG files, processing is not believed not to be needed for SVGs. Filename = %s',
$method, $this->_fileName
);
}
/**
* We skip most logic for SVGs, as ImageMagick can not reliabily handle them on all systems, and things like
* resizing just don't make sense.
*
* Pull requests welcome, if needed, to improve this / make it configurable.
*
* @param $method
* @return bool
*/
private function skipForSvg($method)
{
if (!isset($this->_fileName)) {
return false;
}
if (strtolower(pathinfo($this->_fileName, PATHINFO_EXTENSION)) == 'svg' ){
$this->logger->debug($this->getNotImplementedMessage($method));
return true;
}
return false;
}
public function open($fileName)
{
$this->_fileName = $fileName;
if ($this->skipForSvg(__METHOD__)) {
return;
}
return parent::open($fileName);
}
public function save($destination = null, $newName = null)
{
if ($this->skipForSvg(__METHOD__)) {
return;
}
return parent::save($destination, $newName);
}
public function getImage()
{
if ($this->skipForSvg(__METHOD__)) {
return;
}
return parent::getImage();
}
public function resize($width = null, $height = null)
{
if ($this->skipForSvg(__METHOD__)) {
return;
}
return parent::resize($width, $height);
}
public function rotate($angle)
{
if ($this->skipForSvg(__METHOD__)) {
return;
}
return parent::rotate($angle);
}
public function crop($top = 0, $left = 0, $right = 0, $bottom = 0)
{
if ($this->skipForSvg(__METHOD__)) {
return;
}
return parent::crop($top, $left, $right, $bottom);
}
public function watermark($imagePath, $positionX = 0, $positionY = 0, $opacity = 30, $tile = false)
{
if ($this->skipForSvg(__METHOD__)) {
return;
}
return parent::watermark($imagePath, $positionX, $positionY, $opacity, $tile);
}
public function checkDependencies()
{
if ($this->skipForSvg(__METHOD__)) {
return;
}
parent::checkDependencies();
}
public function createPngFromString($text, $font = '')
{
if ($this->skipForSvg(__METHOD__)) {
return;
}
return parent::createPngFromString($text, $font);
}
public function refreshImageDimensions()
{
if ($this->skipForSvg(__METHOD__)) {
return;
}
return parent::refreshImageDimensions();
}
public function getColorAt($x, $y)
{
if ($this->skipForSvg(__METHOD__)) {
return;
}
return parent::getColorAt($x, $y);
}
}