Code coverage for /20080809/modules/system/image.gd.inc

Line #Times calledCode
1
<?php
2
// $Id: image.gd.inc,v 1.2 2008/07/16 21:59:28 dries Exp $
3
4
/**
5
 * @file
6
 * GD2 toolkit for image manipulation within Drupal.
7
 */
8
9
/**
10
 * @ingroup image
11
 * @{
12
 */
13
14
/**
15
 * Retrieve information about the toolkit.
16
 */
172
function image_gd_info() {
180
  return array('name' => 'gd', 'title' => t('GD2 image manipulation
toolkit'));
190
}
20
21
/**
22
 * Retrieve settings for the GD2 toolkit.
23
 */
242
function image_gd_settings() {
250
  if (image_gd_check_settings()) {
260
    $form = array();
270
    $form['status'] = array(
280
      '#markup' => t('The GD toolkit is installed and working properly.')
290
    );
30
310
    $form['image_jpeg_quality'] = array(
320
      '#type' => 'textfield',
330
      '#title' => t('JPEG quality'),
340
      '#description' => t('Define the image quality for JPEG manipulations.
Ranges from 0 to 100. Higher values mean better image quality but bigger
files.'),
350
      '#size' => 10,
360
      '#maxlength' => 3,
370
      '#default_value' => variable_get('image_jpeg_quality', 75),
380
      '#field_suffix' => t('%'),
39
    );
400
    $form['#element_validate'] = array('image_gd_settings_validate');
41
420
    return $form;
430
  }
44
  else {
450
    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')));
460
    return FALSE;
47
  }
480
}
49
50
/**
51
 * Validate the submitted GD settings.
52
 */
532
function image_gd_settings_validate($form, &$form_state) {
54
  // Validate image quality range.
550
  $value = $form_state['values']['image_jpeg_quality'];
560
  if (!is_numeric($value) || $value < 0 || $value > 100) {
570
    form_set_error('image_jpeg_quality', t('JPEG quality must be a number
between 0 and 100.'));
580
  }
590
}
60
61
/**
62
 * Verify GD2 settings (that the right version is actually installed).
63
 *
64
 * @return
65
 *   A boolean indicating if the GD toolkit is avaiable on this machine.
66
 */
672
function image_gd_check_settings() {
680
  if ($check = get_extension_funcs('gd')) {
690
    if (in_array('imagegd2', $check)) {
70
      // GD2 support is available.
710
      return TRUE;
720
    }
730
  }
740
  return FALSE;
750
}
76
77
/**
78
 * Scale an image to the specified size using GD.
79
 */
802
function image_gd_resize($source, $destination, $width, $height) {
811
  if (!file_exists($source)) {
820
    return FALSE;
830
  }
84
851
  $info = image_get_info($source);
861
  if (!$info) {
870
    return FALSE;
880
  }
89
901
  $im = image_gd_open($source, $info['extension']);
911
  if (!$im) {
920
    return FALSE;
930
  }
94
951
  $res = imagecreatetruecolor($width, $height);
961
  if ($info['extension'] == 'png') {
971
    $transparency = imagecolorallocatealpha($res, 0, 0, 0, 127);
981
    imagealphablending($res, FALSE);
991
    imagefilledrectangle($res, 0, 0, $width, $height, $transparency);
1001
    imagealphablending($res, TRUE);
1011
    imagesavealpha($res, TRUE);
1021
  }
1030
  elseif ($info['extension'] == 'gif') {
104
    // If we have a specific transparent color.
1050
    $transparency_index = imagecolortransparent($im);
1060
    if ($transparency_index >= 0) {
107
      // Get the original image's transparent color's RGB values.
1080
      $transparent_color = imagecolorsforindex($im, $transparency_index);
109
      // Allocate the same color in the new image resource.
1100
      $transparency_index = imagecolorallocate($res,
$transparent_color['red'], $transparent_color['green'],
$transparent_color['blue']);
111
      // Completely fill the background of the new image with allocated
color.
1120
      imagefill($res, 0, 0, $transparency_index);
113
      // Set the background color for new image to transparent.
1140
      imagecolortransparent($res, $transparency_index);
115
      // Find number of colors in the images palette.
1160
      $number_colors = imagecolorstotal($im);
117
      // Convert from true color to palette to fix transparency issues.
1180
      imagetruecolortopalette($res, TRUE, $number_colors);
1190
    }
1200
  }
1211
  imagecopyresampled($res, $im, 0, 0, 0, 0, $width, $height,
$info['width'], $info['height']);
1221
  $result = image_gd_close($res, $destination, $info['extension']);
123
1241
  imagedestroy($res);
1251
  imagedestroy($im);
126
1271
  return $result;
1280
}
129
130
/**
131
 * Rotate an image the given number of degrees.
132
 */
1332
function image_gd_rotate($source, $destination, $degrees, $background =
0x000000) {
1340
  if (!function_exists('imageRotate')) {
1350
    return FALSE;
1360
  }
137
1380
  $info = image_get_info($source);
1390
  if (!$info) {
1400
    return FALSE;
1410
  }
142
1430
  $im = image_gd_open($source, $info['extension']);
1440
  if (!$im) {
1450
    return FALSE;
1460
  }
147
1480
  $res = imageRotate($im, $degrees, $background);
1490
  $result = image_gd_close($res, $destination, $info['extension']);
150
1510
  return $result;
1520
}
153
154
/**
155
 * Crop an image using the GD toolkit.
156
 */
1572
function image_gd_crop($source, $destination, $x, $y, $width, $height) {
1580
  $info = image_get_info($source);
1590
  if (!$info) {
1600
    return FALSE;
1610
  }
162
1630
  $im = image_gd_open($source, $info['extension']);
1640
  $res = imageCreateTrueColor($width, $height);
1650
  imageCopy($res, $im, 0, 0, $x, $y, $width, $height);
1660
  $result = image_gd_close($res, $destination, $info['extension']);
167
1680
  imageDestroy($res);
1690
  imageDestroy($im);
170
1710
  return $result;
1720
}
173
174
/**
175
 * GD helper function to create an image resource from a file.
176
 *
177
 * @param $file
178
 *   A string file path where the iamge should be saved.
179
 * @param $extension
180
 *   A string containing one of the following extensions: gif, jpg, jpeg,
png.
181
 * @return
182
 *   An image resource, or FALSE on error.
183
 */
1842
function image_gd_open($file, $extension) {
1851
  $extension = str_replace('jpg', 'jpeg', $extension);
1861
  $open_func = 'imageCreateFrom' . $extension;
1871
  if (!function_exists($open_func)) {
1880
    return FALSE;
1890
  }
1901
  return $open_func($file);
1910
}
192
193
/**
194
 * GD helper to write an image resource to a destination file.
195
 *
196
 * @param $res
197
 *   An image resource created with image_gd_open().
198
 * @param $destination
199
 *   A string file path where the iamge should be saved.
200
 * @param $extension
201
 *   A string containing one of the following extensions: gif, jpg, jpeg,
png.
202
 * @return
203
 *   Boolean indicating success.
204
 */
2052
function image_gd_close($res, $destination, $extension) {
2061
  $extension = str_replace('jpg', 'jpeg', $extension);
2071
  $close_func = 'image' . $extension;
2081
  if (!function_exists($close_func)) {
2090
    return FALSE;
2100
  }
2111
  if ($extension == 'jpeg') {
2120
    return $close_func($res, $destination,
variable_get('image_jpeg_quality', 75));
2130
  }
214
  else {
2151
    return $close_func($res, $destination);
216
  }
2170
}
218
219
/**
220
 * @} End of "ingroup image".
221
 */
2222