webmaster@1: descriptive title. webmaster@1: */ webmaster@1: function image_get_available_toolkits() { webmaster@1: $toolkits = file_scan_directory('includes', 'image\..*\.inc$'); webmaster@1: webmaster@1: $output = array(); webmaster@1: foreach ($toolkits as $file => $toolkit) { webmaster@1: include_once "./$file"; webmaster@1: $function = str_replace('.', '_', $toolkit->name) .'_info'; webmaster@1: if (function_exists($function)) { webmaster@1: $info = $function(); webmaster@1: $output[$info['name']] = $info['title']; webmaster@1: } webmaster@1: } webmaster@1: webmaster@1: return $output; webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Retrieve the name of the currently used toolkit. webmaster@1: * webmaster@1: * @return webmaster@1: * String containing the name of the selected toolkit, or FALSE on error. webmaster@1: */ webmaster@1: function image_get_toolkit() { webmaster@1: static $toolkit; webmaster@1: webmaster@1: if (!$toolkit) { webmaster@1: $toolkit = variable_get('image_toolkit', 'gd'); webmaster@1: $toolkit_file = './includes/image.'. $toolkit .'.inc'; webmaster@1: if (isset($toolkit) && file_exists($toolkit_file)) { webmaster@1: include_once $toolkit_file; webmaster@1: } webmaster@1: elseif (!image_gd_check_settings()) { webmaster@1: $toolkit = FALSE; webmaster@1: } webmaster@1: } webmaster@1: webmaster@1: return $toolkit; webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Invokes the given method using the currently selected toolkit. webmaster@1: * webmaster@1: * @param $method webmaster@1: * A string containing the method to invoke. webmaster@1: * @param $params webmaster@1: * An optional array of parameters to pass to the toolkit method. webmaster@1: * @return webmaster@1: * Mixed values (typically Boolean indicating successful operation). webmaster@1: */ webmaster@1: function image_toolkit_invoke($method, $params = array()) { webmaster@1: if ($toolkit = image_get_toolkit()) { webmaster@1: $function = 'image_'. $toolkit .'_'. $method; webmaster@1: if (function_exists($function)) { webmaster@1: return call_user_func_array($function, $params); webmaster@1: } webmaster@1: else { webmaster@1: watchdog('php', 'The selected image handling toolkit %toolkit can not correctly process %function.', array('%toolkit' => $toolkit, '%function' => $function), WATCHDOG_ERROR); webmaster@1: return FALSE; webmaster@1: } webmaster@1: } webmaster@1: } webmaster@1: webmaster@1: webmaster@1: /** webmaster@1: * Get details about an image. webmaster@1: * webmaster@1: * Drupal only supports GIF, JPG and PNG file formats. webmaster@1: * webmaster@1: * @return webmaster@1: * FALSE, if the file could not be found or is not an image. Otherwise, a webmaster@1: * keyed array containing information about the image: webmaster@1: * 'width' - Width in pixels. webmaster@1: * 'height' - Height in pixels. webmaster@1: * 'extension' - Commonly used file extension for the image. webmaster@1: * 'mime_type' - MIME type ('image/jpeg', 'image/gif', 'image/png'). webmaster@1: * 'file_size' - File size in bytes. webmaster@1: */ webmaster@1: function image_get_info($file) { webmaster@1: if (!is_file($file)) { webmaster@1: return FALSE; webmaster@1: } webmaster@1: webmaster@1: $details = FALSE; webmaster@1: $data = @getimagesize($file); webmaster@1: $file_size = @filesize($file); webmaster@1: webmaster@1: if (isset($data) && is_array($data)) { webmaster@1: $extensions = array('1' => 'gif', '2' => 'jpg', '3' => 'png'); webmaster@1: $extension = array_key_exists($data[2], $extensions) ? $extensions[$data[2]] : ''; webmaster@1: $details = array('width' => $data[0], webmaster@1: 'height' => $data[1], webmaster@1: 'extension' => $extension, webmaster@1: 'file_size' => $file_size, webmaster@1: 'mime_type' => $data['mime']); webmaster@1: } webmaster@1: webmaster@1: return $details; webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Scales an image to the exact width and height given. Achieves the webmaster@1: * target aspect ratio by cropping the original image equally on both webmaster@1: * sides, or equally on the top and bottom. This function is, for webmaster@1: * example, useful to create uniform sized avatars from larger images. webmaster@1: * webmaster@1: * The resulting image always has the exact target dimensions. webmaster@1: * webmaster@1: * @param $source webmaster@1: * The file path of the source image. webmaster@1: * @param $destination webmaster@1: * The file path of the destination image. webmaster@1: * @param $width webmaster@1: * The target width, in pixels. webmaster@1: * @param $height webmaster@1: * The target height, in pixels. webmaster@1: * @return webmaster@1: * TRUE or FALSE, based on success. webmaster@1: */ webmaster@1: function image_scale_and_crop($source, $destination, $width, $height) { webmaster@1: $info = image_get_info($source); webmaster@1: webmaster@1: $scale = max($width / $info['width'], $height / $info['height']); webmaster@1: $x = round(($info['width'] * $scale - $width) / 2); webmaster@1: $y = round(($info['height'] * $scale - $height) / 2); webmaster@1: webmaster@1: if (image_toolkit_invoke('resize', array($source, $destination, $info['width'] * $scale, $info['height'] * $scale))) { webmaster@1: return image_toolkit_invoke('crop', array($destination, $destination, $x, $y, $width, $height)); webmaster@1: } webmaster@1: return FALSE; webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Scales an image to the given width and height while maintaining aspect webmaster@1: * ratio. webmaster@1: * webmaster@1: * The resulting image can be smaller for one or both target dimensions. webmaster@1: * webmaster@1: * @param $source webmaster@1: * The file path of the source image. webmaster@1: * @param $destination webmaster@1: * The file path of the destination image. webmaster@1: * @param $width webmaster@1: * The target width, in pixels. webmaster@1: * @param $height webmaster@1: * The target height, in pixels. webmaster@1: * @return webmaster@1: * TRUE or FALSE, based on success. webmaster@1: */ webmaster@1: function image_scale($source, $destination, $width, $height) { webmaster@1: $info = image_get_info($source); webmaster@1: webmaster@1: // Don't scale up. webmaster@1: if ($width >= $info['width'] && $height >= $info['height']) { webmaster@1: return FALSE; webmaster@1: } webmaster@1: webmaster@1: $aspect = $info['height'] / $info['width']; webmaster@1: if ($aspect < $height / $width) { webmaster@1: $width = (int)min($width, $info['width']); webmaster@1: $height = (int)round($width * $aspect); webmaster@1: } webmaster@1: else { webmaster@1: $height = (int)min($height, $info['height']); webmaster@1: $width = (int)round($height / $aspect); webmaster@1: } webmaster@1: webmaster@1: return image_toolkit_invoke('resize', array($source, $destination, $width, $height)); webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Resize an image to the given dimensions (ignoring aspect ratio). webmaster@1: * webmaster@1: * @param $source webmaster@1: * The file path of the source image. webmaster@1: * @param $destination webmaster@1: * The file path of the destination image. webmaster@1: * @param $width webmaster@1: * The target width, in pixels. webmaster@1: * @param $height webmaster@1: * The target height, in pixels. webmaster@1: * @return webmaster@1: * TRUE or FALSE, based on success. webmaster@1: */ webmaster@1: function image_resize($source, $destination, $width, $height) { webmaster@1: return image_toolkit_invoke('resize', array($source, $destination, $width, $height)); webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Rotate an image by the given number of degrees. webmaster@1: * webmaster@1: * @param $source webmaster@1: * The file path of the source image. webmaster@1: * @param $destination webmaster@1: * The file path of the destination image. webmaster@1: * @param $degrees webmaster@1: * The number of (clockwise) degrees to rotate the image. webmaster@1: * @param $background webmaster@1: * An hexidecimal integer specifying the background color to use for the webmaster@1: * uncovered area of the image after the rotation. E.g. 0x000000 for black, webmaster@1: * 0xff00ff for magenta, and 0xffffff for white. webmaster@1: * @return webmaster@1: * TRUE or FALSE, based on success. webmaster@1: */ webmaster@1: function image_rotate($source, $destination, $degrees, $background = 0x000000) { webmaster@1: return image_toolkit_invoke('rotate', array($source, $destination, $degrees, $background)); webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Crop an image to the rectangle specified by the given rectangle. webmaster@1: * webmaster@1: * @param $source webmaster@1: * The file path of the source image. webmaster@1: * @param $destination webmaster@1: * The file path of the destination image. webmaster@1: * @param $x webmaster@1: * The top left co-ordinate, in pixels, of the crop area (x axis value). webmaster@1: * @param $y webmaster@1: * The top left co-ordinate, in pixels, of the crop area (y axis value). webmaster@1: * @param $width webmaster@1: * The target width, in pixels. webmaster@1: * @param $height webmaster@1: * The target height, in pixels. webmaster@1: * @return webmaster@1: * TRUE or FALSE, based on success. webmaster@1: */ webmaster@1: function image_crop($source, $destination, $x, $y, $width, $height) { webmaster@1: return image_toolkit_invoke('crop', array($source, $destination, $x, $y, $width, $height)); webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * @} End of "defgroup image". webmaster@1: */