Projet

Général

Profil

Paste
Télécharger (11,5 ko) Statistiques
| Branche: | Révision:

root / drupal7 / modules / system / image.gd.inc @ 6a4d64c4

1
<?php
2

    
3
/**
4
 * @file
5
 * GD2 toolkit for image manipulation within Drupal.
6
 */
7

    
8
/**
9
 * @addtogroup image
10
 * @{
11
 */
12

    
13
/**
14
 * Retrieve settings for the GD2 toolkit.
15
 */
16
function image_gd_settings() {
17
  if (image_gd_check_settings()) {
18
    $form['status'] = array(
19
      '#markup' => t('The GD toolkit is installed and working properly.')
20
    );
21

    
22
    $form['image_jpeg_quality'] = array(
23
      '#type' => 'textfield',
24
      '#title' => t('JPEG quality'),
25
      '#description' => t('Define the image quality for JPEG manipulations. Ranges from 0 to 100. Higher values mean better image quality but bigger files.'),
26
      '#size' => 10,
27
      '#maxlength' => 3,
28
      '#default_value' => variable_get('image_jpeg_quality', 75),
29
      '#field_suffix' => t('%'),
30
    );
31
    $form['#element_validate'] = array('image_gd_settings_validate');
32

    
33
    return $form;
34
  }
35
  else {
36
    form_set_error('image_toolkit', t('The GD image toolkit requires that the GD module for PHP be installed and configured properly. For more information see <a href="@url">PHP\'s image documentation</a>.', array('@url' => 'http://php.net/image')));
37
    return FALSE;
38
  }
39
}
40

    
41
/**
42
 * Validate the submitted GD settings.
43
 */
44
function image_gd_settings_validate($form, &$form_state) {
45
  // Validate image quality range.
46
  $value = $form_state['values']['image_jpeg_quality'];
47
  if (!is_numeric($value) || $value < 0 || $value > 100) {
48
    form_set_error('image_jpeg_quality', t('JPEG quality must be a number between 0 and 100.'));
49
  }
50
}
51

    
52
/**
53
 * Verify GD2 settings (that the right version is actually installed).
54
 *
55
 * @return
56
 *   A boolean indicating if the GD toolkit is available on this machine.
57
 */
58
function image_gd_check_settings() {
59
  // GD2 support is available.
60
  return function_exists('imagegd2');
61
}
62

    
63
/**
64
 * Scale an image to the specified size using GD.
65
 *
66
 * @param $image
67
 *   An image object. The $image->resource, $image->info['width'], and
68
 *   $image->info['height'] values will be modified by this call.
69
 * @param $width
70
 *   The new width of the resized image, in pixels.
71
 * @param $height
72
 *   The new height of the resized image, in pixels.
73
 * @return
74
 *   TRUE or FALSE, based on success.
75
 *
76
 * @see image_resize()
77
 */
78
function image_gd_resize(stdClass $image, $width, $height) {
79
  $res = image_gd_create_tmp($image, $width, $height);
80

    
81
  if (!imagecopyresampled($res, $image->resource, 0, 0, 0, 0, $width, $height, $image->info['width'], $image->info['height'])) {
82
    return FALSE;
83
  }
84

    
85
  imagedestroy($image->resource);
86
  // Update image object.
87
  $image->resource = $res;
88
  $image->info['width'] = $width;
89
  $image->info['height'] = $height;
90
  return TRUE;
91
}
92

    
93
/**
94
 * Rotate an image the given number of degrees.
95
 *
96
 * @param $image
97
 *   An image object. The $image->resource, $image->info['width'], and
98
 *   $image->info['height'] values will be modified by this call.
99
 * @param $degrees
100
 *   The number of (clockwise) degrees to rotate the image.
101
 * @param $background
102
 *   An hexadecimal integer specifying the background color to use for the
103
 *   uncovered area of the image after the rotation. E.g. 0x000000 for black,
104
 *   0xff00ff for magenta, and 0xffffff for white. For images that support
105
 *   transparency, this will default to transparent. Otherwise it will
106
 *   be white.
107
 * @return
108
 *   TRUE or FALSE, based on success.
109
 *
110
 * @see image_rotate()
111
 */
