Difference between revisions of "Image Resizing"

From IndyMedia
Jump to: navigation, search
(Script for incron)
Line 15: Line 15:
 
DIR=/imc/mir/mayday/images/$YEAR/$MONTH
 
DIR=/imc/mir/mayday/images/$YEAR/$MONTH
  
echo "$DIR IN_CLOSE_WRITE /usr/local/bin/exif-strip-resize \$@/\$#"  
+
echo "$DIR IN_CLOSE_WRITE /usr/local/bin/exif-strip-resize \$@ \$#" > /etc/incron.d/mayday.conf
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Line 21: Line 21:
  
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
0 0 1 * * /usr/local/bin/incron-update && /etc/init.d/incron restart
+
0 0 1 * * /usr/local/bin/incron-update ; /etc/init.d/incron restart
 
</syntaxhighlight>
 
</syntaxhighlight>
  

Revision as of 22:44, 23 June 2011

Script for incron

incron can be set up to run on file creation but you have to be careful not to cause loops by creating new content in the same directory, see:

Update the incron tab at the start of each month, run a script called /usr/local/bin/incron-update which contains:

#!/bin/bash
 
YEAR=$(date +%Y)
MONTH=$(date +%m)
DIR=/imc/mir/mayday/images/$YEAR/$MONTH
 
echo "$DIR IN_CLOSE_WRITE /usr/local/bin/exif-strip-resize \$@ \$#" > /etc/incron.d/mayday.conf

Via this crontab:

0 0 1 * * /usr/local/bin/incron-update ; /etc/init.d/incron restart

Save this script as /usr/local/bin/exif-strip-resize:

#!/bin/bash
 
# this script takes the full path to a jpeg file 
# and the filename, paased from incrond
# it scale big ones and also remove all exif metadata
# from the jpeg
 
# sub-directory for sacled images
SCALED=scaled
 
# check for the first command line argument
if [[ $1 ]]; then
  # check it's a directory
  if [[ -d $1 ]]; then
    DIR=$1
  else
    echo "$1 isn't a directory"
    exit
  fi
else
  echo "You need to specify the path to a file as the first argument" 
  exit
fi
 
# make a directory for the scaled images
if [[ -d $DIR/$SCALED ]]; then
  echo $DIR/$SCALED exists
else
  mkdir $DIR/$SCALED
fi
 
if [[ $2 ]]; then
  FILE=$2
  echo file is $DIR/$FILE
  # check the file exists
  if [[ -f $DIR/$FILE ]]; then
    # check if it's really a jpg
    TYPE=$(file -bi $DIR/$FILE)
    if [[ $TYPE = "image/jpeg; charset=binary" ]]; then
 
      # remove exif metadata preserving the image date
      DATE=$(date -r $DIR/$FILE)
      exiv2 rm "$DIR/$FILE"
 
      # check is a sub-directory for scaled images is writable 
      if [[ -w $DIR/$SCALED ]]; then
 
        # generate scaled versions of images if they are greater than 640px wide 
        # or 1024px high, maintaining the aspect ratio
        WIDTH=$(identify -format %w $DIR/$FILE)
 
        if [[ $WIDTH -gt 640 ]]; then
           convert "$DIR/$FILE" -resize 640x1024 -format jpeg -quality 80 "$DIR/$SCALED/$FILE"
           touch -d "$DATE" "$DIR/$SCALED/$FILE"
        fi
 
        # reset the file date
        touch -d "$DATE" "$DIR/$SCALED/$FILE"
 
      else
        echo $DIR/$SCALED is not writable
      fi
    else
      echo "$DIR/$FILE is not a jpeg"
    fi
  else
    echo "$DIR/$FILE does not exist"
  fi
else
  echo "$DIR/$FILE isn't a file"
fi

Script to run via cron

This is a script to be run via cron, perhaps once a min to remove exif metadata and to scale jpeg images if they need it.

#!/bin/bash
 
# this script takes a directory with some jpeg files in and 
# will scale the big ones and also remove all exif metadata
# from the jpegs
 
# you can set an optional AGE vairable here, if it's set then only 
# files that are newer than AGE will be looked at -- this is handy 
# if the script is running on a cron job, set it to a tile just longer
# than the cron interval, use mins for the value  
 
AGE=200
 
# Directory of images is passed to this script via $1, if it's 
# not then we assume that we should run in the current directory.
if [[ $1 ]]; then
  # change to the directory where the images are
  cd $1
  DIR=$(pwd)
else
  DIR=$(pwd)
fi
 
 
# get a list of the files, omitting ones that have already been scaled
if [[ $AGE ]]; then
  FILES=$(find -P $DIR -type f -cmin -$AGE -name '*.jpg' ! -name '*\.indyscaled\.jpg')
else
  FILES=$(find -P $DIR -type f -name '*.jpg' ! -name '*\.indyscaled\.jpg')
fi
 
for file in $FILES; do
#echo file: $file
 
  # check the file exists
  if [[ -f $file ]]; then
 
    # check if we can write to the file
    if [[ -w $file ]]; then
 
      # check if it's really a jpg
      TYPE=$(file -bi $file)
      if [[ $TYPE = "image/jpeg; charset=binary" ]]; then
 
         # remove exif metadata preserving the image date
         DATE=$(date -r $file)
         exiv2 rm "$file"
 
         # generate scaled versions of images if they are greater than 640px wide 
         # or 1024px high, maintaining the aspect ratio
         WIDTH=$(identify -format %w $file)
         if [[ $WIDTH -gt 640 ]]; then
            echo scalling $file
            convert "$file" -resize 640x1024 -format jpeg -quality 80 "$file.indyscaled.jpg"
            touch -d "$DATE" "$file.indyscaled.jpg"
         fi
 
         # reset the file date
         touch -d "$DATE" "$file"
 
      else
 
        echo $file is not a jpeg
 
      fi
 
    else
 
      echo $file is not writable
 
    fi
 
  else
 
    echo $file does not exist 
 
  fi
 
done