lessen the image size

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
pleigh
Forum Contributor
Posts: 445
Joined: Wed Jan 19, 2005 4:26 am

lessen the image size

Post 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.. :)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
pleigh
Forum Contributor
Posts: 445
Joined: Wed Jan 19, 2005 4:26 am

Post 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..
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

You must decrease the physical size, or compression rate in order to decrease the filesize.
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post 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.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post 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
Post Reply