Oftentimes you might want to display an image on the web that is a certain size, whether to avoid breaking your beautiful (CSS of course) layout, preventing download times getting too long or so on. Intelligent people like you would of course not even consider using the HTML <img> tag's width and height properties to fake resizing, the consequence of which is usually that your reader waits for a megabyte of picture to download before seeing a messed-up 10x10 pixel thumbnail. Naughty.
Of course if you have the image in advance, and there's not too many of them, you can resize it on your computer before upload. On the other hand, if the image is getting added later and is somewhat unpredictable in nature - for instance to be uploaded by a visitor to your site - it would be nice to allow them to avoid having to do that themselves, or more likely, ruin your page because they couldn't be bothered to do so. Enter Mr PHP.
Despite the Poorhouse's initial concern, it is actually almost ludicrously simple to resize an image via PHP, assuming the GD toolkit is installed on your server. You can find out whether it is or not by making a page with the following less-than-extensive code in, uploading to your server and viewing it in your web browser.
<?php
phpinfo();
?>That will print out details about your PHP install's configuration. You, unsurprisingly, should look out for a section called "GD", especially an entry named "GD Support". If this is enabled, you're good to go. These days it comes bundled with PHP so hopefully you will be in luck.
If it isn't then if you are buying hosting services from someone else, you need to ask them to install it. If it's your own server, follow the installation instructions on the PHP site. There are also other graphics toolkits via which you could accomplish the same feat, but they won't be mentioned here.
So, to start resizing your images, you need to learn a whole four, count 'em, four commands:
- imagecreatefromjpeg (string filename) - assuming the original image is indeed a jpeg which in this example it will be.
- imagecreatetruecolor(int x_size, int y_size)
- imagecopyresampled ( resource dst_image, resource src_image, int dst_x, int dst_y, int src_x, int src_y, int dst_w, int dst_h, int src_w, int src_h )
- imagejpeg ( resource image [, string filename [, int quality]] )
So, the method is no doubt fairly intuitive, but to make it clear, we will be:
- Creating a (gd) image from an existing jpeg
- Creating a blank image of our chosen size
- Copying and resampling the original image onto the new image
- Saving the new image as a file
Assuming "pic1.jpg" is the original picture and is in place on your server, "pic2.jpg" is what you want your new image to be called, and we want the final image to be 100x100 pixels big we can do it like this:
<?php
$originalimage = imagecreatefromjpeg("pic1.jpg");
$newimage = imagecreatetruecolor(100,100);
imagecopyresampled($newimage, $originalimage, 0, 0, 0, 0, 100, 100, imagesx($originalimage), imagesy($originalimage));
imagejpeg($newimage,"pic2.jpg");
?>Only the third line needs explaining really. The first two parameters are the new and old image. The four 0s following tell the server that you want to resize the whole original image to the whole new image, by changing these to the appropriate pixel co-ordinates you could create a resized version of just a part of the original image if you so wished. The two 100s are the new size, and the next two parameters are the width and height of the old image. You could write these in manually if you wished, but using imagesx() and imagesy() gets the server to calculate this from your source image automatically.
Done.
If all that was a bit much for you but you like the concept, then you might like to try the thumbnails script at icant.co.uk which trawls through a whole folder of jpegs or pngs and makes aspect-ratio-correct thumbnails of your required size for you using cleverer versions of above type of techniques. If you choose to do this and suffer problems relating to files not being found due to working in multiple folders, the Poorhouse found changing:
if (preg_match("/jpg|jpeg/",$system[1])){$src_img=imagecreatefromjpeg($name);}
if (preg_match("/png/",$system[1])){$src_img=imagecreatefrompng($name);}to
if (preg_match("/jpg|jpeg/",end($system))){$src_img=imagecreatefromjpeg($name);}
if (preg_match("/png/",end($system))){$src_img=imagecreatefrompng($name);}in the createthumb() function (as suggested by one commenter over there) to be the simple solution.
Finally, if all you're wanting to do is swiftly resize an single image via the web so you can go on to include it in an email, blog and so on then forget all this hard work, and go to one of the many online resizey-services. Currently, the Poorhouse favours Snipshot which allows you to intuitively resize, crop and mess around with pictures and download the results to your computer or direct to Flickr.

Comments
Post new comment