Compressing images on my Server website ONCE ONLY via CRON TAB

IMagick is a native PHP extension to create and modify images using the ImageMagick API. ImageMagick Studio LLC did not write nor does it maintain the IMagick extension, however, IMagick users are welcome to discuss the extension here.
Post Reply
nexus4.alii1991
Posts: 4
Joined: 2018-12-19T17:26:05-07:00
Authentication code: 1152

Compressing images on my Server website ONCE ONLY via CRON TAB

Post by nexus4.alii1991 » 2018-12-19T17:40:04-07:00

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:

Code: 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 {} \;
For: JPEG

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 {} \;
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.

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" "{}"; }' \;
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.[/b]
Last edited by nexus4.alii1991 on 2018-12-20T16:47:26-07:00, edited 5 times in total.

User avatar
fmw42
Posts: 25272
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Compressing images on my Server website ONCE ONLY

Post by fmw42 » 2018-12-19T19:29:53-07:00

ImageMagick can show the compression method and/or quality value from the verbose information for the file using

Code: Select all

identify -verbose image
But, the best way is to put a comment (or label) into your image's meta data when you create the image. ImageMagick can do that using

Code: Select all

convert image -set comment "your comment" image
You can then test for the command by reading it as

Code: Select all

convert image -format "%c" info: or identify -format "%c" image
But you may be better off using EXIFTOOL to do all this.

snibgo
Posts: 11928
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Compressing images on my Server website ONCE ONLY

Post by snibgo » 2018-12-19T20:19:15-07:00

You need something that distinguishes between files that you have compressed and files you haven't. As Fred says, you could do this by recording a comment in images you have changed.

Personally, I would do it with directories. Have an "incoming" directory and a "compressed" directory. You don't seem to want to keep the uncompressed files, so write a cron job that reads each incoming image, writes to the "compressed" directory, and unlinks the incoming image.

Beware that this (or your current process) should be done after any writing process has finished writing the incoming image.
snibgo's IM pages: im.snibgo.com

nexus4.alii1991
Posts: 4
Joined: 2018-12-19T17:26:05-07:00
Authentication code: 1152

Re: Compressing images on my Server website ONCE ONLY

Post by nexus4.alii1991 » 2018-12-20T09:31:00-07:00

I agree with you, to add a comment or to use identify in order to find out which images already compressed. But how can i do this and apply it in the same command written above. As you can see i am doing the compression using one command in cron job every 24 hours, so how can i tell this command to check for this comment before compressing it?

Also note that i am using the strip option in my compression so comments might be removed if added. That means i have to do the check for the file before the strip.

nexus4.alii1991
Posts: 4
Joined: 2018-12-19T17:26:05-07:00
Authentication code: 1152

Re: Compressing images on my Server website ONCE ONLY

Post by nexus4.alii1991 » 2018-12-20T10:37:03-07:00

While looking around i found a proposed solution but i need to make the code work it is as following:

Code: Select all

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?

nexus4.alii1991
Posts: 4
Joined: 2018-12-19T17:26:05-07:00
Authentication code: 1152

Re: Compressing images on my Server website ONCE ONLY

Post by nexus4.alii1991 » 2018-12-20T16:42:38-07:00

nexus4.alii1991 wrote:
2018-12-20T10:37:03-07:00
While looking around i found a proposed solution but i need to make the code work it is as following:

Code: Select all

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?
Solved it above look at the new code

Post Reply