2
// $Id: image.gd.inc,v 1.4 2008/01/15 10:17:42 goba Exp $
6
* GD2 toolkit for image manipulation within Drupal.
15
* Retrieve information about the toolkit.
17
function image_gd_info() {
18
return array('name' => 'gd', 'title' => t('GD2 image manipulation toolkit'));
22
* Retrieve settings for the GD2 toolkit.
24
function image_gd_settings() {
25
if (image_gd_check_settings()) {
27
$form['status'] = array(
28
'#value' => t('The GD toolkit is installed and working properly.')
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.'),
37
'#default_value' => variable_get('image_jpeg_quality', 75),
38
'#field_suffix' => t('%'),
40
$form['#element_validate'] = array('image_gd_settings_validate');
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')));
51
* Validate the submitted GD settings.
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.'));
62
* Verify GD2 settings (that the right version is actually installed).
65
* A boolean indicating if the GD toolkit is avaiable on this machine.
67
function image_gd_check_settings() {
68
if ($check = get_extension_funcs('gd')) {
69
if (in_array('imagegd2', $check)) {
70
// GD2 support is available.
78
* Scale an image to the specified size using GD.
80
function image_gd_resize($source, $destination, $width, $height) {
81
if (!file_exists($source)) {
85
$info = image_get_info($source);
90
$im = image_gd_open($source, $info['extension']);
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);
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);
121
imagecopyresampled($res, $im, 0, 0, 0, 0, $width, $height, $info['width'], $info['height']);
122
$result = image_gd_close($res, $destination, $info['extension']);
131
* Rotate an image the given number of degrees.
133
function image_gd_rotate($source, $destination, $degrees, $background = 0x000000) {
134
if (!function_exists('imageRotate')) {
138
$info = image_get_info($source);
143
$im = image_gd_open($source, $info['extension']);
148
$res = imageRotate($im, $degrees, $background);
149
$result = image_gd_close($res, $destination, $info['extension']);
155
* Crop an image using the GD toolkit.
157
function image_gd_crop($source, $destination, $x, $y, $width, $height) {
158
$info = image_get_info($source);
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']);
175
* GD helper function to create an image resource from a file.
178
* A string file path where the iamge should be saved.
180
* A string containing one of the following extensions: gif, jpg, jpeg, png.
182
* An image resource, or FALSE on error.
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)) {
190
return $open_func($file);
194
* GD helper to write an image resource to a destination file.
197
* An image resource created with image_gd_open().
198
* @param $destination
199
* A string file path where the iamge should be saved.
201
* A string containing one of the following extensions: gif, jpg, jpeg, png.
203
* Boolean indicating success.
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)) {
211
if ($extension == 'jpeg') {
212
return $close_func($res, $destination, variable_get('image_jpeg_quality', 75));
215
return $close_func($res, $destination);
220
* @} End of "ingroup image".