2005-02-01 16:27:43 +00:00
< ? php
// $Id$
/**
* Return a list of available toolkits .
*
2005-03-23 20:26:21 +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' ];
}
}
2005-03-23 20:26:21 +00:00
$output [ 'gd' ] = t ( 'Built-in GD2 toolkit' );
2005-02-01 16:27:43 +00:00
return $output ;
}
/**
* Retrieve the name of the currently used toolkit .
*
* @ return String containing the name of the toolkit .
*/
function image_get_toolkit () {
static $toolkit ;
if ( ! $toolkit ) {
2005-03-29 00:01:23 +00:00
$toolkit = variable_get ( 'image_toolkit' , 'gd' );
2005-09-08 19:19:01 +00:00
$toolkit_file = './includes/image.' . $toolkit . '.inc' ;
2005-03-23 20:26:21 +00:00
if ( $toolkit != 'gd' && file_exists ( $toolkit_file )) {
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 .
*
* @ param $method A string containing the method to invoke .
2005-03-23 20:26:21 +00:00
* @ param $params An optional array of parameters to pass to the toolkit method .
2005-02-01 16:27:43 +00:00
*
2005-03-23 20:26:21 +00:00
* @ return Mixed values ( typically Boolean for 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 {
2006-08-18 12:17:00 +00:00
watchdog ( 'php' , t ( " 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
}
}
else {
if ( $method == 'settings' ) {
return image_gd_settings ();
2005-02-21 04:16:46 +00:00
}
2005-02-01 16:27:43 +00:00
}
}
/**
* Get details about an image .
*
* @ return array containing information about the image
* 'width' : image ' s width in pixels
* 'height' : image ' s height in pixels
* 'extension' : commonly used extension for the image
* 'mime_type' : image 's MIME type (' image / jpeg ', ' image / gif ' , etc . )
2005-07-31 09:57:27 +00:00
* 'file_size' : image ' s physical 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 ;
}
/**
* Scales an image to the given width and height while maintaining aspect
* ratio .
*
* @ param $source The filepath of the source image
* @ param $destination The file path of the destination image
* @ param $width The target width
* @ param $height The target height
*
2006-07-05 11:45:51 +00:00
* @ 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 );
// don't scale up
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 ) .
*
* @ param $source The filepath of the source image .
* @ param $destination The file path of the destination image .
* @ param $width The target width .
* @ param $height The target height .
*/
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 .
*
* @ param $source The filepath of the source image
* @ param $destination The file path of the destination image
* @ param $degrees The number of ( clockwise ) degrees to rotate the image
*/
function image_rotate ( $source , $destination , $degrees ) {
return image_toolkit_invoke ( 'rotate' , array ( $source , $destination , $degrees ));
}
/**
* Crop an image to the rectangle specified by the given rectangle .
*
* @ param $source The filepath of the source image
* @ param $destination The file path of the destination image
* @ 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
* @ param $height The target height
*/
function image_crop ( $source , $destination , $x , $y , $width , $height ) {
return image_toolkit_invoke ( 'crop' , array ( $source , $destination , $x , $y , $width , $height ));
}
/**
2005-03-23 20:26:21 +00:00
* GD2 toolkit functions
2005-03-29 00:01:23 +00:00
* With the minimal requirements of PHP 4.3 for Drupal , we use the built - in version of GD .
2005-02-01 16:27:43 +00:00
*/
/**
2005-03-29 00:01:23 +00:00
* Retrieve settings for the GD2 toolkit ( not used ) .
2005-02-01 16:27:43 +00:00
*/
function image_gd_settings () {
2005-03-23 20:26:21 +00:00
if ( image_gd_check_settings ()) {
2005-03-29 00:01:23 +00:00
return t ( 'The built-in GD2 toolkit is installed and working properly.' );
2005-03-23 20:26:21 +00:00
}
else {
2006-08-18 12:17:00 +00:00
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 <a href="@url">PHP\'s image documentation</a>.' , array ( '@url' => 'http://php.net/image' )));
2006-07-05 11:45:51 +00:00
return FALSE ;
2005-02-01 16:27:43 +00:00
}
2005-03-23 20:26:21 +00:00
}
/**
* Verify GD2 settings ( that the right version is actually installed ) .
*
* @ return boolean
*/
function image_gd_check_settings () {
if ( $check = get_extension_funcs ( 'gd' )) {
if ( in_array ( 'imagegd2' , $check )) {
// GD2 support is available.
2006-07-05 11:45:51 +00:00
return TRUE ;
2005-03-23 20:26:21 +00:00
}
}
2006-07-05 11:45:51 +00:00
return FALSE ;
2005-02-01 16:27:43 +00:00
}
/**
* Scale an image to the specified size using GD .
*/
function image_gd_resize ( $source , $destination , $width , $height ) {
if ( ! file_exists ( $source )) {
2006-07-05 11:45:51 +00:00
return FALSE ;
2005-02-01 16:27:43 +00:00
}
$info = image_get_info ( $source );
if ( ! $info ) {
2006-07-05 11:45:51 +00:00
return FALSE ;
2005-02-01 16:27:43 +00:00
}
$im = image_gd_open ( $source , $info [ 'extension' ]);
if ( ! $im ) {
2006-07-05 11:45:51 +00:00
return FALSE ;
2005-02-01 16:27:43 +00:00
}
2005-03-23 20:26:21 +00:00
$res = imageCreateTrueColor ( $width , $height );
2006-09-18 05:10:48 +00:00
if ( $info [ 'extension' ] == 'png' ) {
$transparency = imagecolorallocatealpha ( $res , 0 , 0 , 0 , 127 );
imagealphablending ( $res , FALSE );
imagefilledrectangle ( $res , 0 , 0 , $width , $height , $transparency );
imagealphablending ( $res , TRUE );
imagesavealpha ( $res , TRUE );
}
2005-03-23 20:26:21 +00:00
imageCopyResampled ( $res , $im , 0 , 0 , 0 , 0 , $width , $height , $info [ 'width' ], $info [ 'height' ]);
2005-02-01 16:27:43 +00:00
$result = image_gd_close ( $res , $destination , $info [ 'extension' ]);
imageDestroy ( $res );
imageDestroy ( $im );
return $result ;
}
/**
* Rotate an image the given number of degrees .
*/
function image_gd_rotate ( $source , $destination , $degrees , $bg_color = 0 ) {
if ( ! function_exists ( 'imageRotate' )) {
2006-07-05 11:45:51 +00:00
return FALSE ;
2005-02-01 16:27:43 +00:00
}
$info = image_get_info ( $source );
if ( ! $info ) {
2006-07-05 11:45:51 +00:00
return FALSE ;
2005-02-01 16:27:43 +00:00
}
$im = image_gd_open ( $source , $info [ 'extension' ]);
if ( ! $im ) {
2006-07-05 11:45:51 +00:00
return FALSE ;
2005-02-01 16:27:43 +00:00
}
$res = imageRotate ( $im , $degrees , $bg_color );
$result = image_gd_close ( $res , $destination , $info [ 'extension' ]);
return $result ;
}
/**
* Crop an image using the GD toolkit .
*/
function image_gd_crop ( $source , $destination , $x , $y , $width , $height ) {
$info = image_get_info ( $source );
if ( ! $info ) {
2006-07-05 11:45:51 +00:00
return FALSE ;
2005-02-01 16:27:43 +00:00
}
$im = image_gd_open ( $source , $info [ 'extension' ]);
2005-03-23 20:26:21 +00:00
$res = imageCreateTrueColor ( $width , $height );
2005-07-31 10:16:12 +00:00
imageCopy ( $res , $im , 0 , 0 , $x , $y , $width , $height );
2005-02-01 16:27:43 +00:00
$result = image_gd_close ( $res , $destination , $info [ 'extension' ]);
imageDestroy ( $res );
imageDestroy ( $im );
return $result ;
}
/**
* GD helper function to create an image resource from a file .
*/
function image_gd_open ( $file , $extension ) {
$extension = str_replace ( 'jpg' , 'jpeg' , $extension );
$open_func = 'imageCreateFrom' . $extension ;
if ( ! function_exists ( $open_func )) {
2006-07-05 11:45:51 +00:00
return FALSE ;
2005-02-01 16:27:43 +00:00
}
return $open_func ( $file );
}
/**
* GD helper to write an image resource to a destination file .
*/
function image_gd_close ( $res , $destination , $extension ) {
$extension = str_replace ( 'jpg' , 'jpeg' , $extension );
$close_func = 'image' . $extension ;
if ( ! function_exists ( $close_func )) {
2006-07-05 11:45:51 +00:00
return FALSE ;
2005-02-01 16:27:43 +00:00
}
return $close_func ( $res , $destination );
}
2005-08-25 21:14:17 +00:00