Difference between revisions of "Image Resizing"

From IndyMedia
Jump to: navigation, search
(oops)
Line 1: Line 1:
 
== Script for incron ==
 
== Script for incron ==
  
incron can be set up to run on file creation, see:
+
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:
  
 
* http://inotify.aiken.cz/?section=incron&page=doc&lang=en
 
* http://inotify.aiken.cz/?section=incron&page=doc&lang=en
 
* http://www.cyberciti.biz/faq/linux-inotify-examples-to-replicate-directories/
 
* http://www.cyberciti.biz/faq/linux-inotify-examples-to-replicate-directories/
  
The incron tab could be set to have the next years or so of directories to watch or a script could be written to run via cron to update the incron tab at the start of each month, for example:
+
Update the incron tab at the start of each month, run a script called /usr/local/bin/incron-update which contains:
  
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
Line 18: Line 18:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
And this crontab:
+
Via this crontab:
  
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
Line 24: Line 24:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Image directories are created in this manner, I expect, only as there is content to put in them.
+
Save this script as /usr/local/bin/exif-strip-resize:
 
+
/imc/mir/mayday/images/2011/06
+
 
+
And are owned by tomcat6:mayday-tomcat
+
 
+
This script could be used to do the scaling of the images:
+
  
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
Line 36: Line 30:
  
 
# this script takes the full path to a jpeg file  
 
# 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
 
# it scale big ones and also remove all exif metadata
 
# from the jpeg
 
# from the jpeg
  
# check for a command line argument
+
# sub-directory for sacled images
 +
SCALED=scaled
 +
 
 +
# check for the first command line argument
 
if [[ $1 ]]; then
 
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
  
   FILE=$1
+
# 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
 
   # check the file exists
   if [[ -f $FILE ]]; then
+
   if [[ -f $DIR/$FILE ]]; then
 +
    # check if it's really a jpg
 +
    TYPE=$(file -bi $DIR/$FILE)
 +
    if [[ $TYPE = "image/jpeg; charset=binary" ]]; then
  
    # check if we can write to the file
+
      # remove exif metadata preserving the image date
    if [[ -w $FILE ]]; then
+
      DATE=$(date -r $DIR/$FILE)
 +
      exiv2 rm "$DIR/$FILE"
  
       # check if it's really a jpg
+
       # check is a sub-directory for scaled images is writable
      TYPE=$(file -bi $FILE)
+
       if [[ -w $DIR/$SCALED ]]; then
       if [[ $TYPE = "image/jpeg; charset=binary" ]]; then
+
  
        # remove exif metadata preserving the image date
+
        # generate scaled versions of images if they are greater than 640px wide
        DATE=$(date -r $FILE)
+
        # or 1024px high, maintaining the aspect ratio
        exiv2 rm "$FILE"
+
        WIDTH=$(identify -format %w $DIR/$FILE)
  
        # generate scaled versions of images if they are greater than 640px wide
+
        if [[ $WIDTH -gt 640 ]]; then
        # or 1024px high, maintaining the aspect ratio
+
          convert "$DIR/$FILE" -resize 640x1024 -format jpeg -quality 80 "$DIR/$SCALED/$FILE"
        WIDTH=$(identify -format %w $FILE)
+
          touch -d "$DATE" "$DIR/$SCALED/$FILE"
        if [[ $WIDTH -gt 640 ]]; then
+
        fi
            convert "$FILE" -resize 640x1024 -format jpeg -quality 80 "$FILE.indyscaled.jpg"
+
            touch -d "$DATE" "$FILE.indyscaled.jpg"
+
        fi
+
  
        # reset the file date
+
        # reset the file date
        touch -d "$DATE" "$FILE"
+
        touch -d "$DATE" "$DIR/$SCALED/$FILE"
  
 
       else
 
       else
 
+
         echo $DIR/$SCALED is not writable
         echo $file is not a jpeg
+
 
+
 
       fi
 
       fi
 
 
     else
 
     else
 
+
       echo "$DIR/$FILE is not a jpeg"
       echo $file is not writable
+
 
+
 
     fi
 
     fi
 
 
   else
 
   else
 
+
     echo "$DIR/$FILE does not exist"
     echo $file does not exist  
+
 
+
 
   fi
 
   fi
 
 
else
 
else
   echo "You need to specify the full path to a file on the command line to run the script"  
+
   echo "$DIR/$FILE isn't a file"
 
fi
 
fi
  

Revision as of 22:39, 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 \$@/\$#"

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