array('title' => 'Built-in GD2 support for Drupal 6')); } /** * Retrieve settings for the GD2 toolkit. */ function gd_settings() { $form = array(); $form['image_jpeg_quality'] = array( '#type' => 'textfield', '#title' => t('JPEG quality'), '#description' => t('Define the image quality for JPEG manipulations. Ranges from 0 to 100. Higher values mean better image quality, but bigger files.'), '#size' => 10, '#maxlength' => 3, '#default_value' => variable_get('image_jpeg_quality', 75), '#field_suffix' => '%', ); return $form; } /** * Scale an image to the specified size using GD. */ function gd_resize($source, $destination, $width, $height) { // TODO: make some validate function for this in image.inc, we need todo this quite often! if (!file_exists($source)) { return FALSE; } // TODO: make some validate function for this in image.inc, we need todo this quite often! $info = image_get_info($source); if (!$info) { return FALSE; } $im = image_gd_open($source, $info['extension']); if (!$im) { return FALSE; } $res = imageCreateTrueColor($width, $height); if ($info['extension'] == 'png') { $transparency = imagecolorallocatealpha($res, 0, 0, 0, 127); imagealphablending($res, FALSE); imagefilledrectangle($res, 0, 0, $width, $height, $transparency); imagealphablending($res, TRUE); imagesavealpha($res, TRUE); } imageCopyResampled($res, $im, 0, 0, 0, 0, $width, $height, $info['width'], $info['height']); $result = image_gd_close($res, $destination, $info['extension']); imageDestroy($res); imageDestroy($im); return $result; } /** * Rotate an image the given number of degrees. */ function gd_rotate($source, $destination, $degrees, $bg_color = 0) { if (!function_exists('imageRotate')) { return FALSE; } $info = image_get_info($source); if (!$info) { return FALSE; } $im = image_gd_open($source, $info['extension']); if (!$im) { return FALSE; } $res = imageRotate($im, $degrees, $bg_color); $result = image_gd_close($res, $destination, $info['extension']); return $result; } /** * Crop an image using the GD toolkit. */ function gd_crop($source, $destination, $x, $y, $width, $height) { $info = image_get_info($source); if (!$info) { return FALSE; } $im = image_gd_open($source, $info['extension']); $res = imageCreateTrueColor($width, $height); imageCopy($res, $im, 0, 0, $x, $y, $width, $height); $result = image_gd_close($res, $destination, $info['extension']); imageDestroy($res); imageDestroy($im); return $result; } /** * GD helper function to create an image resource from a file. */ function gd_open($file, $extension) { $extension = str_replace('jpg', 'jpeg', $extension); $open_func = 'imageCreateFrom'. $extension; if (!function_exists($open_func)) { return FALSE; } return $open_func($file); } /** * GD helper to write an image resource to a destination file. */ function gd_close($res, $destination, $extension) { $extension = str_replace('jpg', 'jpeg', $extension); $close_func = 'image'. $extension; if (!function_exists($close_func)) { return FALSE; } if ($extension == 'jpeg') { return $close_func($res, $destination, variable_get('image_jpeg_quality', 75)); } else { return $close_func($res, $destination); } }