One problem I encountered recently was saving space on the web server which was being used to store user-uploaded photos. The current upload directory was approaching 8 GB in size – the limit for the Ubuntu Amazon AIM. So I had to find a quick solution to compressing the images (some of which had resolutions of 5 megapixels and higher).
I found that using mogrify (which somes with ImageMagick) was a quick and easy solution to resizing the images. I first had to Install ImageMagick on the server:
| sudo apt-get install imagemagick |
Then, I was ready get compressing. I wanted to resize all images larger than 1024 x 1014 pixels to that size.
| # Resize images larger than 1024x1024 | |
| sudo mogrify -verbose -resize '1024x1024>' image.jpg | |
| # Resize all images to 1024x1024 | |
| sudo mogrify -verbose -resize '1024x1024' image.jpg | |
| # Resize all JPEG images larger than 1024x1024 | |
| sudo mogrify -verbose -resize '1024x1024>' *.jpg |
The command explained:
- Resizes images larger than 1024×1024 to 1024×1024, while keeping the correct aspect ratio.
- Resize all images to 1024×1024. Images larger than this size will be scaled down, while smaller images will be scaled up.
- Resize all images with the ‘jpg’ extension
The -verbose flag will explain how the image was resized, giving the before and after dimensions.
| $> sudo mogrify -verbose -resize '1024x1024>' image.jpg | |
| image.jpg JPEG 2592x1944 2592x1944+0+0 8-bit DirectClass 2.395MB 0.110u 0:00.109 | |
| image.jpg JPEG 2592x1944=>1024x768 1024x768+0+0 8-bit DirectClass 500KB 0.960u 0:00.950 |
mogrify can also be used for other manipulation operations, like converting a JPEG image to PNG. Typing in the commend without any flags will give you other operations you can perform, or you can read more about it here.

Could you share the command?
Oh i see them now. the didn’t appear the first time
Might be a good idea to add a resampler filter to keep quality
Why does ‘mogrify’ need to be run as sudo? Seems like bad practice to me.
It doesn’t need to be. It may be required depending on file permissions (since in my case, the files were uploaded by the
www-datauser, and I was logged in as another).