I have a website hosted on a shared hosting package.
I am using Imagick to compress all images on my websites jpg and jpeg that have a size over 100kb, the command i am using is:
For JPG:
nice -n 15 find . -type f -size +100000c -name '*.jpg' -exec convert {} -sampling-factor 4:2:0 -strip -quality 85 -interlace JPEG -colorspace RGB {} ;
For: JPEG
nice -n 15 find . -type f -size +100000c -name '*.jpeg' -exec convert {} -sampling-factor 4:2:0 -strip -quality 85 -interlace JPEG -colorspace RGB {} ;
I setup these two commands at my cron jobs in my cpanel to run every day once.
The problem is that if i have an image that size is large (ex above 1Mb), and the compressed image is still above 100kb the script will apply the compression again and again on it until it becomes less than 100kb. This results in a very lowwwww resolution and bad quality.
How can i tell imagick command to compress only images that have not been compressed previously. I thought about finding only files modified last 24 hours but i dont think it is the right solution since when the script runs it will take time and the newly compressed images might be in the same 24 hour.
The solution i think will work is using the “identify -verbose” to check quality of the image, if it is 85% then don’t compress. how can i write this in the same code above. Or if you have any other solution it is appreciated.
While looking around i found a proposed solution but i need to make the code work it is as following:
nice -n 15 find . -type f -size +100000c -name '*.jpg' | [[ $(identify -format %c {}) != *optimised* ]] && { echo Optimising {}; convert {} -set comment "optimised" -sampling-factor 4:2:0 -strip -quality 85 -interlace JPEG -colorspace RGB {} ; }
i have a problem with this code, the return of the find normally is stored in {} but in this case it is showing an error.
Can you solve this and make it work?
2
Answers
I finally been able to do it, i created a command that you can place in the cron tab in your cpanel and will be able to compress all images on your webserver. I took into consideration that the web server might be slow with low resources so i used the command "nice -15" to prevent this script from overloading the server CPU and memory. Also i set it to run every 24 hours and added a condition that if the image is already compressed dont compress it again. just enable the imagick in php in your cpanel then go to cron tab and add this command:
this command works on jpg, jpeg and png. jsut replace the string inside ".jpg" to the image format you want to compress. You can set the quality you desire here it is 85, also the Imagick options i added is from an old post i found on the internet but i modified it since if the file being compressed is greater than 1mb it will take over 10 to 20 minutes to compress it this is because of the "-posterize 136" option which i removed from the above code. You can add an option for this command to only find files that have been modified the last 24 hours to work on, this way the images previously compressed will pass through this command twice only, the third time this command runs it will not pass the images compressed three days ago to the rest of the command (will not enter the condition to check if it is optimized), this will speed up the process of going over all the files in the server, check this option ("-mmin n " File's data was last modified n minutes ago).
After i finished this i need help to optimize this command to give the best compressed picture quality, and if any one can help in using the "resize" option to resize images that are bigger than the screen width and height only (images like banners) and leave the rest images with the default size.
Assuming you cannot rename the images or maintain a second index of already compressed images. Instead of relying on the
quality
value I would suggest to explore updating/evaluating the image’s metadata (like ‘comment’ or ‘keywords’).Imagemagick’s
identify
program allows you to list/update metadata.Documentation:
https://imagemagick.org/script/identify.php
https://imagemagick.org/script/command-line-options.php#set