The code in the snippet is an example of how terrible the gd library is at the best of times when dealing with photos users have uploaded. That is why it is best to use the Imagick module whenever possible. It is faster, in oop and after a while becomes easier to use than gd. For example I have prepared the following script which loads a psd image which would be impossible in gd then resizes it, saves it to a file and displays the picture to the browser.
<?php
$file='images/banner.psd'; //yes this format is accepted
$width=96;
$height=72;
//load image
$thumb = new Imagick($file);
//resize image
$thumb->resizeImage($width,$height,Imagick::FILTER_LANCZOS,1);
//save image to file
$thumb->writeImage('mythumb.gif');
//convert image to jpeg
$thumb->setImageFormat('jpeg');
$thumb->setImageCompressionQuality(100);
//display image to browser
header("Content-type: image/jpeg");
echo $thumb->getImageBlob();
//remove from memory
$thumb->clear();
$thumb->destroy();
?>
Note that you can insert any image type that you like into the Imagick() parameter and it will accept it. No if statements, no mime types, just insert and go. So much more simpler.
cwarn23
Occupation: Genius
3,033 posts since Sep 2007
Reputation Points: 413
Solved Threads: 259
Did you mean $image because check again, there is no variable image ;)
cwarn23
Occupation: Genius
3,033 posts since Sep 2007
Reputation Points: 413
Solved Threads: 259
cwarn32, I appreciate your code, but it does not allow for a smaller "preview" of the image, do you know how you could use your suggestion to compensate for this?
turt2live
Junior Poster in Training
85 posts since Jan 2011
Reputation Points: 15
Solved Threads: 3
cwarn32, I appreciate your code, but it does not allow for a smaller "preview" of the image, do you know how you could use your suggestion to compensate for this?
My code resizes the image and displays it allowing for a smaller preview. So please describe what exactly it is your after as my code already fits that description.
cwarn23
Occupation: Genius
3,033 posts since Sep 2007
Reputation Points: 413
Solved Threads: 259
If you go to the sample link I provided on my code it gives you 2 thumbnails:
One that resizes and one that crops the image to a certain area.
Your code does the resize, but not the crop.
turt2live
Junior Poster in Training
85 posts since Jan 2011
Reputation Points: 15
Solved Threads: 3
well to crop you simply add the following code:
$thumb->cropImage($width,$height,$x,$y);
cwarn23
Occupation: Genius
3,033 posts since Sep 2007
Reputation Points: 413
Solved Threads: 259
turt2live
Junior Poster in Training
85 posts since Jan 2011
Reputation Points: 15
Solved Threads: 3