112
function image_gd_rotate(stdClass $image, $degrees, $background = NULL) {
113
  // PHP installations using non-bundled GD do not have imagerotate.
114
  if (!function_exists('imagerotate')) {
115
    watchdog('image', 'The image %file could not be rotated because the imagerotate() function is not available in this PHP installation.', array('%file' => $image->source));
116
    return FALSE;
117
  }
118

    
119
  $width = $image->info['width'];
120
  $height = $image->info['height'];
121

    
122
  // Convert the hexadecimal background value to a color index value.
123
  if (isset($background)) {
124
    $rgb = array();
125
    for ($i = 16; $i >= 0; $i -= 8) {
126
      $rgb[] = (($background >> $i) & 0xFF);
127
    }
128
    $background = imagecolorallocatealpha($image->resource, $rgb[0], $rgb[1], $rgb[2], 0);
129
  }
130
  // Set the background color as transparent if $background is NULL.
131
  else {
132
    // Get the current transparent color.
133
    $background = imagecolortransparent($image->resource);
134

    
135
    // If no transparent colors, use white.
136
    if ($background == 0) {
137
      $background = imagecolorallocatealpha($image->resource, 255, 255, 255, 0);
138
    }
139
  }
140

    
141
  // Images are assigned a new color palette when rotating, removing any
142
  // transparency flags. For GIF images, keep a record of the transparent color.
143
  if ($image->info['extension'] == 'gif') {
144
    $transparent_index = imagecolortransparent($image->resource);
145
    if ($transparent_index != 0) {
146
      $transparent_gif_color = imagecolorsforindex($image->resource, $transparent_index);
147
    }
148
  }
149

    
150
  $image->resource = imagerotate($image->resource, 360 - $degrees, $background);
151

    
152
  // GIFs need to reassign the transparent color after performing the rotate.
153
  if (isset($transparent_gif_color)) {
154
    $background = imagecolorexactalpha($image->resource, $transparent_gif_color['red'], $transparent_gif_color['green'], $transparent_gif_color['blue'], $transparent_gif_color['alpha']);
155
    imagecolortransparent($image->resource, $background);
156
  }
157

    
158
  $image->info['width'] = imagesx($image->resource);
159
  $image->info['height'] = imagesy($image->resource);
160
  return TRUE;
161
}
162

    
163
/**
164
 * Crop an image using the GD toolkit.
165
 *
166
 * @param $image
167
 *   An image object. The $image->resource, $image->info['width'], and
168
 *   $image->info['height'] values will be modified by this call.
169
 * @param $x
170
 *   The starting x offset at which to start the crop, in pixels.
171
 * @param $y
172
 *   The starting y offset at which to start the crop, in pixels.
173
 * @param $width
174
 *   The width of the cropped area, in pixels.
175
 * @param $height
176
 *   The height of the cropped area, in pixels.
177
 * @return
178
 *   TRUE or FALSE, based on success.
179
 *
180
 * @see image_crop()
181
 */
182
function image_gd_crop(stdClass $image, $x, $y, $width, $height) {
183
  $res = image_gd_create_tmp($image, $width, $height);
184

    
185
  if (!imagecopyresampled($res, $image->resource, 0, 0, $x, $y, $width, $height, $width, $height)) {
186
    return FALSE;
187
  }
188

    
189
  // Destroy the original image and return the modified image.
190
  imagedestroy($image->resource);
191
  $image->resource = $res;
192
  $image->info['width'] = $width;
193
  $image->info['height'] = $height;
194
  return TRUE;
195
}
196

    
197
/**
198
 * Convert an image resource to grayscale.
199
 *
200
 * Note that transparent GIFs loose transparency when desaturated.
201
 *
202
 * @param $image
203
 *   An image object. The $image->resource value will be modified by this call.
204
 * @return
205
 *   TRUE or FALSE, based on success.
206
 *
207
 * @see image_desaturate()
208
 */
209
function image_gd_desaturate(stdClass $image) {
210
  // PHP installations using non-bundled GD do not have imagefilter.
211
  if (!function_exists('imagefilter')) {
212
    watchdog('image', 'The image %file could not be desaturated because the imagefilter() function is not available in this PHP installation.', array('%file' => $image->source));
213
    return FALSE;
214
  }
215

    
216
  return imagefilter($image->resource, IMG_FILTER_GRAYSCALE);
217
}
218

    
219
/**
220
 * GD helper function to create an image resource from a file.
221
 *
222
 * @param $image
223
 *   An image object. The $image->resource value will populated by this call.
224
 * @return
225
 *   TRUE or FALSE, based on success.
226
 *
227
 * @see image_load()
228
 */
229
function image_gd_load(stdClass $image) {
230
  $extension = str_replace('jpg', 'jpeg', $image->info['extension']);
231
  $function = 'imagecreatefrom' . $extension;
232
  return (function_exists($function) && $image->resource = $function($image->source));
233
}
234

    
235
/**
236
 * GD helper to write an image resource to a destination file.
237
 *
238
 * @param $image
239
 *   An image object.
240
 * @param $destination
241
 *   A string file URI or path where the image should be saved.
242
 * @return
243
 *   TRUE or FALSE, based on success.
244
 *
245
 * @see image_save()
246
 */
