-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImageConverter.php
185 lines (159 loc) · 4.77 KB
/
ImageConverter.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php
namespace ImageConverter;
class ImageConverter
{
/** @var array */
private $imageFormat = [
'gif',
'jpeg',
'jpg',
'png',
'webp',
];
/** @var array */
private $constImageFormat = [
IMAGETYPE_GIF => 'gif',
IMAGETYPE_JPEG => 'jpeg',
IMAGETYPE_PNG => 'png',
IMAGETYPE_WEBP => 'webp',
];
/**
* Do image conversion work
*
* @param string $from
* @param string $to
*
* @return resource
* @throws \InvalidArgumentException
*/
public function convert($from, $to, $quality = null)
{
$image = $this->loadImage($from);
if (!$image) {
throw new \InvalidArgumentException(sprintf('Cannot load image from %s', $from));
}
return $this->saveImage($to, $image, $quality);
}
private function loadImage($from)
{
$extension = $this->getRealExtension($from);
if (!array_key_exists($extension, $this->constImageFormat)) {
throw new \InvalidArgumentException(sprintf('The %s extension is unsupported', $extension));
}
$format = $this->constImageFormat[$extension];
switch ($format) {
case 'gif':
$image = imagecreatefromgif($from);
break;
case 'jpg':
case 'jpeg':
$image = imagecreatefromjpeg($from);
break;
case 'png':
$image = imagecreatefrompng($from);
break;
case 'webp':
$image = imagecreatefromwebp($from);
break;
default:
$image = null;
}
return $image;
}
private function saveImage($to, $image, $quality)
{
$extension = $this->getExtension($to);
if ($extension === 'jpg') {
$extension = 'jpeg';
}
if (!in_array($extension, $this->imageFormat)) {
throw new \InvalidArgumentException(sprintf('The %s extension is unsupported', $extension));
}
if (!file_exists(dirname($to))) {
$this->makeDirectory($to);
}
if(isset($quality) && !is_int($quality)) {
throw new \InvalidArgumentException(sprintf('The %s quality has to be an integer', $quality));
}
switch ($extension) {
case 'gif':
$image = imagegif($image, $to);
break;
case 'jpg':
case 'jpeg':
if ($quality < -1 && $quality > 100) {
throw new \InvalidArgumentException(sprintf('The %s quality is out of range', $quality));
}
$image = imagejpeg($image, $to, $quality);
break;
case 'png':
if ($quality < -1 && $quality > 9) {
throw new \InvalidArgumentException(sprintf('The %s quality is out of range', $quality));
}
$image = imagepng($image, $to, $quality);
break;
case 'webp':
if ($quality < 0 || $quality > 100) {
throw new \InvalidArgumentException(sprintf('The %s quality is out of range', $quality));
}
$image = imagewebp($image, $to, $quality);
break;
default:
$image = null;
}
return $image;
}
/**
* Given specific $path to detect current image extension
*/
private function getRealExtension($path)
{
$extension = exif_imagetype($path);
if (!array_key_exists($extension, $this->constImageFormat)) {
throw new \InvalidArgumentException(sprintf('Cannot detect %s extension', $path));
}
return $extension;
}
/**
* Get image extension from specific $path
*
* @param string $path
*
* @return string
*/
private function getExtension($path)
{
$pathInfo = pathinfo($path);
if (!array_key_exists('extension', $pathInfo)) {
throw new \InvalidArgumentException(sprintf('Cannot find extension from %s', $path));
}
return $pathInfo['extension'];
}
/**
* Try creating the directory
*
* @return bool
* @throws \InvalidArgumentException
*/
private function makeDirectory($to)
{
$result = @mkdir(dirname($to), 0755);
if (!$result) {
throw new \InvalidArgumentException(\sprintf('Cannot create %s directory', $to));
}
return $result;
}
}
/**
* Helper function
*
* @param string $from
* @param string $to
*
* @return resource
* @throws \InvalidArgumentException
*/
function convert($from, $to, $quality = null) {
$converter = new ImageConverter();
return $converter->convert($from, $to, $quality);
}