Page 1 of 1

lessen the image size

Posted: Wed Feb 08, 2006 11:29 pm
by pleigh
hi guys, my question is, can php reduce the size of an image?to make things clear, for example, i have a 40Kb image, then upload it to the page...upon upload, there will be a function that will reduce its size to, lets say 20Kb, when, and will be stored in the proper folder...can this be done??an example will be gladly appreciated, thanks.. :)

Posted: Wed Feb 08, 2006 11:31 pm
by feyd
various scripts that perform "thumbnailing" can be made to do this.. depending on what you actually mean by reducing the size.. if you mean increasing the compression rate, simply changing the quality rating down in imagejpeg() will do such a thing.

Posted: Wed Feb 08, 2006 11:37 pm
by pleigh
feyd wrote:various scripts that perform "thumbnailing" can be made to do this.. depending on what you actually mean by reducing the size.. if you mean increasing the compression rate, simply changing the quality rating down in imagejpeg() will do such a thing.
thanks feyd..yes, what i meant is the filesize of the image and before storing the 45Kb image to the designated folder, it will convert or reduce the image to 20Kb...wat my concerrn is the load of images stored inside the webserver and i want to control the filesize of the image uploaded to my site..

Posted: Wed Feb 08, 2006 11:41 pm
by josh
You must decrease the physical size, or compression rate in order to decrease the filesize.

Posted: Thu Feb 09, 2006 3:00 am
by phpScott
check out the phpgd.com site as it has code snippets and tutorial on how to do what you are looking for.
Onion2K is the man that runs the site and in my humble opinion a master at using the gd2 library.

Posted: Thu Feb 09, 2006 4:30 am
by onion2k
Cheers Scott. /me bows.

jshpro2 is right though .. in order to reduce the file size you need to do something to the image .. either make it smaller or, in the case of a JPEG reduce the quality. If you're looking for a specific percentage reduction you'll need to create a jpeg at, say, 80 quality .. save it using the output buffer, check the size .. if it's too big then loop around again and create a new version at 70 quality.. check it .. and so on until you get the right file size. Bit of a nightmare really. I seem to remember seeing a class on PHPclasses.org that did it though .. it won one of the weekly thingies. Search there.

EDIT: Here we go.. http://www.phpclasses.org/browse/package/1980.html