247
function image_gd_save(stdClass $image, $destination) {
248
  $scheme = file_uri_scheme($destination);
249
  // Work around lack of stream wrapper support in imagejpeg() and imagepng().
250
  if ($scheme && file_stream_wrapper_valid_scheme($scheme)) {
251
    // If destination is not local, save image to temporary local file.
252
    $local_wrappers = file_get_stream_wrappers(STREAM_WRAPPERS_LOCAL);
253
    if (!isset($local_wrappers[$scheme])) {
254
      $permanent_destination = $destination;
255
      $destination = drupal_tempnam('temporary://', 'gd_');
256
    }
257
    // Convert stream wrapper URI to normal path.
258
    $destination = drupal_realpath($destination);
259
  }
260

    
261
  $extension = str_replace('jpg', 'jpeg', $image->info['extension']);
262
  $function = 'image' . $extension;
263
  if (!function_exists($function)) {
264
    return FALSE;
265
  }
266
  if ($extension == 'jpeg') {
267
    $success = $function($image->resource, $destination, variable_get('image_jpeg_quality', 75));
268
  }
269
  else {
270
    // Always save PNG images with full transparency.
271
    if ($extension == 'png') {
272
      imagealphablending($image->resource, FALSE);
273
      imagesavealpha($image->resource, TRUE);
274
    }
275
    $success = $function($image->resource, $destination);
276
  }
277
  // Move temporary local file to remote destination.
278
  if (isset($permanent_destination) && $success) {
279
    return (bool) file_unmanaged_move($destination, $permanent_destination, FILE_EXISTS_REPLACE);
280
  }
281
  return $success;
282
}
283

    
284
/**
285
 * Create a truecolor image preserving transparency from a provided image.
286
 *
287
 * @param $image
288
 *   An image object.
289
 * @param $width
290
 *   The new width of the new image, in pixels.
291
 * @param $height
292
 *   The new height of the new image, in pixels.
293
 * @return
294
 *   A GD image handle.
295
 */
296
function image_gd_create_tmp(stdClass $image, $width, $height) {
297
  $res = imagecreatetruecolor($width, $height);
298

    
299
  if ($image->info['extension'] == 'gif') {
300
    // Grab transparent color index from image resource.
301
    $transparent = imagecolortransparent($image->resource);
302

    
303
    if ($transparent >= 0) {
304
      // The original must have a transparent color, allocate to the new image.
305
      $transparent_color = imagecolorsforindex($image->resource, $transparent);
306
      $transparent = imagecolorallocate($res, $transparent_color['red'], $transparent_color['green'], $transparent_color['blue']);
307

    
308
      // Flood with our new transparent color.
309
      imagefill($res, 0, 0, $transparent);
310
      imagecolortransparent($res, $transparent);
311
    }
312
  }
313
  elseif ($image->info['extension'] == 'png') {
314
    imagealphablending($res, FALSE);
315
    $transparency = imagecolorallocatealpha($res, 0, 0, 0, 127);
316
    imagefill($res, 0, 0, $transparency);
317
    imagealphablending($res, TRUE);
318
    imagesavealpha($res, TRUE);
319
  }
320
  else {
321
    imagefill($res, 0, 0, imagecolorallocate($res, 255, 255, 255));
322
  }
323

    
324
  return $res;
325
}
326

    
327
/**
328
 * Get details about an image.
329
 *
330
 * @param $image
331
 *   An image object.
332
 * @return
333
 *   FALSE, if the file could not be found or is not an image. Otherwise, a
334
 *   keyed array containing information about the image:
335
 *   - "width": Width, in pixels.
336
 *   - "height": Height, in pixels.
337
 *   - "extension": Commonly used file extension for the image.
338
 *   - "mime_type": MIME type ('image/jpeg', 'image/gif', 'image/png').
339
 *
340
 * @see image_get_info()
341
 */
342
function image_gd_get_info(stdClass $image) {
343
  $details = FALSE;
344
  $data = @getimagesize($image->source);
345

    
346
  if (isset($data) && is_array($data)) {
347
    $extensions = array('1' => 'gif', '2' => 'jpg', '3' => 'png');
348
    $extension = isset($extensions[$data[2]]) ?  $extensions[$data[2]] : '';
349
    $details = array(
350
      'width'     => $data[0],
351
      'height'    => $data[1],
352
      'extension' => $extension,
353
      'mime_type' => $data['mime'],
354
    );
355
  }
356

    
357
  return $details;
358
}
359

    
360
/**
361
 * @} End of "addtogroup image".
362
 */