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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
|
<?php
/*
* Copyright 2014-2015 Florian "Bluewind" Pritz <bluewind@server-speed.net>
*
* Licensed under AGPLv3
* (see COPYING for full license text)
*
*/
namespace libraries\Image;
interface ImageDriver {
/**
* Replace the current image by reading in a file
* @param file file to read
*/
public function read($file);
/**
* Return the current image rendered to a specific format. Passing null as
* the target_type returns the image in the format of the source image
* (loaded with read()).
*
* @param target_type one of IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG or null
* @return binary image data
*/
public function get($target_type);
/**
* Resize the image.
* @param width
* @param height
*/
public function resize($width, $height);
/**
* Crop the image to the area defined by x, y, width and height.
*
* @param x starting coordinate
* @param y starting coordinate
* @param width width of the area
* @param height height of the area
*/
public function crop($x, $y, $width, $height);
/**
* Crop/resize the image to fit into the desired size. This also rotates
* the image if the source image had an EXIF orientation tag.
*
* @param width width of the resulting image
* @param height height of the resulting image
*/
public function makeThumb($target_width, $target_height);
/**
* Rotate the image according to the sources EXIF orientation tag if any.
*/
public function apply_exif_orientation();
/**
* Mirror the image along the x axis.
*/
public function mirror();
/**
* Return priority for this driver. Higher number means a higher priority.
*
* @param mimetype mimetype of the file
* @return > 0 if supported, < 0 if the mimetype can't be handled
*/
public static function get_priority($mimetype);
}
namespace libraries;
/**
* This class deals with a single image and provides useful operations that
* operate on this image.
*/
class Image {
private $driver;
private static function get_image_drivers()
{
return array(
"libraries\Image\Drivers\GD",
"libraries\Image\Drivers\imagemagick",
);
}
/**
* Create a new object and load the contents of file.
* @param file file to read
*/
public function __construct($file)
{
$this->read($file);
}
/**
* Get the best driver supporting $mimetype.
*
* @param drivers list of driver classes
* @param mimetype mimetype the driver should support
* @return driver from $drivers or NULL if no driver supports the type
*/
private static function best_driver($drivers, $mimetype)
{
$best = 0;
$best_driver = null;
foreach ($drivers as $driver) {
$current = $driver::get_priority($mimetype);
if ($current > $best && $current > 0) {
$best_driver = $driver;
$best = $current;
}
}
if ($best_driver === NULL) {
throw new \exceptions\PublicApiException("libraries/Image/unsupported-image-type", "Unsupported image type");
}
return $best_driver;
}
/**
* Check if a mimetype is supported by the image library.
*
* @param mimetype
* @return true if supported, false otherwise
*/
public static function type_supported($mimetype)
{
static $cache = array();
if (isset($cache[$mimetype])) {
return $cache[$mimetype];
}
try {
$driver = self::best_driver(self::get_image_drivers(), $mimetype);
$cache[$mimetype] = true;
} catch (\exceptions\ApiException $e) {
if ($e->get_error_id() == "libraries/Image/unsupported-image-type") {
$cache[$mimetype] = false;
} else {
throw $e;
}
}
return $cache[$mimetype];
}
/**
* Replace the current image by reading in a file
* @param file file to read
*/
public function read($file)
{
$mimetype = mimetype($file);
$driver = self::best_driver(self::get_image_drivers(), $mimetype);
$this->driver = new $driver($file);
}
/**
* Return the current image rendered to a specific format. Passing null as
* the target_type returns the image in the format of the source image
* (loaded with read()).
*
* @param target_type one of IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG or null
* @return binary image data
*/
public function get($target_type = null)
{
return $this->driver->get($target_type);
}
/**
* Resize the image.
* @param width
* @param height
*/
public function resize($width, $height)
{
return $this->driver->resize($width, $height);
}
/**
* Crop the image to the area defined by x, y, width and height.
*
* @param x starting coordinate
* @param y starting coordinate
* @param width width of the area
* @param height height of the area
*/
public function crop($x, $y, $width, $height)
{
return $this->driver->crop($x, $y, $width, $height);
}
/**
* Crop/resize the image to fit into the desired size. This also rotates
* the image if the source image had an EXIF orientation tag.
*
* @param width width of the resulting image
* @param height height of the resulting image
*/
public function makeThumb($target_width, $target_height)
{
return $this->driver->makeThumb($target_width, $target_height);
}
static public function get_exif_orientation($file)
{
$exif = \libraries\Exif::get_exif($file);
if (isset($exif["Orientation"])) {
return $exif["Orientation"];
}
return 0;
}
/**
* Rotate the image according to the sources EXIF orientation tag if any.
*/
public function apply_exif_orientation()
{
return $this->driver->apply_exif_orientation();
}
/**
* Mirror the image along the x axis.
*/
public function mirror()
{
return $this->driver->mirror();
}
}
|