2 // $Id: image.inc 144 2007-03-28 07:52:20Z thierry $
5 * Return a list of available toolkits.
7 * @return An array of toolkit name => descriptive title.
9 function image_get_available_toolkits() {
10 $toolkits = file_scan_directory('includes', 'image\..*\.inc$');
13 foreach ($toolkits as $file => $toolkit) {
14 include_once "./$file";
15 $function = str_replace('.', '_', $toolkit->name) .'_info';
16 if (function_exists($function)) {
18 $output[$info['name']] = $info['title'];
21 $output['gd'] = t('Built-in GD2 toolkit');
26 * Retrieve the name of the currently used toolkit.
28 * @return String containing the name of the toolkit.
30 function image_get_toolkit() {
33 $toolkit = variable_get('image_toolkit', 'gd');
34 $toolkit_file = './includes/image.'.$toolkit.'.inc';
35 if ($toolkit != 'gd' && file_exists($toolkit_file)) {
36 include_once $toolkit_file;
38 elseif (!image_gd_check_settings()) {
47 * Invokes the given method using the currently selected toolkit.
49 * @param $method A string containing the method to invoke.
50 * @param $params An optional array of parameters to pass to the toolkit method.
52 * @return Mixed values (typically Boolean for successful operation).
54 function image_toolkit_invoke($method, $params = array()) {
55 if ($toolkit = image_get_toolkit()) {
56 $function = 'image_'. $toolkit .'_'. $method;
57 if (function_exists($function)) {
58 return call_user_func_array($function, $params);
61 watchdog('php', t("The selected image handling toolkit '%toolkit' can not correctly process '%function'.", array('%toolkit' => "<em>$toolkit</em>", '%function' => "<em>$function</em>")), WATCHDOG_ERROR);
66 if ($method == 'settings') {
67 return image_gd_settings();
74 * Get details about an image.
76 * @return array containing information about the image
77 * 'width': image's width in pixels
78 * 'height': image's height in pixels
79 * 'extension': commonly used extension for the image
80 * 'mime_type': image's MIME type ('image/jpeg', 'image/gif', etc.)
81 * 'file_size': image's physical size (in bytes)
83 function image_get_info($file) {
84 if (!is_file($file)) {
89 $data = @getimagesize($file);
90 $file_size = @filesize($file);
92 if (isset($data) && is_array($data)) {
93 $extensions = array('1' => 'gif', '2' => 'jpg', '3' => 'png');
94 $extension = array_key_exists($data[2], $extensions) ? $extensions[$data[2]] : '';
95 $details = array('width' => $data[0],
97 'extension' => $extension,
98 'file_size' => $file_size,
99 'mime_type' => $data['mime']);
106 * Scales an image to the given width and height while maintaining aspect
109 * @param $source The filepath of the source image
110 * @param $destination The file path of the destination image
111 * @param $width The target width
112 * @param $height The target height
114 * @return True or false, based on success
116 function image_scale($source, $destination, $width, $height) {
117 $info = image_get_info($source);
120 if ($width > $info['width'] && $height > $info['height']) {
124 $aspect = $info['height'] / $info['width'];
125 if ($aspect < $height / $width) {
126 $width = (int)min($width, $info['width']);
127 $height = (int)round($width * $aspect);
130 $height = (int)min($height, $info['height']);
131 $width = (int)round($height / $aspect);
134 return image_toolkit_invoke('resize', array($source, $destination, $width, $height));
138 * Resize an image to the given dimensions (ignoring aspect ratio).
140 * @param $source The filepath of the source image.
141 * @param $destination The file path of the destination image.
142 * @param $width The target width.
143 * @param $height The target height.
145 function image_resize($source, $destination, $width, $height) {
146 return image_toolkit_invoke('resize', array($source, $destination, $width, $height));
150 * Rotate an image by the given number of degrees.
152 * @param $source The filepath of the source image
153 * @param $destination The file path of the destination image
154 * @param $degrees The number of (clockwise) degrees to rotate the image
156 function image_rotate($source, $destination, $degrees) {
157 return image_toolkit_invoke('rotate', array($source, $destination, $degrees));
161 * Crop an image to the rectangle specified by the given rectangle.
163 * @param $source The filepath of the source image
164 * @param $destination The file path of the destination image
165 * @param $x The top left co-ordinate of the crop area (x axis value)
166 * @param $y The top left co-ordinate of the crop area (y axis value)
167 * @param $width The target width
168 * @param $height The target height
170 function image_crop($source, $destination, $x, $y, $width, $height) {
171 return image_toolkit_invoke('crop', array($source, $destination, $x, $y, $width, $height));
175 * GD2 toolkit functions
176 * With the minimal requirements of PHP 4.3 for Drupal, we use the built-in version of GD.
180 * Retrieve settings for the GD2 toolkit (not used).
182 function image_gd_settings() {
183 if (image_gd_check_settings()) {
184 return t('The built-in GD2 toolkit is installed and working properly.');
187 form_set_error('image_toolkit', t("The built-in GD image toolkit requires that the GD module for PHP be installed and configured properly. For more information see %url.", array('%url' => '<a href="http://php.net/image">http://php.net/image</a>')));
193 * Verify GD2 settings (that the right version is actually installed).
197 function image_gd_check_settings() {
198 if ($check = get_extension_funcs('gd')) {
199 if (in_array('imagegd2', $check)) {
200 // GD2 support is available.
208 * Scale an image to the specified size using GD.
210 function image_gd_resize($source, $destination, $width, $height) {
211 if (!file_exists($source)) {
215 $info = image_get_info($source);
220 $im = image_gd_open($source, $info['extension']);
225 $res = imageCreateTrueColor($width, $height);
226 imageCopyResampled($res, $im, 0, 0, 0, 0, $width, $height, $info['width'], $info['height']);
227 $result = image_gd_close($res, $destination, $info['extension']);
236 * Rotate an image the given number of degrees.
238 function image_gd_rotate($source, $destination, $degrees, $bg_color = 0) {
239 if (!function_exists('imageRotate')) {
243 $info = image_get_info($source);
248 $im = image_gd_open($source, $info['extension']);
253 $res = imageRotate($im, $degrees, $bg_color);
254 $result = image_gd_close($res, $destination, $info['extension']);
260 * Crop an image using the GD toolkit.
262 function image_gd_crop($source, $destination, $x, $y, $width, $height) {
263 $info = image_get_info($source);
268 $im = image_gd_open($source, $info['extension']);
269 $res = imageCreateTrueColor($width, $height);
270 imageCopy($res, $im, 0, 0, $x, $y, $width, $height);
271 $result = image_gd_close($res, $destination, $info['extension']);
280 * GD helper function to create an image resource from a file.
282 function image_gd_open($file, $extension) {
283 $extension = str_replace('jpg', 'jpeg', $extension);
284 $open_func = 'imageCreateFrom'. $extension;
285 if (!function_exists($open_func)) {
288 return $open_func($file);
292 * GD helper to write an image resource to a destination file.
294 function image_gd_close($res, $destination, $extension) {
295 $extension = str_replace('jpg', 'jpeg', $extension);
296 $close_func = 'image'. $extension;
297 if (!function_exists($close_func)) {
300 return $close_func($res, $destination);