~spreadubuntu/spreadubuntu/devel-drupal6

« back to all changes in this revision

Viewing changes to includes/image.gd.inc

  • Committer: ruben
  • Date: 2009-06-08 09:38:49 UTC
  • Revision ID: ruben@captive-20090608093849-s1qtsyctv2vwp1x1
SpreadUbuntu moving to Drupal6. Based on ubuntu-drupal theme and adding our modules

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
// $Id: image.gd.inc,v 1.4 2008/01/15 10:17:42 goba 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
 */
 
17
function image_gd_info() {
 
18
  return array('name' => 'gd', 'title' => t('GD2 image manipulation toolkit'));
 
19
}
 
20
 
 
21
/**
 
22
 * Retrieve settings for the GD2 toolkit.
 
23
 */
 
24
function image_gd_settings() {
 
25
  if (image_gd_check_settings()) {
 
26
    $form = array();
 
27
    $form['status'] = array(
 
28
      '#value' => t('The GD toolkit is installed and working properly.')
 
29
    );
 
30
 
 
31
    $form['image_jpeg_quality'] = array(
 
32
      '#type' => 'textfield',
 
33
      '#title' => t('JPEG quality'),
 
34
      '#description' => t('Define the image quality for JPEG manipulations. Ranges from 0 to 100. Higher values mean better image quality but bigger files.'),
 
35
      '#size' => 10,
 
36
      '#maxlength' => 3,
 
37
      '#default_value' => variable_get('image_jpeg_quality', 75),
 
38
      '#field_suffix' => t('%'),
 
39
    );
 
40
    $form['#element_validate'] = array('image_gd_settings_validate');
 
41
    
 
42
    return $form;
 
43
  }
 
44
  else {
 
45
    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')));
 
46
    return FALSE;
 
47
  }
 
48
}
 
49
 
 
50
/**
 
51
 * Validate the submitted GD settings.
 
52
 */
 
53
function image_gd_settings_validate($form, &$form_state) {
 
54
  // Validate image quality range.
 
55
  $value = $form_state['values']['image_jpeg_quality'];
 
56
  if (!is_numeric($value) || $value < 0 || $value > 100) {
 
57
    form_set_error('image_jpeg_quality', t('JPEG quality must be a number between 0 and 100.'));
 
58
  }
 
59
}
 
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
 */
 
67
function image_gd_check_settings() {
 
68
  if ($check = get_extension_funcs('gd')) {
 
69
    if (in_array('imagegd2', $check)) {
 
70
      // GD2 support is available.
 
71
      return TRUE;
 
72
    }
 
73
  }
 
74
  return FALSE;
 
75
}
 
76
 
 
77
/**
 
78
 * Scale an image to the specified size using GD.
 
79
 */
 
80
function image_gd_resize($source, $destination, $width, $height) {
 
81
  if (!file_exists($source)) {
 
82
    return FALSE;
 
83
  }
 
84
 
 
85
  $info = image_get_info($source);
 
86
  if (!$info) {
 
87
    return FALSE;
 
88
  }
 
89
 
 
90
  $im = image_gd_open($source, $info['extension']);
 
91
  if (!$im) {
 
92
    return FALSE;
 
93
  }
 
94
 
 
95
  $res = imagecreatetruecolor($width, $height);
 
96
  if ($info['extension'] == 'png') {
 
97
    $transparency = imagecolorallocatealpha($res, 0, 0, 0, 127);
 
98
    imagealphablending($res, FALSE);
 
99
    imagefilledrectangle($res, 0, 0, $width, $height, $transparency);
 
100
    imagealphablending($res, TRUE);
 
101
    imagesavealpha($res, TRUE);
 
102
  }
 
103
  elseif ($info['extension'] == 'gif') {
 
104
    // If we have a specific transparent color.
 
105
    $transparency_index = imagecolortransparent($im);
 
106
    if ($transparency_index >= 0) {
 
107
      // Get the original image's transparent color's RGB values.
 
108
      $transparent_color = imagecolorsforindex($im, $transparency_index);
 
109
      // Allocate the same color in the new image resource.
 
110
      $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.
 
112
      imagefill($res, 0, 0, $transparency_index);
 
113
      // Set the background color for new image to transparent.
 
114
      imagecolortransparent($res, $transparency_index);
 
115
      // Find number of colors in the images palette.
 
116
      $number_colors = imagecolorstotal($im);
 
117
      // Convert from true color to palette to fix transparency issues.
 
118
      imagetruecolortopalette($res, TRUE, $number_colors);
 
119
    }
 
120
  }
 
121
  imagecopyresampled($res, $im, 0, 0, 0, 0, $width, $height, $info['width'], $info['height']);
 
122
  $result = image_gd_close($res, $destination, $info['extension']);
 
123
 
 
124
  imagedestroy($res);
 
125
  imagedestroy($im);
 
126
 
 
127
  return $result;
 
128
}
 
129
 
 
130
/**
 
131
 * Rotate an image the given number of degrees.
 
132
 */
 
133
function image_gd_rotate($source, $destination, $degrees, $background = 0x000000) {
 
134
  if (!function_exists('imageRotate')) {
 
135
    return FALSE;
 
136
  }
 
137
 
 
138
  $info = image_get_info($source);
 
139
  if (!$info) {
 
140
    return FALSE;
 
141
  }
 
142
 
 
143
  $im = image_gd_open($source, $info['extension']);
 
144
  if (!$im) {
 
145
    return FALSE;
 
146
  }
 
147
 
 
148
  $res = imageRotate($im, $degrees, $background);
 
149
  $result = image_gd_close($res, $destination, $info['extension']);
 
150
 
 
151
  return $result;
 
152
}
 
153
 
 
154
/**
 
155
 * Crop an image using the GD toolkit.
 
156
 */
 
157
function image_gd_crop($source, $destination, $x, $y, $width, $height) {
 
158
  $info = image_get_info($source);
 
159
  if (!$info) {
 
160
    return FALSE;
 
161
  }
 
162
 
 
163
  $im = image_gd_open($source, $info['extension']);
 
164
  $res = imageCreateTrueColor($width, $height);
 
165
  imageCopy($res, $im, 0, 0, $x, $y, $width, $height);
 
166
  $result = image_gd_close($res, $destination, $info['extension']);
 
167
 
 
168
  imageDestroy($res);
 
169
  imageDestroy($im);
 
170
 
 
171
  return $result;
 
172
}
 
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
 */
 
184
function image_gd_open($file, $extension) {
 
185
  $extension = str_replace('jpg', 'jpeg', $extension);
 
186
  $open_func = 'imageCreateFrom'. $extension;
 
187
  if (!function_exists($open_func)) {
 
188
    return FALSE;
 
189
  }
 
190
  return $open_func($file);
 
191
}
 
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
 */
 
205
function image_gd_close($res, $destination, $extension) {
 
206
  $extension = str_replace('jpg', 'jpeg', $extension);
 
207
  $close_func = 'image'. $extension;
 
208
  if (!function_exists($close_func)) {
 
209
    return FALSE;
 
210
  }
 
211
  if ($extension == 'jpeg') {
 
212
    return $close_func($res, $destination, variable_get('image_jpeg_quality', 75));
 
213
  }
 
214
  else {
 
215
    return $close_func($res, $destination);
 
216
  }
 
217
}
 
218
 
 
219
/**
 
220
 * @} End of "ingroup image".
 
221
 */