liberty
[ class tree: liberty ] [ index: liberty ] [ all elements ]

Source for file processor.magickwand.php

Documentation is available at processor.magickwand.php

  1. <?php
  2. /**
  3.  * $Header$
  4.  *
  5.  * Image processor - extension: php-magickwand
  6.  * @package  liberty
  7.  * @subpackage plugins_processor
  8.  * @author   spider <spider@steelsun.com>
  9.  */
  10.  
  11. /**
  12.  * liberty_magickwand_resize_image
  13.  * 
  14.  * @param array $pFileHash 
  15.  * @access public
  16.  * @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
  17.  */
  18. function liberty_magickwand_resize_image( &$pFileHash ) {
  19.     global $gBitSystem;
  20.     // static var here is crucial
  21.     static $rgbConverts = array();
  22.     $magickWand = NewMagickWand();
  23.     $pFileHash['error'] = NULL;
  24.     $ret = NULL;
  25.  
  26.     if( !empty( $pFileHash['source_file'] ) && is_file( $pFileHash['source_file'] ) && filesize( $pFileHash['source_file'] ) ) {
  27.         if( $error = liberty_magickwand_check_error( MagickReadImage( $magickWand, $pFileHash['source_file'] ), $magickWand ) ) {
  28.             // $pFileHash['error'] = $error;
  29.             $destFile = liberty_process_generic( $pFileHash, FALSE );
  30.         } else {
  31.             if( MagickGetImageColorspace( $magickWand ) == MW_CMYKColorspace ) {
  32. //                These two lines are a hack needed for version of Ghostscript less that 8.60
  33.                 MagickRemoveImageProfile( $magickWand, "ICC" );
  34.                 MagickSetImageProfile( $magickWand, 'ICC', file_get_contents( UTIL_PKG_PATH.'icc/USWebCoatedSWOP.icc' ) );
  35.                 MagickProfileImage( $magickWand, 'ICC', file_get_contents( UTIL_PKG_PATH.'icc/srgb.icm' ));
  36.                 MagickSetImageColorspace( $magickWand, MW_RGBColorspace );
  37.                 $pFileHash['colorspace_conversion'] = TRUE;
  38.             }
  39.             MagickSetImageCompressionQuality( $magickWand, $gBitSystem->getConfig( 'liberty_thumbnail_quality', 85 ));
  40.             $iwidth = round( MagickGetImageWidth( $magickWand ) );
  41.             $iheight = round( MagickGetImageHeight( $magickWand ) );
  42.  
  43.             // this does not seem to be needed. magickwand will work out what to do by using the destination file extension
  44.             //MagickSetImageFormat( $magickWand, $format );
  45.  
  46.             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 ) ) {
  47.                 $pFileHash['max_width'] = $iwidth;
  48.                 $pFileHash['max_height'] = $iheight;
  49.             } elseif( $iheight && ( $iwidth / $iheight ) < 1 && !empty( $pFileHash['max_width'] ) && !empty( $pFileHash['max_height'] )) {
  50.                 // we have a portrait image, flip everything
  51.                 $temp = $pFileHash['max_width'];
  52.                 $pFileHash['max_height'] = $pFileHash['max_width'];
  53.                 $pFileHash['max_width'] = round(( $iwidth / $iheight ) * $pFileHash['max_height'] );
  54.             } elseif( !empty( $pFileHash['max_width'] ) ) {
  55.                 $pFileHash['max_height'] = round(( $iheight / $iwidth ) * $pFileHash['max_width'] );
  56.             } elseif( !empty( $pFileHash['max_height'] ) ) {
  57.                 $pFileHash['max_width'] = round(( $iwidth / $iheight ) * $pFileHash['max_height'] );
  58.             }
  59.  
  60.             // Make sure not to scale up
  61.             if( $pFileHash['max_width'] > $iwidth && $pFileHash['max_height'] > $iheight) {
  62.                 $pFileHash['max_width'] = $iwidth;
  63.                 $pFileHash['max_height'] = $iheight;
  64.             }
  65.  
  66.             // override $mimeExt if we have a custom setting for it
  67.             if( $gBitSystem->isFeatureActive( 'liberty_thumbnail_format' )) {
  68.                 $mimeExt = $gBitSystem->getConfig( 'liberty_thumbnail_format' );
  69.             } elseif( $itype = MagickGetImageMimeType( $magickWand ) ) {
  70.                 list( $type, $mimeExt ) = preg_split( '#/#', strtolower( $itype ) );
  71.             } else {
  72.                 list( $type, $mimeExt ) = preg_split( '#/#', strtolower( $pFileHash['type'] ) );
  73.             }
  74.             $replaced = FALSE;
  75.             $mimeExt = preg_replace( "!^(x-)?(jpeg|png|gif)$!", "$2", $mimeExt, -1, $replaced );
  76.             if( $replaced ) {
  77.                 $targetType = $mimeExt;
  78.                 $destExt = '.'.$mimeExt;
  79.             }
  80.             if( empty( $destExt ) || $mimeExt == 'jpeg' ) {
  81.                 $targetType = 'jpeg';
  82.                 $destExt = '.jpg';
  83.             }
  84.             if( !empty( $pFileHash['max_width'] ) && !empty( $pFileHash['max_height'] ) && ( ($pFileHash['max_width'] < $iwidth || $pFileHash['max_height'] < $iheight ) || $mimeExt != $targetType ) || !empty( $pFileHash['colorspace_conversion'] ) ) {
  85.                 if( !empty( $pFileHash['dest_file'] ) ) {
  86.                     $destFile = $pFileHash['dest_file'];
  87.                 } else {
  88.                     $destFile = STORAGE_PKG_PATH.$pFileHash['dest_branch'].$pFileHash['dest_base_name'].$destExt;
  89.                 }
  90.  
  91.                 $pFileHash['name'] = $pFileHash['dest_base_name'].$destExt;
  92.                 // Alternate Filter settings can seen here http://www.dylanbeattie.net/magick/filters/result.html
  93.                 if ( $error = liberty_magickwand_check_error( MagickResizeImage( $magickWand, $pFileHash['max_width'], $pFileHash['max_height'], MW_CatromFilter, 1.00 ), $magickWand ) ) {
  94.                     $pFileHash['error'] .= $error;
  95.                 }
  96.                 if( $error = liberty_magickwand_check_error( MagickWriteImage( $magickWand, $destFile ), $magickWand ) ) {
  97.                     $pFileHash['error'] .= $error;
  98.                 }
  99.                 $pFileHash['size'] = filesize( $destFile );
  100.             } else {
  101.                 $destFile = liberty_process_generic( $pFileHash, FALSE );
  102.             }
  103.         }
  104.         $ret = $destFile;
  105.     } else {
  106.         $pFileHash['error'] = "No source file to resize";
  107.     }
  108.  
  109.     DestroyMagickWand( $magickWand );
  110.     return $ret;
  111. }
  112.  
  113. /**
  114.  * liberty_magickwand_rotate_image
  115.  * 
  116.  * @param array $pFileHash 
  117.  * @access public
  118.  * @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
  119.  */
  120. function liberty_magickwand_rotate_image( &$pFileHash ) {
  121.     $ret = FALSE;
  122.     $magickWand = NewMagickWand();
  123.     $pFileHash['error'] = NULL;
  124.     if( !empty( $pFileHash['source_file'] ) && is_file( $pFileHash['source_file'] ) ) {
  125.         if( $error = liberty_magickwand_check_error( MagickReadImage( $magickWand, $pFileHash['source_file'] ), $magickWand ) ) {
  126.             $pFileHash['error'] = $error;
  127.         } elseif( empty( $pFileHash['degrees'] ) || !is_numeric( $pFileHash['degrees'] ) ) {
  128.             $pFileHash['error'] = tra( 'Invalid rotation amount' );
  129.         } else {
  130.             $bgWand = NewPixelWand('white');
  131.             if( $error = liberty_magickwand_check_error( MagickRotateImage( $magickWand, $bgWand, $pFileHash['degrees'] ), $magickWand ) ) {
  132.                 $pFileHash['error'] .= $error;
  133.             }
  134.             if( $error = liberty_magickwand_check_error( MagickWriteImage( $magickWand, $pFileHash['source_file'] ), $magickWand ) ) {
  135.                 $pFileHash['error'] .= $error;
  136.             }
  137.         }
  138.     } else {
  139.         $pFileHash['error'] = "No source file to resize";
  140.     }
  141.  
  142.     return( empty( $pFileHash['error'] ) );
  143. }
  144.  
  145. /**
  146.  * liberty_magickwand_check_error
  147.  * 
  148.  * @param array $pResult 
  149.  * @param array $pWand 
  150.  * @access public
  151.  * @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
  152.  */
  153. function liberty_magickwand_check_error( $pResult, $pWand ) {
  154.     $ret = FALSE;
  155.     if( $pResult === FALSE && WandHasException( $pWand ) ) {
  156.         $ret = 'An image processing error occurred : '.WandGetExceptionString($pWand);
  157.     }
  158.     return $ret;
  159. }
  160.  
  161. /**
  162.  * liberty_magickwand_can_thumbnail_image
  163.  * 
  164.  * @param array $pMimeType 
  165.  * @access public
  166.  * @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
  167.  */
  168. function liberty_magickwand_can_thumbnail_image( $pMimeType ) {
  169.     global $gBitSystem;
  170.     $ret = FALSE;
  171.     if( !empty( $pMimeType ) ) {
  172.         // allow images, pdf, and postscript thumbnailing (eps, ai, etc...)
  173.         if( $gBitSystem->isFeatureActive( 'liberty_thumbnail_pdf' )) {
  174.             $ret = preg_match( '/(^image|pdf$|postscript$)/i', $pMimeType );
  175.         } else {
  176.             $ret = preg_match( '/^image/i', $pMimeType );
  177.         }
  178.     }
  179.     return $ret;
  180. }
  181.  
  182. /**
  183.  * liberty_magickwand_convert_colorspace
  184.  * 
  185.  * @param array $pFileHash 
  186.  * @param string $pColorSpace - target color space, only 'grayscale' is currently supported
  187.  * @access public
  188.  * @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
  189.  */
  190. function liberty_magickwand_convert_colorspace_image( &$pFileHash, $pColorSpace ) {
  191.     $ret = FALSE;
  192.     if( !empty( $pFileHash['source_file'] ) && is_file( $pFileHash['source_file'] ) ) {
  193.         $magickWand = NewMagickWand();
  194.         if( $error = liberty_magickwand_check_error( MagickReadImage( $magickWand, $pFileHash['source_file'] ), $magickWand ) ) {
  195.             bit_error_log( "MagickReadImage Failed:$error ( $pFileHash[source_file] )" );
  196.         }  else {
  197.             MagickRemoveImageProfile( $magickWand, "ICC" );
  198.             switch( strtolower( $pColorSpace ) ) {
  199.                 case 'grayscale':
  200.                     if( MagickGetImageColorspace( $magickWand ) == MW_GRAYColorspace ) {
  201.                         $ret = TRUE;
  202.                     } else {
  203.                         MagickSetImageColorspace( $magickWand, MW_GRAYColorspace );
  204.                         if( empty( $pFileHash['dest_file'] ) ) {
  205.                             $pFileHash['dest_file'] = STORAGE_PKG_PATH.$pFileHash['dest_branch'].$pFileHash['name'];
  206.                         }
  207.                         if( $error = liberty_magickwand_check_error( MagickWriteImage( $magickWand, $pFileHash['dest_file'] ), $magickWand ) ) {
  208.                             bit_error_log( "MagickWriteImage Failed:$error ( $pFileHash[source_file] )" );
  209.                         } else {
  210.                             $ret = TRUE;
  211.                         }
  212.                     }
  213.                     break;
  214.             }
  215.         }
  216.         DestroyMagickWand( $magickWand );
  217.     }
  218.     return $ret;
  219. }
  220. ?>

Documentation generated on Wed, 29 Jul 2015 13:57:17 +0000 by phpDocumentor 1.5.0-lsces