2005-02-01 16:27:43 +00:00
< ? php
// $Id$
2007-08-02 20:19:02 +00:00
/**
* @ file
* API for manipulating images .
*/
/**
* @ defgroup image Image toolkits
* @ {
* Drupal ' s image toolkits provide an abstraction layer for common image file
* manipulations like scaling , cropping , and rotating . The abstraction frees
* module authors from the need to support multiple image libraries , and it
* allows site administrators to choose the library that ' s best for them .
*
* PHP includes the GD library by default so a GD toolkit is installed with
* Drupal . Other toolkits like ImageMagic are available from contrib modules .
* GD works well for small images , but using it with larger files may cause PHP
* to run out of memory . In contrast the ImageMagick library does not suffer
* from this problem , but it requires the ISP to have installed additional
* software .
*
* Image toolkits are installed by copying the image . $name . inc file into
* Drupal ' s includes directory . The toolkit must then be enabled using the
* form at ? q = admin / settings / image - toolkit .
*/
2007-08-08 07:46:09 +00:00
/**
* @ file
* API for manipulating images .
*/
/**
* @ defgroup image Image toolkits
* @ {
* Drupal ' s image toolkits provide an abstraction layer for common image file
* manipulations like scaling , cropping , and rotating . The abstraction frees
* module authors from the need to support multiple image libraries , and it
* allows site administrators to choose the library that ' s best for them .
*
* PHP includes the GD library by default so a GD toolkit is installed with
* Drupal . Other toolkits like ImageMagic are available from contrib modules .
* GD works well for small images , but using it with larger files may cause PHP
* to run out of memory . In contrast the ImageMagick library does not suffer
* from this problem , but it requires the ISP to have installed additional
* software .
*
* Image toolkits are installed by copying the image . ToolkitName . inc file into
* Drupal ' s includes directory . The toolkit must then be enabled using the
* admin / settings / image - toolkit form .
*
* Only one toolkit maybe selected at a time . If a module author wishes to call
* a specific toolkit they can check that it is installed by calling
* image_get_available_toolkits (), and then calling its functions directly .
*/
2005-02-01 16:27:43 +00:00
/**
* Return a list of available toolkits .
*
2007-08-02 20:19:02 +00:00
* @ return
* An array of toolkit name => descriptive title .
2005-02-01 16:27:43 +00:00
*/
function image_get_available_toolkits () {
$toolkits = file_scan_directory ( 'includes' , 'image\..*\.inc$' );
$output = array ();
foreach ( $toolkits as $file => $toolkit ) {
2005-09-08 19:19:01 +00:00
include_once " ./ $file " ;
2005-03-29 00:01:23 +00:00
$function = str_replace ( '.' , '_' , $toolkit -> name ) . '_info' ;
2005-02-01 16:27:43 +00:00
if ( function_exists ( $function )) {
$info = $function ();
$output [ $info [ 'name' ]] = $info [ 'title' ];
}
}
2007-08-02 20:19:02 +00:00
2005-02-01 16:27:43 +00:00
return $output ;
}
/**
* Retrieve the name of the currently used toolkit .
*
2007-08-02 20:19:02 +00:00
* @ return
* String containing the name of the selected toolkit , or FALSE on error .
2005-02-01 16:27:43 +00:00
*/
function image_get_toolkit () {
static $toolkit ;
2007-08-02 20:19:02 +00:00
2005-02-01 16:27:43 +00:00
if ( ! $toolkit ) {
2005-03-29 00:01:23 +00:00
$toolkit = variable_get ( 'image_toolkit' , 'gd' );
2007-04-13 08:56:59 +00:00
$toolkit_file = './includes/image.' . $toolkit . '.inc' ;
2007-08-02 20:19:02 +00:00
if ( isset ( $toolkit ) && file_exists ( $toolkit_file )) {
2005-03-23 20:26:21 +00:00
include_once $toolkit_file ;
2005-02-21 04:16:46 +00:00
}
2005-03-23 20:26:21 +00:00
elseif ( ! image_gd_check_settings ()) {
2006-07-05 11:45:51 +00:00
$toolkit = FALSE ;
2005-02-01 16:27:43 +00:00
}
}
return $toolkit ;
}
/**
* Invokes the given method using the currently selected toolkit .
*
2007-08-02 20:19:02 +00:00
* @ param $method
* A string containing the method to invoke .
* @ param $params
* An optional array of parameters to pass to the toolkit method .
* @ return
2007-08-08 07:46:09 +00:00
* Mixed values ( typically Boolean indicating successful operation ) .
2005-02-01 16:27:43 +00:00
*/
function image_toolkit_invoke ( $method , $params = array ()) {
2005-02-21 04:16:46 +00:00
if ( $toolkit = image_get_toolkit ()) {
$function = 'image_' . $toolkit . '_' . $method ;
if ( function_exists ( $function )) {
return call_user_func_array ( $function , $params );
}
else {
2007-04-24 13:53:15 +00:00
watchdog ( 'php' , 'The selected image handling toolkit %toolkit can not correctly process %function.' , array ( '%toolkit' => $toolkit , '%function' => $function ), WATCHDOG_ERROR );
2006-07-05 11:45:51 +00:00
return FALSE ;
2005-03-23 20:26:21 +00:00
}
}
2005-02-01 16:27:43 +00:00
}
/**
* Get details about an image .
*
2007-08-02 20:19:02 +00:00
* Drupal only supports GIF , JPG and PNG file formats .
*
* @ return
* FALSE , if the file could not be found or is not an image . Otherwise , a
* keyed array containing information about the image :
* 'width' - Width in pixels .
* 'height' - Height in pixels .
* 'extension' - Commonly used file extension for the image .
* 'mime_type' - MIME type ( 'image/jpeg' , 'image/gif' , 'image/png' ) .
* 'file_size' - File size in bytes .
2005-02-01 16:27:43 +00:00
*/
function image_get_info ( $file ) {
2005-06-19 08:59:06 +00:00
if ( ! is_file ( $file )) {
2006-07-05 11:45:51 +00:00
return FALSE ;
2005-02-01 16:27:43 +00:00
}
2006-07-05 11:45:51 +00:00
$details = FALSE ;
2005-02-01 16:27:43 +00:00
$data = @ getimagesize ( $file );
2005-07-31 09:57:27 +00:00
$file_size = @ filesize ( $file );
2005-02-01 16:27:43 +00:00
2005-12-14 20:10:45 +00:00
if ( isset ( $data ) && is_array ( $data )) {
2005-02-01 16:27:43 +00:00
$extensions = array ( '1' => 'gif' , '2' => 'jpg' , '3' => 'png' );
$extension = array_key_exists ( $data [ 2 ], $extensions ) ? $extensions [ $data [ 2 ]] : '' ;
$details = array ( 'width' => $data [ 0 ],
2005-03-29 00:01:23 +00:00
'height' => $data [ 1 ],
'extension' => $extension ,
2005-07-31 09:57:27 +00:00
'file_size' => $file_size ,
2005-03-29 00:01:23 +00:00
'mime_type' => $data [ 'mime' ]);
2005-02-01 16:27:43 +00:00
}
return $details ;
}
2007-05-11 11:45:11 +00:00
/**
2007-05-12 05:51:20 +00:00
* Scales an image to the exact width and height given . Achieves the
* target aspect ratio by cropping the original image equally on both
2007-05-11 11:45:11 +00:00
* 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 .
*
2007-08-02 20:19:02 +00:00
* @ param $source
* The file path 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 .
* @ return
* TRUE or FALSE , based on success .
2007-05-11 11:45:11 +00:00
*/
function image_scale_and_crop ( $source , $destination , $width , $height ) {
$info = image_get_info ( $source );
$scale = max ( $width / $info [ 'width' ], $height / $info [ 'height' ]);
$x = round (( $info [ 'width' ] * $scale - $width ) / 2 );
$y = round (( $info [ 'height' ] * $scale - $height ) / 2 );
if ( image_toolkit_invoke ( 'resize' , array ( $source , $destination , $info [ 'width' ] * $scale , $info [ 'height' ] * $scale ))) {
return image_toolkit_invoke ( 'crop' , array ( $destination , $destination , $x , $y , $width , $height ));
}
return FALSE ;
}
2005-02-01 16:27:43 +00:00
/**
* Scales an image to the given width and height while maintaining aspect
* ratio .
*
2007-05-11 11:45:11 +00:00
* The resulting image can be smaller for one or both target dimensions .
*
2007-08-02 20:19:02 +00:00
* @ param $source
* The file path 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 .
* @ return
* TRUE or FALSE , based on success .
2005-02-01 16:27:43 +00:00
*/
function image_scale ( $source , $destination , $width , $height ) {
$info = image_get_info ( $source );
2007-05-11 11:45:11 +00:00
// Don't scale up.
2006-12-12 01:03:34 +00:00
if ( $width >= $info [ 'width' ] && $height >= $info [ 'height' ]) {
2006-07-05 11:45:51 +00:00
return FALSE ;
2005-02-01 16:27:43 +00:00
}
$aspect = $info [ 'height' ] / $info [ 'width' ];
if ( $aspect < $height / $width ) {
$width = ( int ) min ( $width , $info [ 'width' ]);
$height = ( int ) round ( $width * $aspect );
2005-10-22 15:14:46 +00:00
}
else {
2005-02-01 16:27:43 +00:00
$height = ( int ) min ( $height , $info [ 'height' ]);
$width = ( int ) round ( $height / $aspect );
}
return image_toolkit_invoke ( 'resize' , array ( $source , $destination , $width , $height ));
}
/**
* Resize an image to the given dimensions ( ignoring aspect ratio ) .
*
2007-08-02 20:19:02 +00:00
* @ param $source
* The file path 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 .
* @ return
* TRUE or FALSE , based on success .
2005-02-01 16:27:43 +00:00
*/
function image_resize ( $source , $destination , $width , $height ) {
return image_toolkit_invoke ( 'resize' , array ( $source , $destination , $width , $height ));
}
/**
* Rotate an image by the given number of degrees .
*
2007-08-02 20:19:02 +00:00
* @ param $source
* The file path of the source image .
* @ param $destination
* The file path of the destination image .
* @ param $degrees
* The number of ( clockwise ) degrees to rotate the image .
* @ param $background
* 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 .
2005-02-01 16:27:43 +00:00
*/
2007-08-02 20:19:02 +00:00
function image_rotate ( $source , $destination , $degrees , $background = 0x000000 ) {
return image_toolkit_invoke ( 'rotate' , array ( $source , $destination , $degrees , $background ));
2005-02-01 16:27:43 +00:00
}
/**
* Crop an image to the rectangle specified by the given rectangle .
*
2007-08-02 20:19:02 +00:00
* @ param $source
* The file path of the source image .
* @ param $destination
* The file path of the destination image .
* @ param $x
* The top left co - ordinate , in pixels , of the crop area ( x axis value ) .
* @ param $y
* The top left co - ordinate , in pixels , 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 .
2005-02-01 16:27:43 +00:00
*/
function image_crop ( $source , $destination , $x , $y , $width , $height ) {
return image_toolkit_invoke ( 'crop' , array ( $source , $destination , $x , $y , $width , $height ));
}
/**
2007-08-02 20:19:02 +00:00
* @ } End of " defgroup image " .
2005-02-01 16:27:43 +00:00
*/