'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 system_settings_form($form); } function imageapi_gd_image_open($image) { if ($image->res = _gd_open($image->source, $image->info['extension'])) { return $image; } return false; } function imageapi_gd_image_close($image, $destination) { return _gd_close($image->res, $destination, $image->info['extension']); } function imageapi_gd_image_crop(&$image, $x, $y, $width, $height) { $res = _gd_create_tmp($image, $width, $height); if (!imagecopyresampled($res, $image->res, 0, 0, $x, $y, $width, $height, $width, $height)) { return false; } // destroy the original image and return the modified image. imagedestroy($image->res); $image->res = $res; $image->info['width'] = $width; $image->info['height'] = $height; return true; } function imageapi_gd_image_resize(&$image, $width, $height) { $res = _gd_create_tmp($image, $width, $height); if (!imagecopyresampled($res, $image->res, 0, 0, 0, 0, $width, $height, $image->info['width'], $image->info['height'])) { return false; } imagedestroy($image->res); // update image object $image->res = $res; $image->info['width'] = $width; $image->info['height'] = $height; return true; } function imageapi_gd_image_rotate(&$image, $degrees, $bgcolor) { $res = imagerotate($image->res, 360 - $degrees, $bgcolor); imagedestroy($image->res); $image->res = $res; return true; } function imageapi_gd_image_sharpen(&$image, $radius, $sigma, $amount, $threshold) { $threshold = round($threshold * 255); $image->res = _gd_unsharp_mask($image->res, $radius, $sigma, $amount, $threshold); return true; } function imageapi_gd_image_desaturate(&$image) { return imagefilter($image->res, IMG_FILTER_GRAYSCALE); } /** * 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); } } if (!function_exists('imagerotate')) { require_once drupal_get_path('module', 'imageapi') .'/imagerotate.inc'; } if (!function_exists('imagefilter')) { require_once drupal_get_path('module', 'imageapi') .'/imagefilter.inc'; } /** * create a true color image preserving transparency from a provided * image. * @todo: add bgcolor setting to add a default background color. */ function _gd_create_tmp($image, $width, $height) { $res = imagecreatetruecolor($width, $height); if ($image->info['extension'] == 'gif' || $image->info['extension'] == 'jpg') { // grab transparent color index from src. $transparent = imagecolortransparent($image->res); // if indexed transparency, lets preserve it. if ($transparent >= 0) { // get color(r,g,b) for index; $transparent = imagecolorsforindex($image->res, $transparent); // allocate to new image and get new index. $transparent = (isset($color['alpha'])) ? imagecolorallocatealpha($res, $color['red'], $color['green'], $color['blue'], $color['alpha']) : imagecolorallocate($res, $color['red'], $color['green'], $color['blue']); $transparent = imagecolorallocate($res, $transparent['red'], $transparent['green'], $transparent['blue']); // flood with our new transparent color. imagefill($res, 0, 0, $transparent); // tell the new image which color is transparent. imagecolortransparent($res, $transparent); } } elseif ($image->info['extension'] == 'png') { imagealphablending($res, false); $transparency = imagecolorallocatealpha($res, 0, 0, 0, 127); imagefill($res, 0, 0, $transparency); imagealphablending($res, true); imagesavealpha($res, true); } return $res; } function _gd_unsharp_mask($img, $radius, $sigma, $amount, $threshold) { ////////////////////////////////////////////////////////////// //// //// Unsharp Mask for PHP - version 2.1.1 //// //// Unsharp mask algorithm by Torstein Hønsi 2003-07. //// thoensi_at_netcom_dot_no. //// Please leave this notice. //// ////////////////////////////////////////////////////////////// // http://vikjavev.no/computing/ump.php // $img is an image that is already created within php using // imgcreatetruecolor. No url! $img must be a truecolor image. // Attempt to calibrate the parameters to Photoshop: if ($amount > 500) $amount = 500; $amount = $amount * 0.016; if ($radius > 50) $radius = 50; $radius = $radius * 2; if ($threshold > 255) $threshold = 255; $radius = abs(round($radius)); // Only integers make sense. if ($radius == 0) { return $img; imagedestroy($img); break; } $w = imagesx($img); $h = imagesy($img); $img_canvas = imagecreatetruecolor($w, $h); $img_blur = imagecreatetruecolor($w, $h); // Gaussian blur matrix: // // 1 2 1 // 2 4 2 // 1 2 1 // ////////////////////////////////////////////////// $matrix = array( array( 1, 2, 1 ), array( 2, 4, 2 ), array( 1, 2, 1 ) ); imagecopy($img_blur, $img, 0, 0, 0, 0, $w, $h); imageconvolution($img_blur, $matrix, 16, 0); if ($threshold > 0) { // Calculate the difference between the blurred pixels and the original // and set the pixels for ($x = 0; $x < $w-1; $x++) { // each row for ($y = 0; $y < $h; $y++) { // each pixel $rgb_orig = imagecolorat($img, $x, $y); $r_orig = (($rgb_orig >> 16) & 0xFF); $g_orig = (($rgb_orig >> 8) & 0xFF); $b_orig = ($rgb_orig & 0xFF); $rgb_blur = imagecolorat($img_blur, $x, $y); $r_blur = (($rgb_blur >> 16) & 0xFF); $g_blur = (($rgb_blur >> 8) & 0xFF); $b_blur = ($rgb_blur & 0xFF); // When the masked pixels differ less from the original // than the threshold specifies, they are set to their original value. $r_new = (abs($r_orig - $r_blur) >= $threshold) ? max(0, min(255, ($amount * ($r_orig - $r_blur)) + $r_orig)) : $r_orig; $g_new = (abs($g_orig - $g_blur) >= $threshold) ? max(0, min(255, ($amount * ($g_orig - $g_blur)) + $g_orig)) : $g_orig; $b_new = (abs($b_orig - $b_blur) >= $threshold) ? max(0, min(255, ($amount * ($b_orig - $b_blur)) + $b_orig)) : $b_orig; if (($r_orig != $r_new) || ($g_orig != $g_new) || ($b_orig != $b_new)) { $pix_col = imagecolorallocate($img, $r_new, $g_new, $b_new); imagesetpixel($img, $x, $y, $pix_col); } } } } else{ for ($x = 0; $x < $w; $x++) { // each row for ($y = 0; $y < $h; $y++) { // each pixel $rgb_orig = imagecolorat($img, $x, $y); $r_orig = (($rgb_orig >> 16) & 0xFF); $g_orig = (($rgb_orig >> 8) & 0xFF); $b_orig = ($rgb_orig & 0xFF); $rgb_blur = imagecolorat($img_blur, $x, $y); $r_blur = (($rgb_blur >> 16) & 0xFF); $g_blur = (($rgb_blur >> 8) & 0xFF); $b_blur = ($rgb_blur & 0xFF); $r_new = ($amount * ($r_orig - $r_blur)) + $r_orig; if ($r_new>255) $r_new=255; elseif ($r_new<0) $r_new=0; $g_new = ($amount * ($g_orig - $g_blur)) + $g_orig; if ($g_new>255) $g_new=255; elseif ($g_new<0) $g_new=0; $b_new = ($amount * ($b_orig - $b_blur)) + $b_orig; if ($b_new>255) $b_new=255; elseif ($b_new<0) $b_new=0; $rgb_new = ($r_new << 16) + ($g_new <<8) + $b_new; imagesetpixel($img, $x, $y, $rgb_new); } } } imagedestroy($img_canvas); imagedestroy($img_blur); return $img; }