Projet

Général

Profil

Paste
Télécharger (12,8 ko) Statistiques
| Branche: | Révision:

root / drupal7 / modules / system / image.gd.inc @ 73a44071

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
  if (function_exists($function) && $image->resource = $function($image->source)) {
233
    if (imageistruecolor($image->resource)) {
234
      return TRUE;
235
    }
236
    else {
237
      // Convert indexed images to truecolor, copying the image to a new
238
      // truecolor resource, so that filters work correctly and don't result
239
      // in unnecessary dither.
240
      $resource = image_gd_create_tmp($image, $image->info['width'], $image->info['height']);
241
      if ($resource) {
242
        imagecopy($resource, $image->resource, 0, 0, 0, 0, imagesx($resource), imagesy($resource));
243
        imagedestroy($image->resource);
244
        $image->resource = $resource;
245
      }
246
    }
247
    return (bool) $image->resource;
248
  }
249
  return FALSE;
250
}
251

    
252
/**
253
 * GD helper to write an image resource to a destination file.
254
 *
255
 * @param $image
256
 *   An image object.
257
 * @param $destination
258
 *   A string file URI or path where the image should be saved.
259
 * @return
260
 *   TRUE or FALSE, based on success.
261
 *
262
 * @see image_save()
263
 */
264
function image_gd_save(stdClass $image, $destination) {
265
  $scheme = file_uri_scheme($destination);
266
  // Work around lack of stream wrapper support in imagejpeg() and imagepng().
267
  if ($scheme && file_stream_wrapper_valid_scheme($scheme)) {
268
    // If destination is not local, save image to temporary local file.
269
    $local_wrappers = file_get_stream_wrappers(STREAM_WRAPPERS_LOCAL);
270
    if (!isset($local_wrappers[$scheme])) {
271
      $permanent_destination = $destination;
272
      $destination = drupal_tempnam('temporary://', 'gd_');
273
    }
274
    // Convert stream wrapper URI to normal path.
275
    $destination = drupal_realpath($destination);
276
  }
277

    
278
  $extension = str_replace('jpg', 'jpeg', $image->info['extension']);
279
  $function = 'image' . $extension;
280
  if (!function_exists($function)) {
281
    return FALSE;
282
  }
283
  if ($extension == 'jpeg') {
284
    $success = $function($image->resource, $destination, variable_get('image_jpeg_quality', 75));
285
  }
286
  else {
287
    // Always save PNG images with full transparency.
288
    if ($extension == 'png') {
289
      imagealphablending($image->resource, FALSE);
290
      imagesavealpha($image->resource, TRUE);
291
    }
292
    $success = $function($image->resource, $destination);
293
  }
294
  // Move temporary local file to remote destination.
295
  if (isset($permanent_destination) && $success) {
296
    return (bool) file_unmanaged_move($destination, $permanent_destination, FILE_EXISTS_REPLACE);
297
  }
298
  return $success;
299
}
300

    
301
/**
302
 * Create a truecolor image preserving transparency from a provided image.
303
 *
304
 * @param $image
305
 *   An image object.
306
 * @param $width
307
 *   The new width of the new image, in pixels.
308
 * @param $height
309
 *   The new height of the new image, in pixels.
310
 * @return
311
 *   A GD image handle.
312
 */
313
function image_gd_create_tmp(stdClass $image, $width, $height) {
314
  $res = imagecreatetruecolor($width, $height);
315

    
316
  if ($image->info['extension'] == 'gif') {
317
    // Find out if a transparent color is set, will return -1 if no
318
    // transparent color has been defined in the image.
319
    $transparent = imagecolortransparent($image->resource);
320

    
321
    if ($transparent >= 0) {
322
      // Find out the number of colors in the image palette. It will be 0 for
323
      // truecolor images.
324
      $palette_size = imagecolorstotal($image->resource);
325
      if ($palette_size == 0 || $transparent < $palette_size) {
326
        // Set the transparent color in the new resource, either if it is a
327
        // truecolor image or if the transparent color is part of the palette.
328
        // Since the index of the transparency color is a property of the
329
        // image rather than of the palette, it is possible that an image
330
        // could be created with this index set outside the palette size (see
331
        // http://stackoverflow.com/a/3898007).
332
        $transparent_color = imagecolorsforindex($image->resource, $transparent);
333
        $transparent = imagecolorallocate($res, $transparent_color['red'], $transparent_color['green'], $transparent_color['blue']);
334

    
335
        // Flood with our new transparent color.
336
        imagefill($res, 0, 0, $transparent);
337
        imagecolortransparent($res, $transparent);
338
      }
339
      else {
340
        imagefill($res, 0, 0, imagecolorallocate($res, 255, 255, 255));
341
      }
342
    }
343
  }
344
  elseif ($image->info['extension'] == 'png') {
345
    imagealphablending($res, FALSE);
346
    $transparency = imagecolorallocatealpha($res, 0, 0, 0, 127);
347
    imagefill($res, 0, 0, $transparency);
348
    imagealphablending($res, TRUE);
349
    imagesavealpha($res, TRUE);
350
  }
351
  else {
352
    imagefill($res, 0, 0, imagecolorallocate($res, 255, 255, 255));
353
  }
354

    
355
  return $res;
356
}
357

    
358
/**
359
 * Get details about an image.
360
 *
361
 * @param $image
362
 *   An image object.
363
 * @return
364
 *   FALSE, if the file could not be found or is not an image. Otherwise, a
365
 *   keyed array containing information about the image:
366
 *   - "width": Width, in pixels.
367
 *   - "height": Height, in pixels.
368
 *   - "extension": Commonly used file extension for the image.
369
 *   - "mime_type": MIME type ('image/jpeg', 'image/gif', 'image/png').
370
 *
371
 * @see image_get_info()
372
 */
373
function image_gd_get_info(stdClass $image) {
374
  $details = FALSE;
375
  $data = @getimagesize($image->source);
376

    
377
  if (isset($data) && is_array($data)) {
378
    $extensions = array('1' => 'gif', '2' => 'jpg', '3' => 'png');
379
    $extension = isset($extensions[$data[2]]) ?  $extensions[$data[2]] : '';
380
    $details = array(
381
      'width'     => $data[0],
382
      'height'    => $data[1],
383
      'extension' => $extension,
384
      'mime_type' => $data['mime'],
385
    );
386
  }
387

    
388
  return $details;
389
}
390

    
391
/**
392
 * @} End of "addtogroup image".
393
 */