danielebarchiesi@0
|
1 <?php
|
danielebarchiesi@0
|
2
|
danielebarchiesi@0
|
3 /**
|
danielebarchiesi@0
|
4 * @file
|
danielebarchiesi@0
|
5 * API for manipulating images.
|
danielebarchiesi@0
|
6 */
|
danielebarchiesi@0
|
7
|
danielebarchiesi@0
|
8 /**
|
danielebarchiesi@0
|
9 * @defgroup image Image toolkits
|
danielebarchiesi@0
|
10 * @{
|
danielebarchiesi@0
|
11 * Functions for image file manipulations.
|
danielebarchiesi@0
|
12 *
|
danielebarchiesi@0
|
13 * Drupal's image toolkits provide an abstraction layer for common image file
|
danielebarchiesi@0
|
14 * manipulations like scaling, cropping, and rotating. The abstraction frees
|
danielebarchiesi@0
|
15 * module authors from the need to support multiple image libraries, and it
|
danielebarchiesi@0
|
16 * allows site administrators to choose the library that's best for them.
|
danielebarchiesi@0
|
17 *
|
danielebarchiesi@0
|
18 * PHP includes the GD library by default so a GD toolkit is installed with
|
danielebarchiesi@0
|
19 * Drupal. Other toolkits like ImageMagick are available from contrib modules.
|
danielebarchiesi@0
|
20 * GD works well for small images, but using it with larger files may cause PHP
|
danielebarchiesi@0
|
21 * to run out of memory. In contrast the ImageMagick library does not suffer
|
danielebarchiesi@0
|
22 * from this problem, but it requires the ISP to have installed additional
|
danielebarchiesi@0
|
23 * software.
|
danielebarchiesi@0
|
24 *
|
danielebarchiesi@0
|
25 * Image toolkits are discovered based on the associated module's
|
danielebarchiesi@0
|
26 * hook_image_toolkits. Additionally the image toolkit include file
|
danielebarchiesi@0
|
27 * must be identified in the files array in the module.info file. The
|
danielebarchiesi@0
|
28 * toolkit must then be enabled using the admin/config/media/image-toolkit
|
danielebarchiesi@0
|
29 * form.
|
danielebarchiesi@0
|
30 *
|
danielebarchiesi@0
|
31 * Only one toolkit may be selected at a time. If a module author wishes to call
|
danielebarchiesi@0
|
32 * a specific toolkit they can check that it is installed by calling
|
danielebarchiesi@0
|
33 * image_get_available_toolkits(), and then calling its functions directly.
|
danielebarchiesi@0
|
34 */
|
danielebarchiesi@0
|
35
|
danielebarchiesi@0
|
36 /**
|
danielebarchiesi@0
|
37 * Gets a list of available toolkits.
|
danielebarchiesi@0
|
38 *
|
danielebarchiesi@0
|
39 * @return
|
danielebarchiesi@0
|
40 * An array with the toolkit names as keys and the descriptions as values.
|
danielebarchiesi@0
|
41 */
|
danielebarchiesi@0
|
42 function image_get_available_toolkits() {
|
danielebarchiesi@0
|
43 // hook_image_toolkits returns an array of toolkit names.
|
danielebarchiesi@0
|
44 $toolkits = module_invoke_all('image_toolkits');
|
danielebarchiesi@0
|
45
|
danielebarchiesi@0
|
46 $output = array();
|
danielebarchiesi@0
|
47 foreach ($toolkits as $name => $info) {
|
danielebarchiesi@0
|
48 // Only allow modules that aren't marked as unavailable.
|
danielebarchiesi@0
|
49 if ($info['available']) {
|
danielebarchiesi@0
|
50 $output[$name] = $info['title'];
|
danielebarchiesi@0
|
51 }
|
danielebarchiesi@0
|
52 }
|
danielebarchiesi@0
|
53
|
danielebarchiesi@0
|
54 return $output;
|
danielebarchiesi@0
|
55 }
|
danielebarchiesi@0
|
56
|
danielebarchiesi@0
|
57 /**
|
danielebarchiesi@0
|
58 * Gets the name of the currently used toolkit.
|
danielebarchiesi@0
|
59 *
|
danielebarchiesi@0
|
60 * @return
|
danielebarchiesi@0
|
61 * String containing the name of the selected toolkit, or FALSE on error.
|
danielebarchiesi@0
|
62 */
|
danielebarchiesi@0
|
63 function image_get_toolkit() {
|
danielebarchiesi@0
|
64 static $toolkit;
|
danielebarchiesi@0
|
65
|
danielebarchiesi@0
|
66 if (!isset($toolkit)) {
|
danielebarchiesi@0
|
67 $toolkits = image_get_available_toolkits();
|
danielebarchiesi@0
|
68 $toolkit = variable_get('image_toolkit', 'gd');
|
danielebarchiesi@0
|
69 if (!isset($toolkits[$toolkit]) || !function_exists('image_' . $toolkit . '_load')) {
|
danielebarchiesi@0
|
70 // The selected toolkit isn't available so return the first one found. If
|
danielebarchiesi@0
|
71 // none are available this will return FALSE.
|
danielebarchiesi@0
|
72 reset($toolkits);
|
danielebarchiesi@0
|
73 $toolkit = key($toolkits);
|
danielebarchiesi@0
|
74 }
|
danielebarchiesi@0
|
75 }
|
danielebarchiesi@0
|
76
|
danielebarchiesi@0
|
77 return $toolkit;
|
danielebarchiesi@0
|
78 }
|
danielebarchiesi@0
|
79
|
danielebarchiesi@0
|
80 /**
|
danielebarchiesi@0
|
81 * Invokes the given method using the currently selected toolkit.
|
danielebarchiesi@0
|
82 *
|
danielebarchiesi@0
|
83 * @param $method
|
danielebarchiesi@0
|
84 * A string containing the method to invoke.
|
danielebarchiesi@0
|
85 * @param $image
|
danielebarchiesi@0
|
86 * An image object returned by image_load().
|
danielebarchiesi@0
|
87 * @param $params
|
danielebarchiesi@0
|
88 * An optional array of parameters to pass to the toolkit method.
|
danielebarchiesi@0
|
89 *
|
danielebarchiesi@0
|
90 * @return
|
danielebarchiesi@0
|
91 * Mixed values (typically Boolean indicating successful operation).
|
danielebarchiesi@0
|
92 */
|
danielebarchiesi@0
|
93 function image_toolkit_invoke($method, stdClass $image, array $params = array()) {
|
danielebarchiesi@0
|
94 $function = 'image_' . $image->toolkit . '_' . $method;
|
danielebarchiesi@0
|
95 if (function_exists($function)) {
|
danielebarchiesi@0
|
96 array_unshift($params, $image);
|
danielebarchiesi@0
|
97 return call_user_func_array($function, $params);
|
danielebarchiesi@0
|
98 }
|
danielebarchiesi@0
|
99 watchdog('image', 'The selected image handling toolkit %toolkit can not correctly process %function.', array('%toolkit' => $image->toolkit, '%function' => $function), WATCHDOG_ERROR);
|
danielebarchiesi@0
|
100 return FALSE;
|
danielebarchiesi@0
|
101 }
|
danielebarchiesi@0
|
102
|
danielebarchiesi@0
|
103 /**
|
danielebarchiesi@0
|
104 * Gets details about an image.
|
danielebarchiesi@0
|
105 *
|
danielebarchiesi@0
|
106 * Drupal supports GIF, JPG and PNG file formats when used with the GD
|
danielebarchiesi@0
|
107 * toolkit, and may support others, depending on which toolkits are
|
danielebarchiesi@0
|
108 * installed.
|
danielebarchiesi@0
|
109 *
|
danielebarchiesi@0
|
110 * @param $filepath
|
danielebarchiesi@0
|
111 * String specifying the path of the image file.
|
danielebarchiesi@0
|
112 * @param $toolkit
|
danielebarchiesi@0
|
113 * An optional image toolkit name to override the default.
|
danielebarchiesi@0
|
114 *
|
danielebarchiesi@0
|
115 * @return
|
danielebarchiesi@0
|
116 * FALSE, if the file could not be found or is not an image. Otherwise, a
|
danielebarchiesi@0
|
117 * keyed array containing information about the image:
|
danielebarchiesi@0
|
118 * - "width": Width, in pixels.
|
danielebarchiesi@0
|
119 * - "height": Height, in pixels.
|
danielebarchiesi@0
|
120 * - "extension": Commonly used file extension for the image.
|
danielebarchiesi@0
|
121 * - "mime_type": MIME type ('image/jpeg', 'image/gif', 'image/png').
|
danielebarchiesi@0
|
122 * - "file_size": File size in bytes.
|
danielebarchiesi@0
|
123 */
|
danielebarchiesi@0
|
124 function image_get_info($filepath, $toolkit = FALSE) {
|
danielebarchiesi@0
|
125 $details = FALSE;
|
danielebarchiesi@0
|
126 if (!is_file($filepath) && !is_uploaded_file($filepath)) {
|
danielebarchiesi@0
|
127 return $details;
|
danielebarchiesi@0
|
128 }
|
danielebarchiesi@0
|
129
|
danielebarchiesi@0
|
130 if (!$toolkit) {
|
danielebarchiesi@0
|
131 $toolkit = image_get_toolkit();
|
danielebarchiesi@0
|
132 }
|
danielebarchiesi@0
|
133 if ($toolkit) {
|
danielebarchiesi@0
|
134 $image = new stdClass();
|
danielebarchiesi@0
|
135 $image->source = $filepath;
|
danielebarchiesi@0
|
136 $image->toolkit = $toolkit;
|
danielebarchiesi@0
|
137 $details = image_toolkit_invoke('get_info', $image);
|
danielebarchiesi@0
|
138 if (isset($details) && is_array($details)) {
|
danielebarchiesi@0
|
139 $details['file_size'] = filesize($filepath);
|
danielebarchiesi@0
|
140 }
|
danielebarchiesi@0
|
141 }
|
danielebarchiesi@0
|
142
|
danielebarchiesi@0
|
143 return $details;
|
danielebarchiesi@0
|
144 }
|
danielebarchiesi@0
|
145
|
danielebarchiesi@0
|
146 /**
|
danielebarchiesi@0
|
147 * Scales an image to the exact width and height given.
|
danielebarchiesi@0
|
148 *
|
danielebarchiesi@0
|
149 * This function achieves the target aspect ratio by cropping the original image
|
danielebarchiesi@0
|
150 * equally on both sides, or equally on the top and bottom. This function is
|
danielebarchiesi@0
|
151 * useful to create uniform sized avatars from larger images.
|
danielebarchiesi@0
|
152 *
|
danielebarchiesi@0
|
153 * The resulting image always has the exact target dimensions.
|
danielebarchiesi@0
|
154 *
|
danielebarchiesi@0
|
155 * @param $image
|
danielebarchiesi@0
|
156 * An image object returned by image_load().
|
danielebarchiesi@0
|
157 * @param $width
|
danielebarchiesi@0
|
158 * The target width, in pixels.
|
danielebarchiesi@0
|
159 * @param $height
|
danielebarchiesi@0
|
160 * The target height, in pixels.
|
danielebarchiesi@0
|
161 *
|
danielebarchiesi@0
|
162 * @return
|
danielebarchiesi@0
|
163 * TRUE on success, FALSE on failure.
|
danielebarchiesi@0
|
164 *
|
danielebarchiesi@0
|
165 * @see image_load()
|
danielebarchiesi@0
|
166 * @see image_resize()
|
danielebarchiesi@0
|
167 * @see image_crop()
|
danielebarchiesi@0
|
168 */
|
danielebarchiesi@0
|
169 function image_scale_and_crop(stdClass $image, $width, $height) {
|
danielebarchiesi@0
|
170 $scale = max($width / $image->info['width'], $height / $image->info['height']);
|
danielebarchiesi@0
|
171 $x = ($image->info['width'] * $scale - $width) / 2;
|
danielebarchiesi@0
|
172 $y = ($image->info['height'] * $scale - $height) / 2;
|
danielebarchiesi@0
|
173
|
danielebarchiesi@0
|
174 if (image_resize($image, $image->info['width'] * $scale, $image->info['height'] * $scale)) {
|
danielebarchiesi@0
|
175 return image_crop($image, $x, $y, $width, $height);
|
danielebarchiesi@0
|
176 }
|
danielebarchiesi@0
|
177 return FALSE;
|
danielebarchiesi@0
|
178 }
|
danielebarchiesi@0
|
179
|
danielebarchiesi@0
|
180 /**
|
danielebarchiesi@0
|
181 * Scales image dimensions while maintaining aspect ratio.
|
danielebarchiesi@0
|
182 *
|
danielebarchiesi@0
|
183 * The resulting dimensions can be smaller for one or both target dimensions.
|
danielebarchiesi@0
|
184 *
|
danielebarchiesi@0
|
185 * @param $dimensions
|
danielebarchiesi@0
|
186 * Dimensions to be modified - an array with components width and height, in
|
danielebarchiesi@0
|
187 * pixels.
|
danielebarchiesi@0
|
188 * @param $width
|
danielebarchiesi@0
|
189 * The target width, in pixels. If this value is NULL then the scaling will be
|
danielebarchiesi@0
|
190 * based only on the height value.
|
danielebarchiesi@0
|
191 * @param $height
|
danielebarchiesi@0
|
192 * The target height, in pixels. If this value is NULL then the scaling will
|
danielebarchiesi@0
|
193 * be based only on the width value.
|
danielebarchiesi@0
|
194 * @param $upscale
|
danielebarchiesi@0
|
195 * Boolean indicating that images smaller than the target dimensions will be
|
danielebarchiesi@0
|
196 * scaled up. This generally results in a low quality image.
|
danielebarchiesi@0
|
197 *
|
danielebarchiesi@0
|
198 * @return
|
danielebarchiesi@0
|
199 * TRUE if $dimensions was modified, FALSE otherwise.
|
danielebarchiesi@0
|
200 *
|
danielebarchiesi@0
|
201 * @see image_scale()
|
danielebarchiesi@0
|
202 */
|
danielebarchiesi@0
|
203 function image_dimensions_scale(array &$dimensions, $width = NULL, $height = NULL, $upscale = FALSE) {
|
danielebarchiesi@0
|
204 $aspect = $dimensions['height'] / $dimensions['width'];
|
danielebarchiesi@0
|
205
|
danielebarchiesi@0
|
206 // Calculate one of the dimensions from the other target dimension,
|
danielebarchiesi@0
|
207 // ensuring the same aspect ratio as the source dimensions. If one of the
|
danielebarchiesi@0
|
208 // target dimensions is missing, that is the one that is calculated. If both
|
danielebarchiesi@0
|
209 // are specified then the dimension calculated is the one that would not be
|
danielebarchiesi@0
|
210 // calculated to be bigger than its target.
|
danielebarchiesi@0
|
211 if (($width && !$height) || ($width && $height && $aspect < $height / $width)) {
|
danielebarchiesi@0
|
212 $height = (int) round($width * $aspect);
|
danielebarchiesi@0
|
213 }
|
danielebarchiesi@0
|
214 else {
|
danielebarchiesi@0
|
215 $width = (int) round($height / $aspect);
|
danielebarchiesi@0
|
216 }
|
danielebarchiesi@0
|
217
|
danielebarchiesi@0
|
218 // Don't upscale if the option isn't enabled.
|
danielebarchiesi@0
|
219 if (!$upscale && ($width >= $dimensions['width'] || $height >= $dimensions['height'])) {
|
danielebarchiesi@0
|
220 return FALSE;
|
danielebarchiesi@0
|
221 }
|
danielebarchiesi@0
|
222
|
danielebarchiesi@0
|
223 $dimensions['width'] = $width;
|
danielebarchiesi@0
|
224 $dimensions['height'] = $height;
|
danielebarchiesi@0
|
225 return TRUE;
|
danielebarchiesi@0
|
226 }
|
danielebarchiesi@0
|
227
|
danielebarchiesi@0
|
228 /**
|
danielebarchiesi@0
|
229 * Scales an image while maintaining aspect ratio.
|
danielebarchiesi@0
|
230 *
|
danielebarchiesi@0
|
231 * The resulting image can be smaller for one or both target dimensions.
|
danielebarchiesi@0
|
232 *
|
danielebarchiesi@0
|
233 * @param $image
|
danielebarchiesi@0
|
234 * An image object returned by image_load().
|
danielebarchiesi@0
|
235 * @param $width
|
danielebarchiesi@0
|
236 * The target width, in pixels. If this value is NULL then the scaling will
|
danielebarchiesi@0
|
237 * be based only on the height value.
|
danielebarchiesi@0
|
238 * @param $height
|
danielebarchiesi@0
|
239 * The target height, in pixels. If this value is NULL then the scaling will
|
danielebarchiesi@0
|
240 * be based only on the width value.
|
danielebarchiesi@0
|
241 * @param $upscale
|
danielebarchiesi@0
|
242 * Boolean indicating that files smaller than the dimensions will be scaled
|
danielebarchiesi@0
|
243 * up. This generally results in a low quality image.
|
danielebarchiesi@0
|
244 *
|
danielebarchiesi@0
|
245 * @return
|
danielebarchiesi@0
|
246 * TRUE on success, FALSE on failure.
|
danielebarchiesi@0
|
247 *
|
danielebarchiesi@0
|
248 * @see image_dimensions_scale()
|
danielebarchiesi@0
|
249 * @see image_load()
|
danielebarchiesi@0
|
250 * @see image_scale_and_crop()
|
danielebarchiesi@0
|
251 */
|
danielebarchiesi@0
|
252 function image_scale(stdClass $image, $width = NULL, $height = NULL, $upscale = FALSE) {
|
danielebarchiesi@0
|
253 $dimensions = $image->info;
|
danielebarchiesi@0
|
254
|
danielebarchiesi@0
|
255 // Scale the dimensions - if they don't change then just return success.
|
danielebarchiesi@0
|
256 if (!image_dimensions_scale($dimensions, $width, $height, $upscale)) {
|
danielebarchiesi@0
|
257 return TRUE;
|
danielebarchiesi@0
|
258 }
|
danielebarchiesi@0
|
259
|
danielebarchiesi@0
|
260 return image_resize($image, $dimensions['width'], $dimensions['height']);
|
danielebarchiesi@0
|
261 }
|
danielebarchiesi@0
|
262
|
danielebarchiesi@0
|
263 /**
|
danielebarchiesi@0
|
264 * Resizes an image to the given dimensions (ignoring aspect ratio).
|
danielebarchiesi@0
|
265 *
|
danielebarchiesi@0
|
266 * @param $image
|
danielebarchiesi@0
|
267 * An image object returned by image_load().
|
danielebarchiesi@0
|
268 * @param $width
|
danielebarchiesi@0
|
269 * The target width, in pixels.
|
danielebarchiesi@0
|
270 * @param $height
|
danielebarchiesi@0
|
271 * The target height, in pixels.
|
danielebarchiesi@0
|
272 *
|
danielebarchiesi@0
|
273 * @return
|
danielebarchiesi@0
|
274 * TRUE on success, FALSE on failure.
|
danielebarchiesi@0
|
275 *
|
danielebarchiesi@0
|
276 * @see image_load()
|
danielebarchiesi@0
|
277 * @see image_gd_resize()
|
danielebarchiesi@0
|
278 */
|
danielebarchiesi@0
|
279 function image_resize(stdClass $image, $width, $height) {
|
danielebarchiesi@0
|
280 $width = (int) round($width);
|
danielebarchiesi@0
|
281 $height = (int) round($height);
|
danielebarchiesi@0
|
282
|
danielebarchiesi@0
|
283 return image_toolkit_invoke('resize', $image, array($width, $height));
|
danielebarchiesi@0
|
284 }
|
danielebarchiesi@0
|
285
|
danielebarchiesi@0
|
286 /**
|
danielebarchiesi@0
|
287 * Rotates an image by the given number of degrees.
|
danielebarchiesi@0
|
288 *
|
danielebarchiesi@0
|
289 * @param $image
|
danielebarchiesi@0
|
290 * An image object returned by image_load().
|
danielebarchiesi@0
|
291 * @param $degrees
|
danielebarchiesi@0
|
292 * The number of (clockwise) degrees to rotate the image.
|
danielebarchiesi@0
|
293 * @param $background
|
danielebarchiesi@0
|
294 * An hexadecimal integer specifying the background color to use for the
|
danielebarchiesi@0
|
295 * uncovered area of the image after the rotation. E.g. 0x000000 for black,
|
danielebarchiesi@0
|
296 * 0xff00ff for magenta, and 0xffffff for white. For images that support
|
danielebarchiesi@0
|
297 * transparency, this will default to transparent. Otherwise it will
|
danielebarchiesi@0
|
298 * be white.
|
danielebarchiesi@0
|
299 *
|
danielebarchiesi@0
|
300 * @return
|
danielebarchiesi@0
|
301 * TRUE on success, FALSE on failure.
|
danielebarchiesi@0
|
302 *
|
danielebarchiesi@0
|
303 * @see image_load()
|
danielebarchiesi@0
|
304 * @see image_gd_rotate()
|
danielebarchiesi@0
|
305 */
|
danielebarchiesi@0
|
306 function image_rotate(stdClass $image, $degrees, $background = NULL) {
|
danielebarchiesi@0
|
307 return image_toolkit_invoke('rotate', $image, array($degrees, $background));
|
danielebarchiesi@0
|
308 }
|
danielebarchiesi@0
|
309
|
danielebarchiesi@0
|
310 /**
|
danielebarchiesi@0
|
311 * Crops an image to a rectangle specified by the given dimensions.
|
danielebarchiesi@0
|
312 *
|
danielebarchiesi@0
|
313 * @param $image
|
danielebarchiesi@0
|
314 * An image object returned by image_load().
|
danielebarchiesi@0
|
315 * @param $x
|
danielebarchiesi@0
|
316 * The top left coordinate, in pixels, of the crop area (x axis value).
|
danielebarchiesi@0
|
317 * @param $y
|
danielebarchiesi@0
|
318 * The top left coordinate, in pixels, of the crop area (y axis value).
|
danielebarchiesi@0
|
319 * @param $width
|
danielebarchiesi@0
|
320 * The target width, in pixels.
|
danielebarchiesi@0
|
321 * @param $height
|
danielebarchiesi@0
|
322 * The target height, in pixels.
|
danielebarchiesi@0
|
323 *
|
danielebarchiesi@0
|
324 * @return
|
danielebarchiesi@0
|
325 * TRUE on success, FALSE on failure.
|
danielebarchiesi@0
|
326 *
|
danielebarchiesi@0
|
327 * @see image_load()
|
danielebarchiesi@0
|
328 * @see image_scale_and_crop()
|
danielebarchiesi@0
|
329 * @see image_gd_crop()
|
danielebarchiesi@0
|
330 */
|
danielebarchiesi@0
|
331 function image_crop(stdClass $image, $x, $y, $width, $height) {
|
danielebarchiesi@0
|
332 $aspect = $image->info['height'] / $image->info['width'];
|
danielebarchiesi@0
|
333 if (empty($height)) $height = $width / $aspect;
|
danielebarchiesi@0
|
334 if (empty($width)) $width = $height * $aspect;
|
danielebarchiesi@0
|
335
|
danielebarchiesi@0
|
336 $width = (int) round($width);
|
danielebarchiesi@0
|
337 $height = (int) round($height);
|
danielebarchiesi@0
|
338
|
danielebarchiesi@0
|
339 return image_toolkit_invoke('crop', $image, array($x, $y, $width, $height));
|
danielebarchiesi@0
|
340 }
|
danielebarchiesi@0
|
341
|
danielebarchiesi@0
|
342 /**
|
danielebarchiesi@0
|
343 * Converts an image to grayscale.
|
danielebarchiesi@0
|
344 *
|
danielebarchiesi@0
|
345 * @param $image
|
danielebarchiesi@0
|
346 * An image object returned by image_load().
|
danielebarchiesi@0
|
347 *
|
danielebarchiesi@0
|
348 * @return
|
danielebarchiesi@0
|
349 * TRUE on success, FALSE on failure.
|
danielebarchiesi@0
|
350 *
|
danielebarchiesi@0
|
351 * @see image_load()
|
danielebarchiesi@0
|
352 * @see image_gd_desaturate()
|
danielebarchiesi@0
|
353 */
|
danielebarchiesi@0
|
354 function image_desaturate(stdClass $image) {
|
danielebarchiesi@0
|
355 return image_toolkit_invoke('desaturate', $image);
|
danielebarchiesi@0
|
356 }
|
danielebarchiesi@0
|
357
|
danielebarchiesi@0
|
358 /**
|
danielebarchiesi@0
|
359 * Loads an image file and returns an image object.
|
danielebarchiesi@0
|
360 *
|
danielebarchiesi@0
|
361 * Any changes to the file are not saved until image_save() is called.
|
danielebarchiesi@0
|
362 *
|
danielebarchiesi@0
|
363 * @param $file
|
danielebarchiesi@0
|
364 * Path to an image file.
|
danielebarchiesi@0
|
365 * @param $toolkit
|
danielebarchiesi@0
|
366 * An optional, image toolkit name to override the default.
|
danielebarchiesi@0
|
367 *
|
danielebarchiesi@0
|
368 * @return
|
danielebarchiesi@0
|
369 * An image object or FALSE if there was a problem loading the file. The
|
danielebarchiesi@0
|
370 * image object has the following properties:
|
danielebarchiesi@0
|
371 * - 'source' - The original file path.
|
danielebarchiesi@0
|
372 * - 'info' - The array of information returned by image_get_info()
|
danielebarchiesi@0
|
373 * - 'toolkit' - The name of the image toolkit requested when the image was
|
danielebarchiesi@0
|
374 * loaded.
|
danielebarchiesi@0
|
375 * Image toolkits may add additional properties. The caller is advised not to
|
danielebarchiesi@0
|
376 * monkey about with them.
|
danielebarchiesi@0
|
377 *
|
danielebarchiesi@0
|
378 * @see image_save()
|
danielebarchiesi@0
|
379 * @see image_get_info()
|
danielebarchiesi@0
|
380 * @see image_get_available_toolkits()
|
danielebarchiesi@0
|
381 * @see image_gd_load()
|
danielebarchiesi@0
|
382 */
|
danielebarchiesi@0
|
383 function image_load($file, $toolkit = FALSE) {
|
danielebarchiesi@0
|
384 if (!$toolkit) {
|
danielebarchiesi@0
|
385 $toolkit = image_get_toolkit();
|
danielebarchiesi@0
|
386 }
|
danielebarchiesi@0
|
387 if ($toolkit) {
|
danielebarchiesi@0
|
388 $image = new stdClass();
|
danielebarchiesi@0
|
389 $image->source = $file;
|
danielebarchiesi@0
|
390 $image->info = image_get_info($file, $toolkit);
|
danielebarchiesi@0
|
391 if (isset($image->info) && is_array($image->info)) {
|
danielebarchiesi@0
|
392 $image->toolkit = $toolkit;
|
danielebarchiesi@0
|
393 if (image_toolkit_invoke('load', $image)) {
|
danielebarchiesi@0
|
394 return $image;
|
danielebarchiesi@0
|
395 }
|
danielebarchiesi@0
|
396 }
|
danielebarchiesi@0
|
397 }
|
danielebarchiesi@0
|
398 return FALSE;
|
danielebarchiesi@0
|
399 }
|
danielebarchiesi@0
|
400
|
danielebarchiesi@0
|
401 /**
|
danielebarchiesi@0
|
402 * Closes the image and saves the changes to a file.
|
danielebarchiesi@0
|
403 *
|
danielebarchiesi@0
|
404 * @param $image
|
danielebarchiesi@0
|
405 * An image object returned by image_load(). The object's 'info' property
|
danielebarchiesi@0
|
406 * will be updated if the file is saved successfully.
|
danielebarchiesi@0
|
407 * @param $destination
|
danielebarchiesi@0
|
408 * Destination path where the image should be saved. If it is empty the
|
danielebarchiesi@0
|
409 * original image file will be overwritten.
|
danielebarchiesi@0
|
410 *
|
danielebarchiesi@0
|
411 * @return
|
danielebarchiesi@0
|
412 * TRUE on success, FALSE on failure.
|
danielebarchiesi@0
|
413 *
|
danielebarchiesi@0
|
414 * @see image_load()
|
danielebarchiesi@0
|
415 * @see image_gd_save()
|
danielebarchiesi@0
|
416 */
|
danielebarchiesi@0
|
417 function image_save(stdClass $image, $destination = NULL) {
|
danielebarchiesi@0
|
418 if (empty($destination)) {
|
danielebarchiesi@0
|
419 $destination = $image->source;
|
danielebarchiesi@0
|
420 }
|
danielebarchiesi@0
|
421 if ($return = image_toolkit_invoke('save', $image, array($destination))) {
|
danielebarchiesi@0
|
422 // Clear the cached file size and refresh the image information.
|
danielebarchiesi@0
|
423 clearstatcache();
|
danielebarchiesi@0
|
424 $image->info = image_get_info($destination, $image->toolkit);
|
danielebarchiesi@0
|
425
|
danielebarchiesi@0
|
426 if (drupal_chmod($destination)) {
|
danielebarchiesi@0
|
427 return $return;
|
danielebarchiesi@0
|
428 }
|
danielebarchiesi@0
|
429 }
|
danielebarchiesi@0
|
430 return FALSE;
|
danielebarchiesi@0
|
431 }
|
danielebarchiesi@0
|
432
|
danielebarchiesi@0
|
433 /**
|
danielebarchiesi@0
|
434 * @} End of "defgroup image".
|
danielebarchiesi@0
|
435 */
|