If you are a web developer then you definitely have been faced with this little thingy. Let’s say you want to do dynamic resizing of images for a project of yours. All you need is an extension for PHP called GD. With that you are given many possibilities on image handling and manipulation, among others resizing. There, though,is a small problem you need to face. Let’s take it one step at a time.
Here is an image we want to resize:
Below is a small resizing script:
$source = imagecreatefromjpeg('tree.jpg');//opening image assuming you have an image called tree.jpg on the same folder of the script
$source_width = imagesx($source);
$source_height = imagesy($source);
$ratio = $source_width / $source_height;//the ration of the image which is a VERY good idea to preserve
$dest_width = 300;//width of the resized image
$dest_height = $dest_width / $ratio;//height of the resized image preserving the ratio
$destination = imagecreate($dest_width, $dest_height);//creating a new image
imagecopyresized($destination, $source, 0, 0, 0, 0, $dest_width, $dest_height, $source_width, $source_height);//resizing the image by actually copying a part (here the whole image but you can use a part) to the new image we created
//rendering the image to the browser
header('Content-Type: image/jpeg');
imagejpeg($destination);
//a good idea to free up some space especially if this script is part of a bigger one
imagedestroy($source);
imagedestroy($destination);
As you can see, it’s not rocket science. All we do is open the image, create a new canvas, copy a part (or whole) of the image and put it resized there using imagecopyresized. After running the script, here is the result:
Wops! As you can see, something, not so desirable, has happened! Although the image is resized preserving it’s initial ratio the colors are all messed up! It’s scrambled! To fix this small con all we need to do is change the way we create the new image (on line 7) using function “imagecreatetruecolor” instead of the simple “imagecreate”. This way, we avoid color scrambling. If you change the above code according to that and run it again here is the new image you will get:
Perfect! Congratulations on a perfect resize! Now start creating your php image gallery 😉
If you find any errors, have any suggestions or bugs please leave a comment!
Yes, that is a problem many of us faced, and found out that the true color function must be used instead of the standard one.
Good article, Stratos! 🙂
Haha this script looks Great One of the best post! Though I have not implemented this but I feel the post surely deserves a stumble 😈
Keep checking ur stats for a big boom 😛
thank you both for your nice words and comments!
Hah, I was also messing around with GD these days!
Good guide! Keep it up 😀
Also, why not use ImageMagick, since it has more features?
well all providers go with GD as a standard an i want to be as popular as i can 😛
hmm…if you’re really serious about the image quality then try imagecopyresampled than imagecopyresized.Believe me you get better quality than imagecopyresized but in performancewise imagecopyresized is better than imagecopyresampled.
it’s true and as you also said performance-wise the simple copy is what you are after. what i’d like to add is that when you go for a smaller resize then the simple copy is fine… thx for dropping by!