diff options
Diffstat (limited to 'plugins')
| -rw-r--r-- | plugins/processor.imagick.php | 166 | ||||
| -rw-r--r-- | plugins/processor.magickwand.php | 8 |
2 files changed, 151 insertions, 23 deletions
diff --git a/plugins/processor.imagick.php b/plugins/processor.imagick.php index 2505424..252d3b6 100644 --- a/plugins/processor.imagick.php +++ b/plugins/processor.imagick.php @@ -1,6 +1,6 @@ <?php /** - * $Header: /cvsroot/bitweaver/_bit_liberty/plugins/processor.imagick.php,v 1.4 2007/07/29 14:23:25 squareing Exp $ + * $Header: /cvsroot/bitweaver/_bit_liberty/plugins/processor.imagick.php,v 1.5 2007/09/25 15:36:09 squareing Exp $ * * Image processor - extension: php-imagick * @package liberty @@ -15,6 +15,68 @@ * @return TRUE on success, FALSE on failure - mErrors will contain reason for failure */ function liberty_imagick_resize_image( &$pFileHash, $pThumbnail = FALSE ) { + if( $func = liberty_imagick_get_function( 'resize_image' )) { + return $func( $pFileHash, $pThumbnail ); + } +} + +/** + * liberty_imagick_rotate_image + * + * @param array $pFileHash + * @access public + * @return TRUE on success, FALSE on failure - mErrors will contain reason for failure + */ +function liberty_imagick_rotate_image( &$pFileHash ) { + if( $func = liberty_imagick_get_function( 'rotate_image' )) { + return $func( $pFileHash, $pThumbnail ); + } +} + +/** + * liberty_imagick_can_thumbnail_image + * + * @param array $pMimeType + * @access public + * @return TRUE on success, FALSE on failure - mErrors will contain reason for failure + */ +function liberty_imagick_can_thumbnail_image( $pMimeType ) { + $ret = FALSE; + if( !empty( $pMimeType ) ) { + $ret = preg_match( '/(^image|pdf$|postscript$)/i', $pMimeType ); + } + return $ret; +} + +/** + * liberty_imagick_get_function will automagically pick the correct function based on the version of imagick extension installed + * + * @return valid function. + */ +function liberty_imagick_get_function( $pFunction ) { + $ret = FALSE; + if( extension_loaded( 'imagick' )) { + if( function_exists( 'imagick_readimage' )) { + $version = 0; + } elseif( class_exists( 'Imagick' )) { + $version = 2; + } + } + + if( isset( $version ) && !empty( $pFunction )) { + $func = 'liberty_imagick'.$version.'_'.$pFunction; + if( function_exists( $func )) { + $ret = $func; + } + } + return $ret; +} + + +// ============================================= +// ======== Version 0.9* of php-imagick ======== +// ============================================= +function liberty_imagick0_resize_image( &$pFileHash, $pThumbnail = FALSE ) { global $gBitSystem; $pFileHash['error'] = NULL; $ret = NULL; @@ -90,14 +152,7 @@ function liberty_imagick_resize_image( &$pFileHash, $pThumbnail = FALSE ) { return $ret; } -/** - * liberty_imagick_rotate_image - * - * @param array $pFileHash - * @access public - * @return TRUE on success, FALSE on failure - mErrors will contain reason for failure - */ -function liberty_imagick_rotate_image( &$pFileHash ) { +function liberty_imagick0_rotate_image( &$pFileHash ) { $ret = FALSE; if( !empty( $pFileHash['source_file'] ) && is_file( $pFileHash['source_file'] ) ) { $iImg = imagick_readimage( $pFileHash['source_file'] ); @@ -122,18 +177,89 @@ function liberty_imagick_rotate_image( &$pFileHash ) { return( empty( $pFileHash['error'] ) ); } -/** - * liberty_imagick_can_thumbnail_image - * - * @param array $pMimeType - * @access public - * @return TRUE on success, FALSE on failure - mErrors will contain reason for failure - */ -function liberty_imagick_can_thumbnail_image( $pMimeType ) { - $ret = FALSE; - if( !empty( $pMimeType ) ) { - $ret = preg_match( '/(^image|pdf$|postscript$)/i', $pMimeType ); + + +// ============================================ +// ======== Version 2.* of php-imagick ======== +// ============================================ +function liberty_imagick2_resize_image( &$pFileHash, $pThumbnail = FALSE ) { + global $gBitSystem; + $pFileHash['error'] = NULL; + $ret = NULL; + if( !empty( $pFileHash['source_file'] ) && is_file( $pFileHash['source_file'] )) { + $im = new Imagick(); + $im->readImage( $pFileHash['source_file'] ); + if( !$im->valid()) { + $destUrl = liberty_process_generic( $pFileHash, FALSE ); + } else { + $im->setCompressionQuality( 85 ); + $iwidth = $im->getImageWidth(); + $iheight = $im->getImageHeight(); + if((( $iwidth / $iheight ) > 0 ) && !empty( $pFileHash['max_width'] ) && !empty( $pFileHash['max_height'] )) { + // we have a portrait image, flip everything + $temp = $pFileHash['max_width']; + $pFileHash['max_height'] = $pFileHash['max_width']; + $pFileHash['max_width'] = $temp; + } + + // override $mimeExt if we have a custom setting for it + if( $gBitSystem->isFeatureActive( 'liberty_thumbnail_format' )) { + $mimeExt = $gBitSystem->getConfig( 'liberty_thumbnail_format' ); + } else { + list( $type, $mimeExt ) = split( '/', strtolower( $pFileHash['type'] )); + } + + if( preg_match( "!(png|gif)!", $mimeExt )) { + $targetType = $mimeExt; + $destExt = '.'.$mimeExt; + } else { + $targetType = 'jpeg'; + $destExt = '.jpg'; + } + + if( !empty( $pFileHash['max_width'] ) && !empty( $pFileHash['max_height'] ) && (( $pFileHash['max_width'] < $iwidth || $pFileHash['max_height'] < $iheight ) || $mimeExt != $targetType )) { + $destUrl = $pFileHash['dest_path'].$pFileHash['dest_base_name'].$destExt; + $destFile = BIT_ROOT_PATH.'/'.$destUrl; + $pFileHash['name'] = $pFileHash['dest_base_name'].$destExt; + + // create thumb and write + $im->thumbnailImage( $pFileHash['max_width'], NULL ); + $im->writeImage( $destFile ); + + $pFileHash['size'] = filesize( $destFile ); + } else { + $destUrl = liberty_process_generic( $pFileHash, FALSE ); + } + } + + // destroy object + $im->destroy(); + + $ret = $destUrl; + } else { + $pFileHash['error'] = "No source file to resize"; } + return $ret; } + +function liberty_imagick2_rotate_image( &$pFileHash ) { + $ret = FALSE; + if( !empty( $pFileHash['source_file'] ) && is_file( $pFileHash['source_file'] )) { + $im = new Imagick(); + $im->readImage( $pFileHash['source_file'] ); + if( !$im->valid()) { + $destUrl = liberty_process_generic( $pFileHash, FALSE ); + } elseif( empty( $pFileHash['degrees'] ) || !is_numeric( $pFileHash['degrees'] )) { + $pFileHash['error'] = tra( 'Invalid rotation amount' ); + } else { + $im->rotateImage( new ImagickPixel(), $pFileHash['degrees'] ); + $im->writeImage( $pFileHash['source_file'] ); + } + } else { + $pFileHash['error'] = "No source file to resize"; + } + + return( empty( $pFileHash['error'] )); +} ?> diff --git a/plugins/processor.magickwand.php b/plugins/processor.magickwand.php index 7517bc5..0da769b 100644 --- a/plugins/processor.magickwand.php +++ b/plugins/processor.magickwand.php @@ -1,6 +1,6 @@ <?php /** - * $Header: /cvsroot/bitweaver/_bit_liberty/plugins/processor.magickwand.php,v 1.10 2007/07/29 14:23:25 squareing Exp $ + * $Header: /cvsroot/bitweaver/_bit_liberty/plugins/processor.magickwand.php,v 1.11 2007/09/25 15:36:09 squareing Exp $ * * Image processor - extension: php-magickwand * @package liberty @@ -47,6 +47,8 @@ function liberty_magickwand_resize_image( &$pFileHash, $pThumbnail = FALSE ) { MagickSetImageCompressionQuality( $magickWand, 85 ); $iwidth = round( MagickGetImageWidth( $magickWand ) ); $iheight = round( MagickGetImageHeight( $magickWand ) ); + vd($iwidth); + vd($iheight); // this does not seem to be needed. magickwand will work out what to do by using the destination file extension //MagickSetImageFormat( $magickWand, $format ); @@ -54,13 +56,13 @@ function liberty_magickwand_resize_image( &$pFileHash, $pThumbnail = FALSE ) { if( empty( $pFileHash['max_width'] ) || empty( $pFileHash['max_height'] ) || $pFileHash['max_width'] == MAX_THUMBNAIL_DIMENSION || $pFileHash['max_height'] == MAX_THUMBNAIL_DIMENSION ) { $pFileHash['max_width'] = $iwidth; $pFileHash['max_height'] = $iheight; - } elseif( (($iwidth / $iheight) < 1) && !empty( $pFileHash['max_width'] ) && !empty( $pFileHash['max_height'] ) ) { + } elseif(( $iwidth / $iheight ) < 1 && !empty( $pFileHash['max_width'] ) && !empty( $pFileHash['max_height'] )) { // we have a portrait image, flip everything $temp = $pFileHash['max_width']; $pFileHash['max_height'] = $pFileHash['max_width']; $pFileHash['max_width'] = round( ($iwidth / $iheight) * $pFileHash['max_height'] ); } elseif( !empty( $pFileHash['max_width'] ) ) { - $pFileHash['max_height'] = round( ($iheight / $iwidth) * $pFileHash['max_width'] ); + $pFileHash['max_height'] = round(( $iheight / $iwidth ) * $pFileHash['max_width'] ); } // Make sure not to scale up |
