*/ /** * liberty_imagick_can_thumbnail_image * * @param string $pMimeType * @access public * @return string|bool true on success, false on failure - mErrors will contain reason for failure */ function liberty_imagick_can_thumbnail_image( $pMimeType ) { global $gBitSystem; $ret = false; if( !empty( $pMimeType ) ) { // allow images, pdf, and postscript thumbnailing (eps, ai, etc...) $ret = $gBitSystem->isFeatureActive( 'liberty_thumbnail_pdf' ) ? preg_match( '/(^image|pdf$|postscript$)/i', $pMimeType ) : preg_match( '/^image/i', $pMimeType ); } return $ret; } // ============================================ // ======== Version 2.* of php-imagick ======== // ============================================ function liberty_imagick_resize_image( &$pFileHash ) { global $gBitSystem; $pFileHash['error'] = null; $ret = null; if( !empty( $pFileHash['source_file'] ) && file_exists( $pFileHash['source_file'] ) && is_file( $pFileHash['source_file'] ) && (filesize( $pFileHash['source_file'] ) > 0) ) { try { $im = new Imagick(); $im->readImage( $pFileHash['source_file'] ); if( !$im->valid()) { $destFile = \Bitweaver\Liberty\liberty_process_generic( $pFileHash, false ); } else { $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() * -wjames5 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; } */ global $gBitSystem; $pFileHash['type'] = $gBitSystem->verifyMimeType( $pFileHash['source_file'] ); // 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 ) = explode( '/', 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'] ) ) || ( !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'] ); } if( !empty( $pFileHash['max_width'] ) && !empty( $pFileHash['max_height'] ) && (( $pFileHash['max_width'] < $iwidth || $pFileHash['max_height'] < $iheight ) || $mimeExt != $targetType )) { $destFile = !empty( $pFileHash['dest_file'] ) ? $pFileHash['dest_file'] : STORAGE_PKG_PATH.$pFileHash['dest_branch'].$pFileHash['dest_base_name'].$destExt; if( !empty( $pFileHash['dest_base_name'] ) ) { $pFileHash['name'] = $pFileHash['dest_base_name'].$destExt; } // create thumb and write $im->thumbnailImage( (int)$pFileHash['max_width'], (int)$pFileHash['max_height'], true ); $im->writeImage( $destFile ); $pFileHash['size'] = filesize( $destFile ); } else { $destFile = \Bitweaver\Liberty\liberty_process_generic( $pFileHash, false ); } } // destroy object $im->clear(); $ret = $destFile; } catch( ImagickException $e ) { \Bitweaver\bit_error_log( $e->getMessage().' '.$pFileHash['source_file'] ); $ret = null; } } else { $pFileHash['error'] = "No source file to resize"; } return $ret; } function liberty_imagick_rotate_image( &$pFileHash ) { $ret = false; if( !empty( $pFileHash['source_file'] ) && is_file( $pFileHash['source_file'] )) { try { $im = new Imagick(); $im->readImage( $pFileHash['source_file'] ); if( !$im->valid()) { $destFile = \Bitweaver\Liberty\liberty_process_generic( $pFileHash, false ); } elseif( empty( $pFileHash['degrees'] ) || !is_numeric( $pFileHash['degrees'] )) { $pFileHash['error'] = KernelTools::tra( 'Invalid rotation amount' ); } else { $im->rotateImage( new ImagickPixel('none'), $pFileHash['degrees'] ); $im->writeImage( $pFileHash['source_file'] ); } $im->clear(); } catch( ImagickException $e ) { \Bitweaver\bit_error_log( $e->getMessage().' '.$pFileHash['source_file'] ); $pFileHash['error'] = $e->getMessage(); } } else { $pFileHash['error'] = "No source file to resize"; } return empty( $pFileHash['error'] ); }