You are here

Find and Image Magick

I ran into a problem where a site user had upload a bunch of larger images that ended up crashing PHP as 196Mb of memory was not enough for image styles to resize the images. To the rescue comes the find command combined with ImageMagick's convert program to resize the images.

Resize images

A Drupal site files directory may contain many files and finding all files larger then a given size can be difficult, but not when find is available. To use find to find all files in the current directory and that's lager then 1Mb and have a width larger then 724 pixels. To do this we need to use find's exec option to execute ImageMagick's convert on each file found (the {} is substituted by the file found).

So this command finds all files in the current directory (.) with a size bigger (+) then 1Mb and calls convert on each file.

~$ find . -size +1024k -exec convert {} -resize '724>' {} \;

If you want to see which files you are converting before doing it you can run this command. It will show detailed information for each file by using the ls command.

~$ find . -size +1024k -exec ls -lh {} \;

Extra find command

This command finds all files that have not been modified within the last 7 days and executes the convert command on each file. If you want to find the files that have been modified within the last 7 days simply use -7.

~$ find . -mtime +7 -name '*.jpg' -exec convert {} -resize '724>' {} \;

References

Tags

Images ImageMagick Find

Comments

Thank you VERY MUCH for that!!!!!!!!!!!!!!!!

Add new comment