2

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.

Igerko
  • 79
  • 2
  • 7

6 Answers6

4

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.

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
2

Try this

convert DSC_0124.JPG -resize 602x400! -strip -define jpeg:extent=100KB img1.jpg
Lakhan
  • 12,328
  • 3
  • 19
  • 28
  • 1
    It has worked for me. I've used -auto-orient, and instead of resizing it to a specific size, resized it to 15% original size (-resize 15%) to preserve the aspect ratio. See: https://www.imagemagick.org/Usage/resize/ – Orlando Sep 23 '20 at 05:14
1

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.

Sammitch
  • 30,782
  • 7
  • 50
  • 77
  • But if I get actual size and resize image by percentages, I lose a lot of quality ? – Igerko Oct 08 '13 at 17:56
  • 1
    When you shrink an image you are guaranteed to lose detail, that could be considered a loss of quality. When you use the JPEG file format there is an *additional* loss of image quality base on the the JPEG algorithm's 'quality/compression factor' which is [exemplified here](http://photo.stackexchange.com/a/29646). It is a balance between image quality and file size. – Sammitch Oct 08 '13 at 18:31
  • if your server is strong, imagemagick is fast, so if the orignal file sizes are only around 500K a fast loop checking the output file size of imagemagick could be made. Basically set how much you want the quality to drop, run the command and keep checking the file size until it is the size you want. Be sure to run the resize on the original file though every time, otherwise you will suffer the wrath of compounded lossy encoding – Dani Oct 08 '13 at 22:01
0

You will need to use gd/imagemagick

For example http://phpthumb.gxdlabs.com/

B-rad
  • 186
  • 3
  • 16
0

Have a look at this

It should be the solution you are looking for.

Community
  • 1
  • 1
0

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.

Dani
  • 1,220
  • 7
  • 22