Reputation: 79
I need resize uploaded image to maximum size 100 kB. Is it possible ?
For example : image1.jpg with dimensions 1200x600 has 280kB and I need resize it to <100kB.
Upvotes: 2
Views: 9756
Reputation: 13226
Try this
convert DSC_0124.JPG -resize 602x400! -strip -define jpeg:extent=100KB img1.jpg
Upvotes: 2
Reputation: 207540
Recent versions of ImageMagick allow you to specify the maximum size for a JPEG file like this:
convert input.jpg -define jpeg:extent=100kb output.jpg
which does its best to get the output file under 100kB.
Upvotes: 4
Reputation: 32262
Short answer: No.
Long answer: It's possible, but it's probably more hassle than it's worth.
JPEGs do not have a fixed relationship between their physical dimensions and their filesize because of the algorithm used to compress the image not only takes into account the contents of the image, but also a 'quality' factor that determines how 'lossy' the compression process will be. It let's you save disk space, but your images look worse.
The best approach would be to use trial-and-error to find an image size [dimensions] that gets you to the neighbourhood of 100kB with a decent quality factor, resize to that in your script, check if the image's filesize is still too large, and start bringing down the quality if necessary.
Upvotes: 1
Reputation: 1250
If you are using a linux based server, i would recommend using imagemagick, it has a good CLI that can be interfaced easily with web languages.
There would be a few ways to approach this depending on if resolution or quality is more important to you.
Depending on image complexity, you are looking a file size between about 500K and 3K for a 1200x600 image
a command like
"imagemagick inputfile.jpg -quality 80 -resize 1200x600"
would probably work for most situations, but you could get more granular and check for file sizes and stuff and come up with a dynamic value for your quality.
Upvotes: 0
Reputation: 70
Have a look at this
It should be the solution you are looking for.
Upvotes: 0
Reputation: 186
You will need to use gd/imagemagick
For example http://phpthumb.gxdlabs.com/
Upvotes: 0