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

Source for file processor.imagick.php

Documentation is available at processor.imagick.php

  1. <?php
  2. /**
  3.  * $Header$
  4.  *
  5.  * Image processor - extension: php-imagick
  6.  * @package  liberty
  7.  * @subpackage plugins_processor
  8.  * @author   spider <spider@steelsun.com>
  9.  */
  10.  
  11. /**
  12.  * liberty_imagick_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_imagick_resize_image( &$pFileHash ) {
  19.     if( $func = liberty_imagick_get_function( 'resize_image' )) {
  20.         return $func( $pFileHash );
  21.     }
  22. }
  23.  
  24. /**
  25.  * liberty_imagick_rotate_image
  26.  * 
  27.  * @param array $pFileHash 
  28.  * @access public
  29.  * @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
  30.  */
  31. function liberty_imagick_rotate_image( &$pFileHash ) {
  32.     if( $func = liberty_imagick_get_function( 'rotate_image' )) {
  33.         return $func( $pFileHash );
  34.     }
  35. }
  36.  
  37. /**
  38.  * liberty_imagick_can_thumbnail_image
  39.  * 
  40.  * @param array $pMimeType 
  41.  * @access public
  42.  * @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
  43.  */
  44. function liberty_imagick_can_thumbnail_image( $pMimeType ) {
  45.     global $gBitSystem;
  46.     $ret = FALSE;
  47.     if( !empty( $pMimeType ) ) {
  48.         // allow images, pdf, and postscript thumbnailing (eps, ai, etc...)
  49.         if( $gBitSystem->isFeatureActive( 'liberty_thumbnail_pdf' )) {
  50.             $ret = preg_match( '/(^image|pdf$|postscript$)/i', $pMimeType );
  51.         } else {
  52.             $ret = preg_match( '/^image/i', $pMimeType );
  53.         }
  54.     }
  55.     return $ret;
  56. }
  57.  
  58. /**
  59.  * liberty_imagick_get_function will automagically pick the correct function based on the version of imagick extension installed
  60.  * 
  61.  * @return valid function.
  62.  */
  63. function liberty_imagick_get_function( $pFunction ) {
  64.     $ret = FALSE;
  65.     if( extension_loaded( 'imagick' )) {
  66.         if( function_exists( 'imagick_readimage' )) {
  67.             $version = 0;
  68.         } elseif( class_exists( 'Imagick' )) {
  69.             $version = 2;
  70.         }
  71.     }
  72.  
  73.     if( isset( $version ) && !empty( $pFunction )) {
  74.         $func = 'liberty_imagick'.$version.'_'.$pFunction;
  75.         if( function_exists( $func )) {
  76.             $ret = $func;
  77.         }
  78.     }
  79.     return $ret;
  80. }
  81.  
  82.  
  83. // =============================================
  84. // ======== Version 0.9* of php-imagick ========
  85. // =============================================
  86. function liberty_imagick0_resize_image( &$pFileHash ) {
  87.     global $gBitSystem;
  88.     $pFileHash['error'] = NULL;
  89.     $ret = NULL;
  90.     if( !empty( $pFileHash['source_file'] ) && is_file( $pFileHash['source_file'] ) ) {
  91.         $iImg = imagick_readimage( $pFileHash['source_file'] );
  92.         if( !$iImg ) {
  93.             // $pFileHash['error'] = $pFileHash['name'].' '.tra ( "is not a known image file" );
  94.             $destFile = liberty_process_generic( $pFileHash, FALSE );
  95.         } elseif( imagick_iserror( $iImg ) ) {
  96.             // $pFileHash['error'] = imagick_failedreason( $iImg ) . imagick_faileddescription( $iImg );
  97.             $destFile = liberty_process_generic( $pFileHash, FALSE );
  98.         } else {
  99.             imagick_set_image_quality( $iImg, $gBitSystem->getConfig( 'liberty_thumbnail_quality', 85 ));
  100.             $iwidth = imagick_getwidth( $iImg );
  101.             $iheight = imagick_getheight( $iImg );
  102.             if( (($iwidth / $iheight) > 0) && !empty( $pFileHash['max_width'] ) && !empty( $pFileHash['max_height'] ) ) {
  103.                 // we have a portrait image, flip everything
  104.                 $temp = $pFileHash['max_width'];
  105.                 $pFileHash['max_height'] = $pFileHash['max_width'];
  106.                 $pFileHash['max_width'] = $temp;
  107.             }
  108.  
  109.             $itype = imagick_getmimetype( $iImg );
  110.  
  111.             // override $mimeExt if we have a custom setting for it
  112.             if( $gBitSystem->isFeatureActive( 'liberty_thumbnail_format' )) {
  113.                 $mimeExt = $gBitSystem->getConfig( 'liberty_thumbnail_format' );
  114.             } else {
  115.                 list( $type, $mimeExt ) = explode( '/', strtolower( $itype ));
  116.             }
  117.  
  118.             if( $mimeExt = preg_replace( "!^(x-)?(jpeg|png|gif)$!", "$2", $mimeExt )) {
  119.                 $targetType = $mimeExt;
  120.                 $destExt = '.'.$mimeExt;
  121.             }
  122.             if( !$mimeExt || $mimeExt == 'jpeg' ) {
  123.                 $targetType = 'jpeg';
  124.                 $destExt = '.jpg';
  125.             }
  126.  
  127.             if( !empty( $pFileHash['max_width'] ) && !empty( $pFileHash['max_height'] ) && ( ($pFileHash['max_width'] < $iwidth || $pFileHash['max_height'] < $iheight ) || $mimeExt != $targetType )) {
  128.                 // We have to resize. *ALL* resizes are converted to jpeg or png
  129.                 if( !empty( $pFileHash['dest_file'] ) ) {
  130.                     $destFile = $pFileHash['dest_file'];
  131.                 } else {
  132.                     $destFile = STORAGE_PKG_PATH.$pFileHash['dest_branch'].$pFileHash['dest_base_name'].$destExt;
  133.                 }
  134.                 $pFileHash['name'] = $pFileHash['dest_base_name'].$destExt;
  135.                 // print "            if ( !imagick_resize( $iImg, $pFileHash[max_width], $pFileHash[max_height], IMAGICK_FILTER_LANCZOS, 0.5, $pFileHash[max_width] x $pFileHash[max_height] > ) ) {";
  136.  
  137.                 // Alternate Filter settings can seen here http://www.dylanbeattie.net/magick/filters/result.html
  138.  
  139.                 if ( !imagick_resize( $iImg, $pFileHash['max_width'], $pFileHash['max_height'], IMAGICK_FILTER_CATROM, 1.00, '>' ) ) {
  140.                     $pFileHash['error'] .= imagick_failedreason( $iImg ) . imagick_faileddescription( $iImg );
  141.                 }
  142.  
  143.                 if( function_exists( 'imagick_set_attribute' ) ) {
  144.                     // this exists in the PECL package, but not php-imagick
  145.                     $imagick_set_attribute( $iImg, array( "quality"=>1 ));
  146.                 }
  147.  
  148.                 if( !imagick_writeimage( $iImg, $destFile ) ) {
  149.                     $pFileHash['error'] .= imagick_failedreason( $iImg ) . imagick_faileddescription( $iImg );
  150.                 }
  151.                 $pFileHash['size'] = filesize( $destFile );
  152.             } else {
  153.                 // print "GENERIC";
  154.                 $destFile = liberty_process_generic( $pFileHash, FALSE );
  155.             }
  156.         }
  157.         $ret = $destFile;
  158.     } else {
  159.         $pFileHash['error'] = "No source file to resize";
  160.     }
  161.  
  162.     return $ret;
  163. }
  164.  
  165. function liberty_imagick0_rotate_image( &$pFileHash ) {
  166.     $ret = FALSE;
  167.     if( !empty( $pFileHash['source_file'] ) && is_file( $pFileHash['source_file'] ) ) {
  168.         $iImg = imagick_readimage( $pFileHash['source_file'] );
  169.         if( !$iImg ) {
  170.             $pFileHash['error'] = $pFileHash['name'].' '.tra ( "is not a known image file" );
  171.         } elseif( imagick_iserror( $iImg ) ) {
  172.             $pFileHash['error'] = imagick_failedreason( $iImg ) . imagick_faileddescription( $iImg );
  173.         } elseif( empty( $pFileHash['degrees'] ) || !is_numeric( $pFileHash['degrees'] ) ) {
  174.             $pFileHash['error'] = tra( 'Invalid rotation amount' );
  175.         } else {
  176.             if ( !imagick_rotate( $iImg, $pFileHash['degrees'] ) ) {
  177.                 $pFileHash['error'] .= imagick_failedreason( $iImg ) . imagick_faileddescription( $iImg );
  178.             }
  179.             if( !imagick_writeimage( $iImg, $pFileHash['source_file'] ) ) {
  180.                 $pFileHash['error'] .= imagick_failedreason( $iImg ) . imagick_faileddescription( $iImg );
  181.             }
  182.         }
  183.     } else {
  184.         $pFileHash['error'] = "No source file to resize";
  185.     }
  186.  
  187.     return( empty( $pFileHash['error'] ) );
  188. }
  189.  
  190.  
  191.  
  192. // ============================================
  193. // ======== Version 2.* of php-imagick ========
  194. // ============================================
  195. function liberty_imagick2_resize_image( &$pFileHash ) {
  196.     global $gBitSystem;
  197.     $pFileHash['error'] = NULL;
  198.     $ret = NULL;
  199.     if( !empty( $pFileHash['source_file'] ) && is_file( $pFileHash['source_file'] )) {
  200.         $im = new Imagick();
  201.         $im->readImage( $pFileHash['source_file'] );
  202.         if( !$im->valid()) {
  203.             $destFile = liberty_process_generic( $pFileHash, FALSE );
  204.         } else {
  205.             $im->setCompressionQuality( $gBitSystem->getConfig( 'liberty_thumbnail_quality', 85 ));
  206.             $iwidth = $im->getImageWidth();
  207.             $iheight = $im->getImageHeight();
  208.             /*
  209.              * the math on this was bad and the property assignments were bad - those are fixed. 
  210.              * however this is disabled since its being invoked by default, which prevents the max height
  211.              * from being enforced on portrait images which is counter intuitive. by default the bounding
  212.              * rectangle should be enforced. if someone wants this feature, then a flag for this needs to
  213.              * be created which will likely require some bigger change up stream, like in gThumbSizes and
  214.              * in liberty_generate_thumbnails()
  215.              * -wjames5
  216.             if((( $iwidth / $iheight ) < 1 ) && !empty( $pFileHash['max_width'] ) && !empty( $pFileHash['max_height'] )) {
  217.                 // we have a portrait image, flip everything
  218.                 $temp = $pFileHash['max_height'];
  219.                 $pFileHash['max_height'] = $pFileHash['max_width'];
  220.                 $pFileHash['max_width'] = $temp;
  221.             }
  222.              */
  223.  
  224.             // override $mimeExt if we have a custom setting for it
  225.             if( $gBitSystem->isFeatureActive( 'liberty_thumbnail_format' )) {
  226.                 $mimeExt = $gBitSystem->getConfig( 'liberty_thumbnail_format' );
  227.             } else {
  228.                 list( $type, $mimeExt ) = explode( '/', strtolower( $pFileHash['type'] ));
  229.             }
  230.  
  231.             if( preg_match( "!(png|gif)!", $mimeExt )) {
  232.                 $targetType = $mimeExt;
  233.                 $destExt = '.'.$mimeExt;
  234.             } else {
  235.                 $targetType = 'jpeg';
  236.                 $destExt = '.jpg';
  237.             }
  238.  
  239.             if( !empty( $pFileHash['max_width'] ) && !empty( $pFileHash['max_height'] ) && (( $pFileHash['max_width'] < $iwidth || $pFileHash['max_height'] < $iheight ) || $mimeExt != $targetType )) {
  240.                 if( !empty( $pFileHash['dest_file'] ) ) {
  241.                     $destFile = $pFileHash['dest_file'];
  242.                 } else {
  243.                     $destFile = STORAGE_PKG_PATH.$pFileHash['dest_branch'].$pFileHash['dest_base_name'].$destExt;
  244.                 }
  245.                 $pFileHash['name'] = $pFileHash['dest_base_name'].$destExt;
  246.  
  247.                 // create thumb and write
  248.                 $im->thumbnailImage( $pFileHash['max_width'],  $pFileHash['max_height'], TRUE );
  249.                 $im->writeImage( $destFile );
  250.  
  251.                 $pFileHash['size'] = filesize( $destFile );
  252.             } else {
  253.                 $destFile = liberty_process_generic( $pFileHash, FALSE );
  254.             }
  255.         }
  256.  
  257.         // destroy object
  258.         $im->destroy();
  259.  
  260.         $ret = $destFile;
  261.     } else {
  262.         $pFileHash['error'] = "No source file to resize";
  263.     }
  264.  
  265.     return $ret;
  266. }
  267.  
  268. function liberty_imagick2_rotate_image( &$pFileHash ) {
  269.     $ret = FALSE;
  270.     if( !empty( $pFileHash['source_file'] ) && is_file( $pFileHash['source_file'] )) {
  271.         $im = new Imagick();
  272.         $im->readImage( $pFileHash['source_file'] );
  273.         if( !$im->valid()) {
  274.             $destFile = liberty_process_generic( $pFileHash, FALSE );
  275.         } elseif( empty( $pFileHash['degrees'] ) || !is_numeric( $pFileHash['degrees'] )) {
  276.             $pFileHash['error'] = tra( 'Invalid rotation amount' );
  277.         } else {
  278.             $im->rotateImage( new ImagickPixel(), $pFileHash['degrees'] );
  279.             $im->writeImage( $pFileHash['source_file'] );
  280.         }
  281.     } else {
  282.         $pFileHash['error'] = "No source file to resize";
  283.     }
  284.  
  285.     return( empty( $pFileHash['error'] ));
  286. }

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