'ImageAPI', 'description' => 'Configure ImageAPI.', 'page callback' => 'drupal_get_form', 'page arguments' => array('imageapi_settings'), 'access arguments' => array('administer imageapi'), ); $toolkits = imageapi_get_available_toolkits(); if ($toolkits) { $items['admin/settings/imageapi/list'] = array( 'title' => 'List', 'type' => MENU_DEFAULT_LOCAL_TASK, 'weight' => -1, ); $items['admin/settings/imageapi/config'] = array( 'title' => 'Configure', 'type' => MENU_LOCAL_TASK, 'page callback' => 'drupal_get_form', 'page arguments' => array(imageapi_default_toolkit() .'_settings_form'), 'access arguments' => array('administer imageapi'), ); foreach ($toolkits as $module => $info) { if (function_exists($module .'_settings_form')) { $items['admin/settings/imageapi/config/'. $module] = array( 'title' => $info['name'], 'page callback' => 'drupal_get_form', 'page arguments' => array($module .'_settings_form'), 'access arguments' => array('administer imageapi'), 'type' => $module == imageapi_default_toolkit() ? MENU_DEFAULT_LOCAL_TASK : MENU_LOCAL_TASK, ); } else { drupal_set_message(t('ImageAPI toolkit missing settings form'), 'error'); } } } return $items; } function imageapi_settings() { $form = array(); $toolkits = imageapi_get_available_toolkits(); switch (count($toolkits)) { case 0: $form['imageapi_toolkits']['#value'] = t('There are no image toolkit modules enabled. Toolkit modules can be enabled from the module configuration page.', array('!admin-build-modules' => url('admin/build/modules'))); return $form; case 1: $toolkit = key($toolkits); // The variable needs to be manually set. Otherwise, if a user has two // toolkits and disables the selected one they won't be able to select the // remaing toolkit. variable_set('imageapi_image_toolkit', $toolkit); $form['imageapi_image_toolkit']['#value'] = t('The %toolkit module is the only enabled image toolkit. Drupal will use it for resizing, cropping and other image manipulations.', array('%toolkit' => $toolkits[$toolkit]['name'])); return $form; default: $options = array(); foreach ($toolkits as $module => $info) { $options[$module] = $info['name']; } $form['imageapi_image_toolkit'] = array( '#type' => 'radios', '#title' => t('Select a default image processing toolkit'), '#default_value' => imageapi_default_toolkit(), '#options' => $options, '#description' => t('This setting lets you choose which toolkit Drupal uses resizing, cropping and other image manipulations.'), ); } return system_settings_form($form); } /** * Return a list of available toolkits. * * @return * An array of the enabled image toolkit modules. The module name is the key * and the value is a sub-array with the module's 'name' and 'description'. */ function imageapi_get_available_toolkits() { static $toolkits; if (!isset($toolkits)) { $toolkits = array(); foreach (module_implements('imageapi_toolkit', true) as $module) { $info = drupal_parse_info_file(drupal_get_path('module', $module) .'/'. $module .'.info'); $toolkits[$module] = array('name' => $info['name'], 'description' => $info['description']); } } return $toolkits; } /** * Retrieve the name of the currently used toolkit. * * @return * String containing the name of the toolkit, or false if none is available. */ function imageapi_default_toolkit() { $toolkit = variable_get('imageapi_image_toolkit', 'imageapi_gd'); // Verify that the image toolkit is available. if (isset($toolkit) && module_exists($toolkit)) { return $toolkit; } // If it's not fall back to first available toolist. foreach (imageapi_get_available_toolkits() as $toolkit => $info) { return $toolkit; } return false; } /** * Scales an image to the exact width and height given. Achieves the * target aspect ratio by cropping the original image equally on both * sides, or equally on the top and bottom. This function is, for * example, useful to create uniform sized avatars from larger images. * * The resulting image always has the exact target dimensions. * * @param $image * An ImageAPI Image object. * @param $width * The target width in pixels. * @param $height * The target height in pixels. * @return * True or false, based on success. */ function imageapi_image_scale_and_crop(&$image, $width, $height) { $aspect = $image->info['height'] / $image->info['width']; if (empty($height)) $height = $width / $aspect; if (empty($width)) $width = $height * $aspect; $scale = max($width / $image->info['width'], $height / $image->info['height']); $x = (int) round(($image->info['width'] * $scale - $width) / 2); $y = (int) round(($image->info['height'] * $scale - $height) / 2); if (imageapi_image_resize($image, $image->info['width'] * $scale, $image->info['height'] * $scale)) { return imageapi_image_crop($image, $x, $y, $width, $height); } return false; } /** * Scales an image to the given width and height while maintaining aspect * ratio. * * The resulting image can be smaller for one or both target dimensions. * * @param $image * An ImageAPI Image object. * @param $width * The target width in pixels. * @param $height * The target height in pixels. * @param $upscale * Allow upscaling. * @return * True or false, based on success. */ function imageapi_image_scale(&$image, $width, $height, $upscale = false) { // set width/height according to aspect ratio if either is empty; $aspect = $image->info['height'] / $image->info['width']; if (empty($height)) $height = $width / $aspect; if (empty($width)) $width = $height * $aspect; // Don't scale up. if (!$upscale && round($width) >= $image->info['width'] && round($height) >= $image->info['height']) { return true; } $aspect = $image->info['height'] / $image->info['width']; if ($aspect < $height / $width) { $height = $width * $aspect; } else { $width = $height / $aspect; } $width = (int) round($width); $height = (int) round($height); return call_user_func($image->toolkit .'_image_resize', $image, $width, $height); } /** * Resize an image to the given dimensions (ignoring aspect ratio). * * @param $source The filepath of the source image. * @param $source * The filepath of the source image. * @param $destination * The file path of the destination image. * @param $width * The target width in pixels. * @param $height * The target height in pixels. * @param $toolkit * An optional override of the default image toolkit. * @return * True or false, based on success. */ function imageapi_image_resize(&$image, $width, $height) { $width = (int) round($width); $height = (int) round($height); return call_user_func($image->toolkit .'_image_resize', $image, $width, $height); } /** * Rotate an image by the given number of degrees. * * @param $image * An imageapi image object returned by imageapi_image_open(). * @param $degrees * The number of (clockwise) degrees to rotate the image. * @param $bg_color * An hexidecimal integer specifying the background color to use for the * uncovered area of the image after the rotation. E.g. 0x000000 for black, * 0xff00ff for magenta, and 0xffffff for white. * @return * True or false, based on success. */ function imageapi_image_rotate(&$image, $degrees, $bgcolor = 0x000000) { return call_user_func($image->toolkit .'_image_rotate', $image, $degrees, $bgcolor); } /** * Sharpen an image given some sharpening parameters. * * NOTE: These parameters only have an effect when Imagemagick is used. * GD will used a fixed convolution matrix as described in imageapi_gd.module * * @param $image * An imageapi image object returned by imageapi_image_open(). * @param $radius * The radius of the gaussian, in pixels, not counting the center pixel. (default 0.5) * @param $sigma * The standard deviation of the gaussian, in pixels. (default 0.5) * @param $amount * The percentage of the difference between the original and the blur image that is * added back into the original. (default 100) * @param $threshold * The threshold, as a fraction of max RGB levels, needed to apply the difference * amount. (default 0.05) * @return * True or false, based on success. */ function imageapi_image_sharpen(&$image, $radius, $sigma, $amount, $threshold) { return call_user_func($image->toolkit .'_image_sharpen', $image, $radius, $sigma, $amount, $threshold); } /** * Crop an image to the rectangle specified by the given rectangle. * * @param $image * An imageapi image object returned by imageapi_image_open(). * @param $x * The top left co-ordinate of the crop area (x axis value). * @param $y * The top left co-ordinate of the crop area (y axis value). * @param $width * The target width in pixels. * @param $height * The target height in pixels. * @return * True or false, based on success. */ function imageapi_image_crop(&$image, $x, $y, $width, $height) { $aspect = $image->info['height'] / $image->info['width']; if (empty($height)) $height = $width / $aspect; if (empty($width)) $width = $height * $aspect; $width = (int) round($width); $height = (int) round($height); return call_user_func($image->toolkit .'_image_crop', $image, $x, $y, $width, $height); } /** * Convert an image to grayscale. * * @param $image * An imageapi image object returned by imageapi_image_open(). * @return * True or false, based on success. */ function imageapi_image_desaturate(&$image) { return call_user_func($image->toolkit .'_image_desaturate', $image); } /** * Open an image file and return an imageapi image object. Any changes to the * file are not saved until imageapi_image_close() is called. * * @param $file * Path to an image file. * @param $toolkit * An optional, image toolkit name to override the default. * @return * ImageAPI Image object or FALSE if there was a problem opening the file. */ function imageapi_image_open($file, $toolkit = false) { if (!$toolkit) { $toolkit = imageapi_default_toolkit(); } if ($toolkit) { $image = new stdClass(); $image->source = $file; $image->info = image_get_info($file); $image->toolkit = $toolkit; if ($image = call_user_func($image->toolkit .'_image_open', $image)) { return $image; } } watchdog('imageapi', 'ImageAPI failed to open %file with %toolkit', array('%file' => $file, '%toolkit' => $toolkit), WATCHDOG_WARNING); return false; } /** * Close the image and save the changes to a file. * * @param $image * imageAPI image object. * * @param $destination * Destination path where the image should be saved. If it is empty the * original image file will be overwritten. */ function imageapi_image_close($image, $destination = '') { if (empty($destination)) { $destination = $image->source; } if ($return = call_user_func($image->toolkit .'_image_close', $image, $destination)) { if (@chmod($destination, 0775)) { return $return; } watchdog('imageapi', 'Could not set permissons on destination file: %file', array('%file' => $destination)); } return false; } /** * Convert a hex string to its RGBA (Red, Green, Blue, Alpha) integer * components. * * @param $hex * A string specifing an RGB color in the formats: * '#ABC','ABC','#ABCD','ABCD','#AABBCC','AABBCC','#AABBCCDD','AABBCCDD' * @return * An array with four elements for red, green, blue, and alpha. */ function imageapi_hex2rgba($hex) { $hex = ltrim($hex, '#'); if (preg_match('/^[0-9a-f]{3}$/i', $hex)) { // 'FA3' is the same as 'FFAA33' so r=FF, g=AA, b=33 $r = str_repeat($hex{0}, 2); $g = str_repeat($hex{1}, 2); $b = str_repeat($hex{2}, 2); $a = '0'; } elseif (preg_match('/^[0-9a-f]{6}$/i', $hex)) { // #FFAA33 or r=FF, g=AA, b=33 list($r, $g, $b) = str_split($hex, 2); $a = '0'; } elseif (preg_match('/^[0-9a-f]{8}$/i', $hex)) { // #FFAA33 or r=FF, g=AA, b=33 list($r, $g, $b, $a) = str_split($hex, 2); } elseif (preg_match('/^[0-9a-f]{4}$/i', $hex)) { // 'FA37' is the same as 'FFAA3377' so r=FF, g=AA, b=33, a=77 $r = str_repeat($hex{0}, 2); $g = str_repeat($hex{1}, 2); $b = str_repeat($hex{2}, 2); $a = str_repeat($hex{3}, 2); } else { //error: invalide hex string, TODO: set form error.. return false; } $r = hexdec($r); $g = hexdec($g); $b = hexdec($b); $a = hexdec($a); return array($r, $g, $b, $a); }