View Full Version : Resize/create image

10-28-2009, 01:57 AM
Does anyone know how I can resize, and create new image with php? I want the first image to only be resized if it is greater than 600 pixels wide and the second image to be the same image but 110 pixels wide and have a _tmb after the name but before the extension.

I found this http://www.white-hat-web-design.co.uk/articles/php-image-resizing.php but it doesn't work even if I copy it straight over with no changes. I don't know if it's only for a specific version of php or something else but I don't see much documentation on it either; so if anyone knows something better that'd be great. Thanks.

10-28-2009, 02:33 AM
You have to use the GD library or imagemagick (which is not part of the default installation for PHP).

You simply calculate the size of the image and determine if you want it to change, then do some basic math to keep the ratio the same, assuming you don't want to distort it, and apply an image function from one of the two libraries, and output the image.

Here's a place to start:
The example script is basically all you need.

Be warned, though, the image functions in PHP are slow, resource intensive (I've gone over allowed processor usage once or twice with big images), and they can be difficult to use for complex processes.
It's always best to limit your use of them to as few images as possible and always keep the images reasonably small-- using this to resize large photos may be a bad idea.
Also, never perform the same calculation twice, so find a way to cache the thumbnail/etc once you have created it (basically save it to a folder and then next time see if that image exists-- if so use it, else create/save the scaled image from the original).

10-29-2009, 01:09 AM
I use GD library and ImageMagick together to resize images when they are uploaded to my site so that the original image is stored in one folder and the thumbnail version is stored in another folder, much like you describe. I recently went through a lot of my anime reviews updating the screenshots to higher quality images and I would upload 6 "full sized" images at a time to be formatted. I spent two full days going through my dvds and uploading the images.

Another program, which is just for fun will take an image and randomize the colors or alter it it in many of the ways that can be done utilizing the GD library. There is also a reset button for the image as well.

Lastly, I have a program that will generate a rounded corner for my articles which is small and has the appropriate foreground and background colors.

The question is how intensive do these programs need to get before you get in trouble with your website host for doing something that is too processor intensive? To be honest I am the only one that can and does utilize these programs with the exception of the one that I created just for fun, but I doubt many people are even aware of that one.

10-29-2009, 05:20 AM
Thanks I did't know I need a library upon going into my server I found a pear package that looks like it does this as well. I'm going to test it out later though got back up with some other stuff.

10-29-2009, 06:20 AM
Note: GD does come installed with PHP in almost all packages; it's not necessarily part of the core of PHP, but it's always there. Imagemagick is something you must go find/install yourself (not that it's hard to find, etc.).