| [ Index ] |
PHP Cross Reference of Drupal 6 (gatewave) |
[Summary view] [Print] [Text view]
1 <?php // $Id: imageapi_gd.module,v 1.13.2.7 2009/04/17 00:15:21 drewish Exp $ 2 3 /** 4 * @file 5 * GD2 toolkit functions 6 */ 7 8 /** 9 * Implementation of hook_imageapi_toolkit(). 10 * 11 * this hook only gets called to see is a module implements the imageapi hooks... 12 */ 13 function imageapi_gd_imageapi_toolkit() { 14 } 15 16 17 /** 18 * Settings form for the toolkit. 19 */ 20 function imageapi_gd_settings_form() { 21 $form['imageapi_jpeg_quality'] = array( 22 '#type' => 'textfield', 23 '#title' => t('JPEG quality'), 24 '#description' => t('Define the image quality for JPEG manipulations. Ranges from 0 to 100. Higher values mean better image quality, but bigger files.'), 25 '#size' => 10, 26 '#maxlength' => 3, 27 '#default_value' => variable_get('imageapi_jpeg_quality', 75), 28 '#field_suffix' => '%', 29 ); 30 return system_settings_form($form); 31 } 32 33 /** 34 * Open an image file. 35 * 36 * @param $image 37 * An image object. The $image->resource value will populated by this call. 38 * @return 39 * TRUE or FALSE, based on success. 40 */ 41 function imageapi_gd_image_open($image) { 42 $extension = str_replace('jpg', 'jpeg', $image->info['extension']); 43 $function = 'imagecreatefrom'. $extension; 44 return (function_exists($function) && $image->resource = $function($image->source)); 45 } 46 47 /** 48 * Save an image file to a destination. 49 * 50 * @param $image 51 * An image object. 52 * @param $destination 53 * A string file path where the image should be saved. 54 * @param $extension 55 * A string containing one of the following extensions: gif, jpg, jpeg, png. 56 * @return 57 * TRUE or FALSE, based on success. 58 */ 59 function imageapi_gd_image_close($image, $destination) { 60 $extension = str_replace('jpg', 'jpeg', $image->info['extension']); 61 $function = 'image'. $extension; 62 if (!function_exists($function)) { 63 return FALSE; 64 } 65 if ($extension == 'jpeg') { 66 return $function($image->resource, $destination, variable_get('imageapi_jpeg_quality', 75)); 67 } 68 else { 69 // Always save PNG images with full transparency. 70 if ($extension == 'png') { 71 imagealphablending($image->resource, FALSE); 72 imagesavealpha($image->resource, TRUE); 73 } 74 return $function($image->resource, $destination); 75 } 76 } 77 78 /** 79 * Crop an image using the GD toolkit. 80 * 81 * @param $image 82 * An image object. The $image->resource, $image->info['width'], and 83 * $image->info['height'] values will be modified by this call. 84 * @param $x 85 * The starting x offset at which to start the crop, in pixels. 86 * @param $y 87 * The starting y offset at which to start the crop, in pixels. 88 * @param $width 89 * The width of the cropped area, in pixels. 90 * @param $height 91 * The height of the cropped area, in pixels. 92 * @return 93 * TRUE or FALSE, based on success. 94 */ 95 function imageapi_gd_image_crop(&$image, $x, $y, $width, $height) { 96 $res = imageapi_gd_create_tmp($image, $width, $height); 97 98 if (!imagecopyresampled($res, $image->resource, 0, 0, $x, $y, $width, $height, $width, $height)) { 99 return FALSE; 100 } 101 102 // Destroy the original image and return the modified image. 103 imagedestroy($image->resource); 104 $image->resource = $res; 105 $image->info['width'] = $width; 106 $image->info['height'] = $height; 107 return TRUE; 108 } 109 110 /** 111 * Scale an image to the specified size using GD. 112 * 113 * @param $image 114 * An image object. The $image->resource, $image->info['width'], and 115 * $image->info['height'] values will be modified by this call. 116 * @param $width 117 * The new width of the resized image, in pixels. 118 * @param $height 119 * The new height of the resized image, in pixels. 120 * @return 121 * TRUE or FALSE, based on success. 122 */ 123 function imageapi_gd_image_resize(&$image, $width, $height) { 124 $res = imageapi_gd_create_tmp($image, $width, $height); 125 126 if (!imagecopyresampled($res, $image->resource, 0, 0, 0, 0, $width, $height, $image->info['width'], $image->info['height'])) { 127 return FALSE; 128 } 129 130 imagedestroy($image->resource); 131 // Update image object. 132 $image->resource = $res; 133 $image->info['width'] = $width; 134 $image->info['height'] = $height; 135 return TRUE; 136 } 137 138 /** 139 * Rotate an image the given number of degrees. 140 * 141 * @param $image 142 * An image object. The $image->resource, $image->info['width'], and 143 * $image->info['height'] values will be modified by this call. 144 * @param $degrees 145 * The number of (clockwise) degrees to rotate the image. 146 * @param $background 147 * An hexadecimal integer specifying the background color to use for the 148 * uncovered area of the image after the rotation. E.g. 0x000000 for black, 149 * 0xff00ff for magenta, and 0xffffff for white. For images that support 150 * transparency, this will default to transparent. Otherwise it will 151 * be white. 152 * @return 153 * TRUE or FALSE, based on success. 154 */ 155 function imageapi_gd_image_rotate(&$image, $degrees, $background) { 156 // PHP installations using non-bundled GD do not have imagerotate. 157 if (!function_exists('imagerotate')) { 158 require_once drupal_get_path('module', 'imageapi_gd') .'/imagerotate.inc'; 159 } 160 161 $width = $image->info['width']; 162 $height = $image->info['height']; 163 164 // Convert the hexadecimal background value to a color index value. 165 if (isset($background)) { 166 $rgb = array(); 167 for ($i = 16; $i >= 0; $i -= 8) { 168 $rgb[] = (($background >> $i) & 0xFF); 169 } 170 $background = imagecolorallocatealpha($image->resource, $rgb[0], $rgb[1], $rgb[2], 0); 171 } 172 // Set the background color as transparent if $background is NULL. 173 else { 174 // Get the current transparent color. 175 $background = imagecolortransparent($image->resource); 176 177 // If no transparent colors, use white. 178 if ($background == 0) { 179 $background = imagecolorallocatealpha($image->resource, 255, 255, 255, 0); 180 } 181 } 182 183 // Images are assigned a new color pallete when rotating, removing any 184 // transparency flags. For GIF images, keep a record of the transparent color. 185 if ($image->info['extension'] == 'gif') { 186 $transparent_index = imagecolortransparent($image->resource); 187 if ($transparent_index != 0) { 188 $transparent_gif_color = imagecolorsforindex($image->resource, $transparent_index); 189 } 190 } 191 192 $image->resource = imagerotate($image->resource, 360 - $degrees, $background); 193 194 // GIFs need to reassign the transparent color after performing the rotate. 195 if (isset($transparent_gif_color)) { 196 $background = imagecolorexactalpha($image->resource, $transparent_gif_color['red'], $transparent_gif_color['green'], $transparent_gif_color['blue'], $transparent_gif_color['alpha']); 197 imagecolortransparent($image->resource, $background); 198 } 199 200 $image->info['width'] = imagesx($image->resource); 201 $image->info['height'] = imagesy($image->resource); 202 return TRUE; 203 } 204 205 function imageapi_gd_image_sharpen(&$image, $radius, $sigma, $amount, $threshold) { 206 $threshold = round($threshold * 255); 207 $image->resource = imageapi_gd_unsharp_mask($image->resource, $radius, $sigma, $amount, $threshold); 208 return TRUE; 209 } 210 211 /** 212 * Convert an image resource to grayscale. 213 * 214 * Note that transparent GIFs loose transparency when desaturated. 215 * 216 * @param $image 217 * An image object. The $image->resource value will be modified by this call. 218 * @return 219 * TRUE or FALSE, based on success. 220 */ 221 function imageapi_gd_image_desaturate(&$image) { 222 // PHP installations using non-bundled GD do not have imagefilter. 223 if (!function_exists('imagefilter')) { 224 require_once drupal_get_path('module', 'imageapi_gd') .'/imagefilter.inc'; 225 } 226 227 return imagefilter($image->resource, IMG_FILTER_GRAYSCALE); 228 } 229 230 /** 231 * Create a truecolor image preserving transparency from a provided image. 232 * 233 * @param $image 234 * An image object. 235 * @param $width 236 * The new width of the new image, in pixels. 237 * @param $height 238 * The new height of the new image, in pixels. 239 * @return 240 * A GD image handle. 241 */ 242 function imageapi_gd_create_tmp($image, $width, $height) { 243 $res = imagecreatetruecolor($width, $height); 244 245 if ($image->info['extension'] == 'gif') { 246 // Grab transparent color index from image resource. 247 $transparent = imagecolortransparent($image->resource); 248 249 if ($transparent >= 0) { 250 // The original must have a transparent color, allocate to the new image. 251 $transparent_color = imagecolorsforindex($image->resource, $transparent); 252 $transparent = imagecolorallocate($res, $transparent_color['red'], $transparent_color['green'], $transparent_color['blue']); 253 254 // Flood with our new transparent color. 255 imagefill($res, 0, 0, $transparent); 256 imagecolortransparent($res, $transparent); 257 } 258 } 259 elseif ($image->info['extension'] == 'png') { 260 imagealphablending($res, FALSE); 261 $transparency = imagecolorallocatealpha($res, 0, 0, 0, 127); 262 imagefill($res, 0, 0, $transparency); 263 imagealphablending($res, TRUE); 264 imagesavealpha($res, TRUE); 265 } 266 else { 267 imagefill($res, 0, 0, imagecolorallocate($res, 255, 255, 255)); 268 } 269 270 return $res; 271 } 272 273 /** 274 * $sigma is currently unused for _gd_sharp_mask due to 3x3 convolution matrix limit. 275 * we should explore properly implementing sigma. 276 */ 277 function imageapi_gd_unsharp_mask($img, $radius, $sigma, $amount, $threshold) { 278 279 ////////////////////////////////////////////////////////////// 280 //// 281 //// Unsharp Mask for PHP - version 2.1.1 282 //// 283 //// Unsharp mask algorithm by Torstein H�nsi 2003-07. 284 //// thoensi_at_netcom_dot_no. 285 //// Please leave this notice. 286 //// 287 ////////////////////////////////////////////////////////////// 288 289 // http://vikjavev.no/computing/ump.php 290 291 // $img is an image that is already created within php using 292 // imgcreatetruecolor. No url! $img must be a truecolor image. 293 294 // Attempt to calibrate the parameters to Photoshop: 295 if ($amount > 500) $amount = 500; 296 $amount = $amount * 0.016; 297 if ($radius > 50) $radius = 50; 298 $radius = $radius * 2; 299 if ($threshold > 255) $threshold = 255; 300 301 $radius = abs(round($radius)); // Only integers make sense. 302 if ($radius == 0) { 303 return $img; imagedestroy($img); 304 break; 305 } 306 307 $w = imagesx($img); $h = imagesy($img); 308 $img_canvas = imagecreatetruecolor($w, $h); 309 $img_blur = imagecreatetruecolor($w, $h); 310 311 // Gaussian blur matrix: 312 // 313 // 1 2 1 314 // 2 4 2 315 // 1 2 1 316 // 317 ////////////////////////////////////////////////// 318 319 $matrix = array( 320 array( 1, 2, 1 ), 321 array( 2, 4, 2 ), 322 array( 1, 2, 1 ) 323 ); 324 325 imagecopy($img_blur, $img, 0, 0, 0, 0, $w, $h); 326 imageconvolution($img_blur, $matrix, 16, 0); 327 328 if ($threshold > 0) { 329 // Calculate the difference between the blurred pixels and the original 330 // and set the pixels 331 for ($x = 0; $x < $w-1; $x++) { // each row 332 for ($y = 0; $y < $h; $y++) { // each pixel 333 334 $rgb_orig = imagecolorat($img, $x, $y); 335 $r_orig = (($rgb_orig >> 16) & 0xFF); 336 $g_orig = (($rgb_orig >> 8) & 0xFF); 337 $b_orig = ($rgb_orig & 0xFF); 338 339 $rgb_blur = imagecolorat($img_blur, $x, $y); 340 341 $r_blur = (($rgb_blur >> 16) & 0xFF); 342 $g_blur = (($rgb_blur >> 8) & 0xFF); 343 $b_blur = ($rgb_blur & 0xFF); 344 345 // When the masked pixels differ less from the original 346 // than the threshold specifies, they are set to their original value. 347 $r_new = (abs($r_orig - $r_blur) >= $threshold) 348 ? max(0, min(255, ($amount * ($r_orig - $r_blur)) + $r_orig)) 349 : $r_orig; 350 $g_new = (abs($g_orig - $g_blur) >= $threshold) 351 ? max(0, min(255, ($amount * ($g_orig - $g_blur)) + $g_orig)) 352 : $g_orig; 353 $b_new = (abs($b_orig - $b_blur) >= $threshold) 354 ? max(0, min(255, ($amount * ($b_orig - $b_blur)) + $b_orig)) 355 : $b_orig; 356 357 if (($r_orig != $r_new) || ($g_orig != $g_new) || ($b_orig != $b_new)) { 358 $pix_col = imagecolorallocate($img, $r_new, $g_new, $b_new); 359 imagesetpixel($img, $x, $y, $pix_col); 360 } 361 } 362 } 363 } 364 else{ 365 for ($x = 0; $x < $w; $x++) { // each row 366 for ($y = 0; $y < $h; $y++) { // each pixel 367 $rgb_orig = imagecolorat($img, $x, $y); 368 $r_orig = (($rgb_orig >> 16) & 0xFF); 369 $g_orig = (($rgb_orig >> 8) & 0xFF); 370 $b_orig = ($rgb_orig & 0xFF); 371 372 $rgb_blur = imagecolorat($img_blur, $x, $y); 373 374 $r_blur = (($rgb_blur >> 16) & 0xFF); 375 $g_blur = (($rgb_blur >> 8) & 0xFF); 376 $b_blur = ($rgb_blur & 0xFF); 377 378 $r_new = ($amount * ($r_orig - $r_blur)) + $r_orig; 379 if ($r_new>255) $r_new=255; 380 elseif ($r_new<0) $r_new=0; 381 382 $g_new = ($amount * ($g_orig - $g_blur)) + $g_orig; 383 if ($g_new>255) $g_new=255; 384 elseif ($g_new<0) $g_new=0; 385 386 $b_new = ($amount * ($b_orig - $b_blur)) + $b_orig; 387 if ($b_new>255) $b_new=255; 388 elseif ($b_new<0) $b_new=0; 389 390 $rgb_new = ($r_new << 16) + ($g_new <<8) + $b_new; 391 imagesetpixel($img, $x, $y, $rgb_new); 392 } 393 } 394 } 395 imagedestroy($img_canvas); 396 imagedestroy($img_blur); 397 398 return $img; 399 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Thu Mar 24 11:18:33 2011 | Cross-referenced by PHPXref 0.7 |