How to resize via a cron job?

Questions and postings pertaining to the development of ImageMagick, feature enhancements, and ImageMagick internals. ImageMagick source code and algorithms are discussed here. Usage questions which are too arcane for the normal user list should also be posted here.
Post Reply
bambinou
Posts: 1
Joined: 2011-01-23T14:01:43-07:00
Authentication code: 8675308

How to resize via a cron job?

Post by bambinou »

Hello,

I am having a real problem, my customers will upload pictures in one of the site folder(the same one),I would like to set a cron job which(every hours) check for newly uploaded pictures and resize them by 800x600.
ImageMagik has been installed on my linux server, someone gave me this code:

#!/bin/bash

DIR=/home/username/public_html/wordpress/pictures/
TIMER=/home/username/timerfile

find $DIR -maxdepth 1 -name "*.jpg" -anewer $TIMER | xargs mogrify -resize 800x600 -quality 85

touch $TIMER


I have 2 problems with this, the person is not familiar with image magik therefore I would need someone else with a good knowledge of this software to check it out please and also what is the $timer? or the "timer file".

Any idea? Do you have an example please?


Thank you,


Ben
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: How to resize via a cron job?

Post by anthony »

Note cron runs in a seperate environment that may not be initialized in the same way as command line.

That means you may not have PATH set, or any other environment variables beyond basic system directories.
You may not even be in your home directory!
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
radilly
Posts: 2
Joined: 2011-04-29T14:20:05-07:00
Authentication code: 8675308

Re: How to resize via a cron job?

Post by radilly »

$TIMER is a reference file. The touch command resets the modification time to the current time in the last line of the script.

The "find .... -anewer $TIMER ...." clause fires when a .jpg is found with a newer timestamp than the reference file (which was reset after at the end of the last run).

The timer file itself is mostly irrelevant- the script only cares about the timestamp associated with it.

Bob


BTW - Anthony is correct that cron runs with a very minimal environment. I have the same issue as was looking for a quick fix. I'm pretty sure I can sort it out, and I'll share the methodology when I do, since others seem to have run into it...
radilly
Posts: 2
Joined: 2011-04-29T14:20:05-07:00
Authentication code: 8675308

Re: How to resize via a cron job?

Post by radilly »

I was doing some more cron work and was reminded that I never posted the detail
I had in mind. Here's how I got the lowdown on the cron shell environment...

I setup a temporary cron job to invoke a shell script to create a little web page:

Code: Select all

#!/bin/bash

DIR="/home/radilly/html/test"
OUT_FILE="${DIR}/cron_env_check.htm"

touch ${OUT_FILE} ; # create file if it doesn't exist

echo "<PRE>" >> ${OUT_FILE}

echo "<B> env </B>" >> ${OUT_FILE}
env | sort >> ${OUT_FILE}

echo "<B> which convert / version </B>" >> ${OUT_FILE}
which convert >> ${OUT_FILE}
convert -version >> ${OUT_FILE}

echo "<B> which mogrify / version </B>" >> ${OUT_FILE}
which mogrify >> ${OUT_FILE}
mogrify -version >> ${OUT_FILE}
The output looks something like:

Code: Select all

  env 
HOME=/home/radilly/
LOGNAME=radilly
MAILTO=
PATH=/usr/bin:/bin
PWD=/home/radilly/
SHELL=/bin/sh
SHLVL=3
USER=radilly
_=/usr/bin/env

  which convert / version
/usr/bin/convert
Version: ImageMagick 5.5.6 04/01/03 Q16 http://www.imagemagick.org
Copyright: Copyright (C) 2003 ImageMagick Studio LLC

  which mogrify / version 
/usr/bin/mogrify
Version: ImageMagick 5.5.6 04/01/03 Q16 http://www.imagemagick.org
Copyright: Copyright (C) 2003 ImageMagick Studio LLC
The interesting results are the PATH and PWD variables and the which command output.

Under cron we see:
PATH=/usr/bin:/bin
When I login interactively I see:
PATH=/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/home/radilly/bin
Any script or command not in /usr/bin or /bin would need the full path spelled
out explicitly when running under cron. In a login shell, 3 additional locations are
searched. It would be unusual for tools and utilities to be found in /usr/local/bin

The cron process will typically log into my HOME directory:
/home/radilly/
but my script ... or at least the interactive commands which worked, ran in
/home/radilly/html/test
When I reference at files in this directory under cron, I need to either give the
full path, or the relative path html/test. I prefer the former. (This, BTW
is where I messed up in my first attempt.
)

Finally which convert prints the full path to convert.
/usr/bin/convert
Actually, the fact that the path was printed means /usr/bin is in the PATH.
Were it not, which would fail. It actually makes more sense to run which convert
in a login shell and note the path. Technically you shouldn't need the path in this
environment, but I would tend to specify it, i.e. /usr/bin/convert rather
than just convert.

It's a little work, but running a little test script (repeatedly if need be) under cron
has almost always help me dope out what's going on.

Bob

FYI- my application was to make a 50% version of a web cam image to post on
http://www.the-dillys.org/WX/. I could resize in the html, but I
wanted to minimze the upload time. On average they should be 1/4 the file size.
Post Reply