| [ Index ] |
PHP Cross Reference of Drupal 6 (yi-drupal) |
[Summary view] [Print] [Text view]
1 <?php 2 3 /** 4 * @file 5 * GD2 toolkit for image manipulation within Drupal. 6 */ 7 8 /** 9 * @ingroup image 10 * @{ 11 */ 12 13 /** 14 * Retrieve information about the toolkit. 15 */ 16 function image_gd_info() { 17 return array('name' => 'gd', 'title' => t('GD2 image manipulation toolkit')); 18 } 19 20 /** 21 * Retrieve settings for the GD2 toolkit. 22 */ 23 function image_gd_settings() { 24 if (image_gd_check_settings()) { 25 $form = array(); 26 $form['status'] = array( 27 '#value' => t('The GD toolkit is installed and working properly.') 28 ); 29 30 $form['image_jpeg_quality'] = array( 31 '#type' => 'textfield', 32 '#title' => t('JPEG quality'), 33 '#description' => t('Define the image quality for JPEG manipulations. Ranges from 0 to 100. Higher values mean better image quality but bigger files.'), 34 '#size' => 10, 35 '#maxlength' => 3, 36 '#default_value' => variable_get('image_jpeg_quality', 75), 37 '#field_suffix' => t('%'), 38 ); 39 $form['#element_validate'] = array('image_gd_settings_validate'); 40 41 return $form; 42 } 43 else { 44 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'))); 45 return FALSE; 46 } 47 } 48 49 /** 50 * Validate the submitted GD settings. 51 */ 52 function image_gd_settings_validate($form, &$form_state) { 53 // Validate image quality range. 54 $value = $form_state['values']['image_jpeg_quality']; 55 if (!is_numeric($value) || $value < 0 || $value > 100) { 56 form_set_error('image_jpeg_quality', t('JPEG quality must be a number between 0 and 100.')); 57 } 58 } 59 60 /** 61 * Verify GD2 settings (that the right version is actually installed). 62 * 63 * @return 64 * A boolean indicating if the GD toolkit is avaiable on this machine. 65 */ 66 function image_gd_check_settings() { 67 if ($check = get_extension_funcs('gd')) { 68 if (in_array('imagegd2', $check)) { 69 // GD2 support is available. 70 return TRUE; 71 } 72 } 73 return FALSE; 74 } 75 76 /** 77 * Scale an image to the specified size using GD. 78 */ 79 function image_gd_resize($source, $destination, $width, $height) { 80 if (!file_exists($source)) { 81 return FALSE; 82 } 83 84 $info = image_get_info($source); 85 if (!$info) { 86 return FALSE; 87 } 88 89 $im = image_gd_open($source, $info['extension']); 90 if (!$im) { 91 return FALSE; 92 } 93 94 $res = imagecreatetruecolor($width, $height); 95 if ($info['extension'] == 'png') { 96 $transparency = imagecolorallocatealpha($res, 0, 0, 0, 127); 97 imagealphablending($res, FALSE); 98 imagefilledrectangle($res, 0, 0, $width, $height, $transparency); 99 imagealphablending($res, TRUE); 100 imagesavealpha($res, TRUE); 101 } 102 elseif ($info['extension'] == 'gif') { 103 // If we have a specific transparent color. 104 $transparency_index = imagecolortransparent($im); 105 if ($transparency_index >= 0) { 106 // Get the original image's transparent color's RGB values. 107 $transparent_color = imagecolorsforindex($im, $transparency_index); 108 // Allocate the same color in the new image resource. 109 $transparency_index = imagecolorallocate($res, $transparent_color['red'], $transparent_color['green'], $transparent_color['blue']); 110 // Completely fill the background of the new image with allocated color. 111 imagefill($res, 0, 0, $transparency_index); 112 // Set the background color for new image to transparent. 113 imagecolortransparent($res, $transparency_index); 114 // Find number of colors in the images palette. 115 $number_colors = imagecolorstotal($im); 116 // Convert from true color to palette to fix transparency issues. 117 imagetruecolortopalette($res, TRUE, $number_colors); 118 } 119 } 120 imagecopyresampled($res, $im, 0, 0, 0, 0, $width, $height, $info['width'], $info['height']); 121 $result = image_gd_close($res, $destination, $info['extension']); 122 123 imagedestroy($res); 124 imagedestroy($im); 125 126 return $result; 127 } 128 129 /** 130 * Rotate an image the given number of degrees. 131 */ 132 function image_gd_rotate($source, $destination, $degrees, $background = 0x000000) { 133 if (!function_exists('imageRotate')) { 134 return FALSE; 135 } 136 137 $info = image_get_info($source); 138 if (!$info) { 139 return FALSE; 140 } 141 142 $im = image_gd_open($source, $info['extension']); 143 if (!$im) { 144 return FALSE; 145 } 146 147 $res = imageRotate($im, $degrees, $background); 148 $result = image_gd_close($res, $destination, $info['extension']); 149 150 return $result; 151 } 152 153 /** 154 * Crop an image using the GD toolkit. 155 */ 156 function image_gd_crop($source, $destination, $x, $y, $width, $height) { 157 $info = image_get_info($source); 158 if (!$info) { 159 return FALSE; 160 } 161 162 $im = image_gd_open($source, $info['extension']); 163 $res = imageCreateTrueColor($width, $height); 164 imageCopy($res, $im, 0, 0, $x, $y, $width, $height); 165 $result = image_gd_close($res, $destination, $info['extension']); 166 167 imageDestroy($res); 168 imageDestroy($im); 169 170 return $result; 171 } 172 173 /** 174 * GD helper function to create an image resource from a file. 175 * 176 * @param $file 177 * A string file path where the iamge should be saved. 178 * @param $extension 179 * A string containing one of the following extensions: gif, jpg, jpeg, png. 180 * @return 181 * An image resource, or FALSE on error. 182 */ 183 function image_gd_open($file, $extension) { 184 $extension = str_replace('jpg', 'jpeg', $extension); 185 $open_func = 'imageCreateFrom'. $extension; 186 if (!function_exists($open_func)) { 187 return FALSE; 188 } 189 return $open_func($file); 190 } 191 192 /** 193 * GD helper to write an image resource to a destination file. 194 * 195 * @param $res 196 * An image resource created with image_gd_open(). 197 * @param $destination 198 * A string file path where the iamge should be saved. 199 * @param $extension 200 * A string containing one of the following extensions: gif, jpg, jpeg, png. 201 * @return 202 * Boolean indicating success. 203 */ 204 function image_gd_close($res, $destination, $extension) { 205 $extension = str_replace('jpg', 'jpeg', $extension); 206 $close_func = 'image'. $extension; 207 if (!function_exists($close_func)) { 208 return FALSE; 209 } 210 if ($extension == 'jpeg') { 211 return $close_func($res, $destination, variable_get('image_jpeg_quality', 75)); 212 } 213 else { 214 return $close_func($res, $destination); 215 } 216 } 217 218 /** 219 * @} End of "ingroup image". 220 */
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Mon Jul 9 18:01:44 2012 | Cross-referenced by PHPXref 0.7 |