Image resizing with PHP and GD
Sunday, October 31st, 2004
This morning I posted about some of my issues with Pictorialis II as a simple photo gallery manager. I decided to look into how PHP implements the imagecopyresized() and imagecopyresampled() functions. PHP basically just calls GD graphics library gdImageCopyResized() and gdImageCopyResampled() and passes the hard work off to GD (I guess I just didn’t remember it at 3:30AM this morning - then again I’m not sure I’ve ever looked at that particular part of the PHP source). So I downloaded the source for GD 2.0.31. It turns out my uneducated guess last night about the algorithm used for imagecopyresized() and imagecopyresampled() was basically right. The GD functions gdImageCopyResized() and gdImageCopyResampled() are in the file gd.c if anybody really wants to study them. This explains why PHP’s imagecopyresampled() is a bit slower than than imagecopyresized() but produces such prettier results. Imagecopyresampled() basically samples all of the pixels that imagecopyresized() would simply ignore in the resize and averages them together with the weighted colors of the pixels that imagecopyresized() would have directly copied into the new image.