Thumbnail code not working. Resize code do, but it's so slow
Posted: 2012-07-14T04:30:37-07:00
Hi, my name is Sara and I am using Imagick for the first time on shared LAMP, php version 5.3
During a file upload I am trying to:
1) Create a thumbnail.
2) Resize the image.
Number 1 is not working
Number 2 is working, but only if number 1 is not used.
This is whats phpinfo says:
imagick module version 3.0.1
imagick classes Imagick, ImagickDraw, ImagickPixel, ImagickPixelIterator
ImageMagick version ImageMagick 6.4.8 2009-12-02 Q16 OpenMP http://www.imagemagick.org
ImageMagick copyright Copyright (C) 1999-2009 ImageMagick Studio LLC
ImageMagick release date 2009-12-02
NB: I can not use exec, because of security demands.
I have tried for ages to get it working, but no succes.
Upon succes I happily will remove my install of TimThumb, which doesn't fullfill my requirements.
So please, I do need help.
I apologize for posting the code, please remove it if need be.
Kindest regards
Sara
During a file upload I am trying to:
1) Create a thumbnail.
2) Resize the image.
Number 1 is not working
Number 2 is working, but only if number 1 is not used.
This is whats phpinfo says:
imagick module version 3.0.1
imagick classes Imagick, ImagickDraw, ImagickPixel, ImagickPixelIterator
ImageMagick version ImageMagick 6.4.8 2009-12-02 Q16 OpenMP http://www.imagemagick.org
ImageMagick copyright Copyright (C) 1999-2009 ImageMagick Studio LLC
ImageMagick release date 2009-12-02
NB: I can not use exec, because of security demands.
I have tried for ages to get it working, but no succes.
Upon succes I happily will remove my install of TimThumb, which doesn't fullfill my requirements.
So please, I do need help.
I apologize for posting the code, please remove it if need be.
Kindest regards
Sara
Code: Select all
<?php
//BOC Image Process
if (extension_loaded('imagick')) {
// imagick installed
//#####################################################
//BOC thumbnail process
//
// Check if the thumbnails directory exists, if not create it with access rights set to 0775
$tn_dir = $this->destination . '/thumbnails/';
//
if (is_dir($tn_dir) || @mkdir($tn_dir,0775)) {return TRUE;}
// Delete old thumbnail if it exists
$old_thumbnail = $tn_dir . $this->filename;
//The function replace_extension is defined at the bottom
replace_extension($old_thumbnail, 'png');
if(file_exists($old_thumbnail)) {
unlink($old_thumbnail);
clearstatcache();
}
// Create and save a new thumbnail
// Load the image
$thumbnail = $tn_dir . $this->filename;
// Create an new Imagick object
$image = new Imagick($thumbnail);
// Strip out meta data/check if it's an image
if( $image->stripImage() == true ) {
// The file is an image, continue process
// Convert to png
$image->setImageFormat( "png" );
// Get the original dimensions
$width = $thumbnail->getImageWidth();
$height = $thumbnail->getImageHeight();
$bestfit = false;
// Set the thumbnails dimensions (these are treated as maximums and aspect ratio is maintained)
$thumbnailWidth = max(0,SMALL_IMAGE_WIDTH);
$thumbnailHeight = max(0,SMALL_IMAGE_HEIGHT);
// determine what to fit to
if ($thumbnailWidth*$thumbnailHeight != 0) {
$bestfit = true;
} else {
$fitDim = $thumbnailWidth > $thumbnailHeight;
}
// Create thumbnail
if ($bestfit == true) {
$image->thumbnailImage( intval($thumbnailWidth),intval($thumbnailHeight, TRUE));
} else {
$image->thumbnailImage( $fitDim ? intval($thumbnailWidth) : 0, $fitDim ? 0 : intval($thumbnailHeight), FALSE);
}
// Make it sharper
$image->unsharpMaskImage(0 , 0.5 , 1 , 0.05);
// Save thumbnail
$image->writeImage($thumbnail);
//Destroy the Imagick object and frees all resources associated with it.
$image->destroy();
}
//
//EOC thumbnail process
//#
//The above do not work. And, causes the below resize to fail.
//#####################################################
//BOC product image process
// Get the image
$image_file = $this->destination . $this->filename;
// create new Imagick object
$image = new Imagick($image_file);
if( $image->stripImage() == true ) {
// It's an image, continue process
// Set memory limit to 8 MB
$image->setResourceLimit( Imagick::RESOURCETYPE_MEMORY, 8 );
//Get the original image type
$imagePathParts = pathinfo($image_file);
$image_type = $imagePathParts['extension'];
// Convert to png format (used for simplified processing and better quality)
$image->setImageFormat( "png" );
// Set Max vert or horiz resolution
$maxsize = 480;// 320 is exactly 20em and 480 is exactly 30em (half the 960 grid)
// Resizes to whichever is larger, width or height
if($image->getImageHeight() <= $image->getImageWidth())
{
// Resize image using the lanczos resampling algorithm based on width
$image->resizeImage($maxsize,0,Imagick::FILTER_LANCZOS,0.75);
// Resize image using the catrom resampling algorithm based on width, faster than lanczos
// $image->resizeImage($maxsize,0,Imagick::FILTER_CATROM,0.75); <--------------------did not work, even without thumbnail code
} else {
// Resize image using the lanczos resampling algorithm based on height
$image->resizeImage(0,$maxsize,Imagick::FILTER_LANCZOS,0.75);
// Resize image using the catrom resampling algorithm based on height, faster than lanczos
// $image->resizeImage(0,$maxsize,Imagick::FILTER_CATROM,0.75); <--------------------did not work, even without thumbnail code
}
// Make it sharper
$image->unsharpMaskImage(0 , 0.5 , 1 , 0.05); //For center part
$image->sharpenimage( 0.8, 0.6);
$image->contrastImage( 5 );
// Set compression type for png's
$image->setInterlaceScheme(Imagick::INTERLACE_PNG);
$image->setCompression(Imagick::COMPRESSION_ZIP);
$image->setImageCompressionQuality(90);
// Convert back to original type
$image->setImageFormat( $image_type );
// Write resultant image to the original uploaded file
$image->writeImage($image_file);
//Destroy the Imagick object and free all resources associated with it.
$image->destroy();
//
//EOC product image process
//
}
}
//EOC Image Process
//#
function replace_extension($filename, $new_extension) {
$info = pathinfo($filename);
return $info['filename'] . '.' . $new_extension;
}
?>