Source for file processor.magickwand.php
Documentation is available at processor.magickwand.php
* Image processor - extension: php-magickwand
* @subpackage plugins_processor
* @author spider <spider@steelsun.com>
* liberty_magickwand_resize_image
* @param array $pFileHash
* @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
// static var here is crucial
static $rgbConverts = array();
$magickWand = NewMagickWand();
$pFileHash['error'] = NULL;
if( !empty( $pFileHash['source_file'] ) && is_file( $pFileHash['source_file'] ) && filesize( $pFileHash['source_file'] ) ) {
if( $error = liberty_magickwand_check_error( MagickReadImage( $magickWand, $pFileHash['source_file'] ), $magickWand ) ) {
// $pFileHash['error'] = $error;
$destFile = liberty_process_generic( $pFileHash, FALSE );
if( MagickGetImageColorspace( $magickWand ) == MW_CMYKColorspace ) {
// These two lines are a hack needed for version of Ghostscript less that 8.60
MagickRemoveImageProfile( $magickWand, "ICC" );
MagickSetImageColorspace( $magickWand, MW_RGBColorspace );
$pFileHash['colorspace_conversion'] = TRUE;
MagickSetImageCompressionQuality( $magickWand, $gBitSystem->getConfig( 'liberty_thumbnail_quality', 85 ));
$iwidth = round( MagickGetImageWidth( $magickWand ) );
$iheight = round( MagickGetImageHeight( $magickWand ) );
// this does not seem to be needed. magickwand will work out what to do by using the destination file extension
//MagickSetImageFormat( $magickWand, $format );
if( ( empty( $pFileHash['max_width'] ) && empty( $pFileHash['max_height'] ) ) || ( !empty( $pFileHash['max_width'] ) && $pFileHash['max_width'] == MAX_THUMBNAIL_DIMENSION ) || ( !empty( $pFileHash['max_height'] ) && $pFileHash['max_height'] == MAX_THUMBNAIL_DIMENSION ) ) {
$pFileHash['max_width'] = $iwidth;
$pFileHash['max_height'] = $iheight;
} elseif( $iheight && ( $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'] );
} elseif( !empty( $pFileHash['max_height'] ) ) {
$pFileHash['max_width'] = round(( $iwidth / $iheight ) * $pFileHash['max_height'] );
// Make sure not to scale up
if( $pFileHash['max_width'] > $iwidth && $pFileHash['max_height'] > $iheight) {
$pFileHash['max_width'] = $iwidth;
$pFileHash['max_height'] = $iheight;
// override $mimeExt if we have a custom setting for it
$mimeExt = $gBitSystem->getConfig( 'liberty_thumbnail_format' );
} elseif( $itype = MagickGetImageMimeType( $magickWand ) ) {
$mimeExt = preg_replace( "!^(x-)?(jpeg|png|gif)$!", "$2", $mimeExt, - 1, $replaced );
if( empty( $destExt ) || $mimeExt == 'jpeg' ) {
if( !empty( $pFileHash['max_width'] ) && !empty( $pFileHash['max_height'] ) && ( ($pFileHash['max_width'] < $iwidth || $pFileHash['max_height'] < $iheight ) || $mimeExt != $targetType ) || !empty( $pFileHash['colorspace_conversion'] ) ) {
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;
// Alternate Filter settings can seen here http://www.dylanbeattie.net/magick/filters/result.html
if ( $error = liberty_magickwand_check_error( MagickResizeImage( $magickWand, $pFileHash['max_width'], $pFileHash['max_height'], MW_CatromFilter, 1.00 ), $magickWand ) ) {
$pFileHash['error'] .= $error;
$pFileHash['error'] .= $error;
$pFileHash['size'] = filesize( $destFile );
$pFileHash['error'] = "No source file to resize";
DestroyMagickWand( $magickWand );
* liberty_magickwand_rotate_image
* @param array $pFileHash
* @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
$magickWand = NewMagickWand();
$pFileHash['error'] = NULL;
if( !empty( $pFileHash['source_file'] ) && is_file( $pFileHash['source_file'] ) ) {
$pFileHash['error'] = $error;
} elseif( empty( $pFileHash['degrees'] ) || !is_numeric( $pFileHash['degrees'] ) ) {
$pFileHash['error'] = tra( 'Invalid rotation amount' );
$bgWand = NewPixelWand('white');
$pFileHash['error'] .= $error;
$pFileHash['error'] .= $error;
$pFileHash['error'] = "No source file to resize";
return( empty( $pFileHash['error'] ) );
* liberty_magickwand_check_error
* @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
if( $pResult === FALSE && WandHasException( $pWand ) ) {
$ret = 'An image processing error occurred : '. WandGetExceptionString($pWand);
* liberty_magickwand_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_magickwand_convert_colorspace
* @param array $pFileHash
* @param string $pColorSpace - target color space, only 'grayscale' is currently supported
* @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
if( !empty( $pFileHash['source_file'] ) && is_file( $pFileHash['source_file'] ) ) {
$magickWand = NewMagickWand();
bit_error_log( "MagickReadImage Failed:$error ( $pFileHash[source_file] )" );
MagickRemoveImageProfile( $magickWand, "ICC" );
if( MagickGetImageColorspace( $magickWand ) == MW_GRAYColorspace ) {
MagickSetImageColorspace( $magickWand, MW_GRAYColorspace );
if( empty( $pFileHash['dest_file'] ) ) {
$pFileHash['dest_file'] = STORAGE_PKG_PATH. $pFileHash['dest_branch']. $pFileHash['name'];
bit_error_log( "MagickWriteImage Failed:$error ( $pFileHash[source_file] )" );
DestroyMagickWand( $magickWand );
|