Hi,
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:For: JPEGCode: Select all
nice -n 15 find . -type f -size +100000c -name '*.jpg' -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.Code: Select all
nice -n 15 find . -type f -size +100000c -name '*.jpeg' -exec convert {} -sampling-factor 4:2:0 -strip -quality 85 -interlace JPEG -colorspace RGB {} \;
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.
Thanks
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:
Code: Select all
nice -n 15 find . -type f -size +100000c -name "*.jpg" -exec bash -c '[[ $(identify -format "%c" "{}") != *optimised* ]] && { echo Optimising "{}"; convert "{}" -filter Triangle -sampling-factor 4:2:0 -define filter:support=2 -unsharp 0.25x0.25+8+0.065 -dither None -quality 85 -define jpeg:fancy-upsampling=off -define png:compression-filter=5 -define png:compression-level=9 -define png:compression-strategy=1 -define png:exclude-chunk=all -interlace none -colorspace RGB -strip -set comment "optimised" "{}"; }' \;
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.[/b]