Source for file processor.imagick.php
Documentation is available at processor.imagick.php
* Image processor - extension: php-imagick
* @subpackage plugins_processor
* @author spider <spider@steelsun.com>
* liberty_imagick_resize_image
* @param array $pFileHash
* @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
return $func( $pFileHash );
* liberty_imagick_rotate_image
* @param array $pFileHash
* @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
return $func( $pFileHash );
* liberty_imagick_can_thumbnail_image
* @param array $pMimeType
* @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
if( !empty( $pMimeType ) ) {
// allow images, pdf, and postscript thumbnailing (eps, ai, etc...)
$ret = preg_match( '/(^image|pdf$|postscript$)/i', $pMimeType );
* liberty_imagick_get_function will automagically pick the correct function based on the version of imagick extension installed
* @return valid function.
if( isset ( $version ) && !empty( $pFunction )) {
$func = 'liberty_imagick'. $version. '_'. $pFunction;
// =============================================
// ======== Version 0.9* of php-imagick ========
// =============================================
$pFileHash['error'] = NULL;
if( !empty( $pFileHash['source_file'] ) && is_file( $pFileHash['source_file'] ) ) {
$iImg = imagick_readimage( $pFileHash['source_file'] );
// $pFileHash['error'] = $pFileHash['name'].' '.tra ( "is not a known image file" );
} elseif( imagick_iserror( $iImg ) ) {
// $pFileHash['error'] = imagick_failedreason( $iImg ) . imagick_faileddescription( $iImg );
imagick_set_image_quality( $iImg, $gBitSystem->getConfig( 'liberty_thumbnail_quality', 85 ));
$iwidth = imagick_getwidth( $iImg );
$iheight = imagick_getheight( $iImg );
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;
$itype = imagick_getmimetype( $iImg );
// override $mimeExt if we have a custom setting for it
$mimeExt = $gBitSystem->getConfig( 'liberty_thumbnail_format' );
if( $mimeExt = preg_replace( "!^(x-)?(jpeg|png|gif)$!", "$2", $mimeExt )) {
if( !$mimeExt || $mimeExt == 'jpeg' ) {
if( !empty( $pFileHash['max_width'] ) && !empty( $pFileHash['max_height'] ) && ( ($pFileHash['max_width'] < $iwidth || $pFileHash['max_height'] < $iheight ) || $mimeExt != $targetType )) {
// We have to resize. *ALL* resizes are converted to jpeg or png
if( !empty( $pFileHash['dest_file'] ) ) {
$destFile = $pFileHash['dest_file'];
$destFile = STORAGE_PKG_PATH. $pFileHash['dest_branch']. $pFileHash['dest_base_name']. $destExt;
$pFileHash['name'] = $pFileHash['dest_base_name']. $destExt;
// print " if ( !imagick_resize( $iImg, $pFileHash[max_width], $pFileHash[max_height], IMAGICK_FILTER_LANCZOS, 0.5, $pFileHash[max_width] x $pFileHash[max_height] > ) ) {";
// Alternate Filter settings can seen here http://www.dylanbeattie.net/magick/filters/result.html
if ( !imagick_resize( $iImg, $pFileHash['max_width'], $pFileHash['max_height'], IMAGICK_FILTER_CATROM, 1.00, '>' ) ) {
$pFileHash['error'] .= imagick_failedreason( $iImg ) . imagick_faileddescription( $iImg );
// this exists in the PECL package, but not php-imagick
$imagick_set_attribute( $iImg, array( "quality"=> 1 ));
if( !imagick_writeimage( $iImg, $destFile ) ) {
$pFileHash['error'] .= imagick_failedreason( $iImg ) . imagick_faileddescription( $iImg );
$pFileHash['size'] = filesize( $destFile );
$pFileHash['error'] = "No source file to resize";
if( !empty( $pFileHash['source_file'] ) && is_file( $pFileHash['source_file'] ) ) {
$iImg = imagick_readimage( $pFileHash['source_file'] );
$pFileHash['error'] = $pFileHash['name']. ' '. tra ( "is not a known image file" );
} elseif( imagick_iserror( $iImg ) ) {
$pFileHash['error'] = imagick_failedreason( $iImg ) . imagick_faileddescription( $iImg );
} elseif( empty( $pFileHash['degrees'] ) || !is_numeric( $pFileHash['degrees'] ) ) {
$pFileHash['error'] = tra( 'Invalid rotation amount' );
if ( !imagick_rotate( $iImg, $pFileHash['degrees'] ) ) {
$pFileHash['error'] .= imagick_failedreason( $iImg ) . imagick_faileddescription( $iImg );
if( !imagick_writeimage( $iImg, $pFileHash['source_file'] ) ) {
$pFileHash['error'] .= imagick_failedreason( $iImg ) . imagick_faileddescription( $iImg );
$pFileHash['error'] = "No source file to resize";
return( empty( $pFileHash['error'] ) );
// ============================================
// ======== Version 2.* of php-imagick ========
// ============================================
$pFileHash['error'] = NULL;
if( !empty( $pFileHash['source_file'] ) && is_file( $pFileHash['source_file'] )) {
$im->readImage( $pFileHash['source_file'] );
$im->setCompressionQuality( $gBitSystem->getConfig( 'liberty_thumbnail_quality', 85 ));
$iwidth = $im->getImageWidth();
$iheight = $im->getImageHeight();
* the math on this was bad and the property assignments were bad - those are fixed.
* however this is disabled since its being invoked by default, which prevents the max height
* from being enforced on portrait images which is counter intuitive. by default the bounding
* rectangle should be enforced. if someone wants this feature, then a flag for this needs to
* be created which will likely require some bigger change up stream, like in gThumbSizes and
* in liberty_generate_thumbnails()
if((( $iwidth / $iheight ) < 1 ) && !empty( $pFileHash['max_width'] ) && !empty( $pFileHash['max_height'] )) {
// we have a portrait image, flip everything
$temp = $pFileHash['max_height'];
$pFileHash['max_height'] = $pFileHash['max_width'];
$pFileHash['max_width'] = $temp;
// override $mimeExt if we have a custom setting for it
$mimeExt = $gBitSystem->getConfig( 'liberty_thumbnail_format' );
if( !empty( $pFileHash['max_width'] ) && !empty( $pFileHash['max_height'] ) && (( $pFileHash['max_width'] < $iwidth || $pFileHash['max_height'] < $iheight ) || $mimeExt != $targetType )) {
if( !empty( $pFileHash['dest_file'] ) ) {
$destFile = $pFileHash['dest_file'];
$destFile = STORAGE_PKG_PATH. $pFileHash['dest_branch']. $pFileHash['dest_base_name']. $destExt;
$pFileHash['name'] = $pFileHash['dest_base_name']. $destExt;
// create thumb and write
$im->thumbnailImage( $pFileHash['max_width'], $pFileHash['max_height'], TRUE );
$im->writeImage( $destFile );
$pFileHash['size'] = filesize( $destFile );
$pFileHash['error'] = "No source file to resize";
if( !empty( $pFileHash['source_file'] ) && is_file( $pFileHash['source_file'] )) {
$im->readImage( $pFileHash['source_file'] );
} elseif( empty( $pFileHash['degrees'] ) || !is_numeric( $pFileHash['degrees'] )) {
$pFileHash['error'] = tra( 'Invalid rotation amount' );
$im->rotateImage( new ImagickPixel(), $pFileHash['degrees'] );
$im->writeImage( $pFileHash['source_file'] );
$pFileHash['error'] = "No source file to resize";
return( empty( $pFileHash['error'] ));
|