#!/bin/sh # a small script to generate smaller images # mainly used to generate thumbnails # requires decent pnmtools installed # (c) 1999-2000 by Yves Lafon / W3C - ylafon@w3.org # Sat Jan 29 00:53:52 MET 2000 # better use of pnmsmooth # Change: Thu Jan 4 00:29:53 MET 2001 added -png # Change: Thu Apr 7 16:08:29 CEST 2005 added -sq # to create square icons by cropping the middle of the image. # $Id: anytothumb,v 1.2 2005/04/07 14:10:06 ylafon Exp $ if [ $# = 0 ]; then echo "Usage: `basename $0` [-h][-sq][-png][-size ][-x][-y] " 1>&2 exit 1 fi doHelp() { echo "Usage:" echo " -h : display help" echo " -size: specify the thumbnail maximum size in pixel of the longest edge (default 100px)" echo " -x : the above size is used only for the horizontal size" echo " -y : the above size is used only for the vertical size" echo " -sq : create a square thumbnail by cropping the middle of the original image" echo " -png : use PNG for the output" echo echo "if -x and -y are used, then the size will apply to the " echo "longest edge, like if they were not defined" } size=100 x=0 y=0 use_png=0 sq=0 smooth_threshold=3 for arg do case "$1" in "-size" ) shift; size=$1; shift;; "-x" ) x=1; shift;; "-y" ) y=1; shift;; "-png") use_png=1; shift;; "-sq") sq=1; shift;; "-h") doHelp ; exit 1 ;; "*" ) break esac done cut_cmd="cat" if [ $use_png -eq 1 ]; then pre_cmd="cat" out_cmd="pnmtopng -downscale -compression 9" else pre_cmd="ppmquant -fs 256" out_cmd="ppmtogif" fi file_type=`file "$1" | awk '{print $2}'` case "$file_type" in "JPEG" ) info_cmd="djpeg -pnm" trans_cmd="djpeg -pnm -dct float" ;; "GIF" ) info_cmd=giftopnm trans_cmd=giftopnm ;; * ) ext=`echo "$1" | sed 's/.*\.//g' | tr '[A-Z]' '[a-z]'` case "$ext" in "jpg" | "jpeg" ) info_cmd="djpeg -pnm" trans_cmd="djpeg -pnm -dct float" ;; "gif" ) info_cmd=giftopnm trans_cmd=giftopnm ;; *) info_cmd=anytopnm trans_cmd=anytopnm ;; esac esac # get the size info raw_info=`$info_cmd "$1" | pnmfile ` x_size=`echo $raw_info | awk '{print $4}'` y_size=`echo $raw_info | awk '{print $6}'` if [ $sq -eq 1 ]; then if [ $x_size -gt $y_size ]; then start_x=`expr \( $x_size - $y_size \) / 2` start_y=0 width_xy=$y_size else start_x=0 start_y=`expr \( $y_size - $x_size \) / 2` width_xy=$x_size fi cut_cmd="pnmcut -left=${start_x} -top=${start_y} -width=${width_xy} -height=${width_xy}" x_size=$width_xy y_size=$width_xy fi # transform and generate the small gif case "$size" in *x* ) x_nsize=`echo $size | sed 's/x.*//'` y_nsize=`echo $size | sed 's/.*x//'` scale_cmd="-xsize $x_nsize -ysize $y_nsize" x_factor=`expr $x_size / $x_nsize` y_factor=`expr $y_size / $y_nsize` ix_factor=`expr $x_nsize / $x_size` iy_factor=`expr $y_nsize / $y_size` ;; * ) x_factor=0 y_factor=0 ix_factor=0 iy_factor=0 if [ $x = $y ]; then if [ $x_size -gt $y_size ]; then scale_cmd="-xsize $size" x_factor=`expr $x_size / $size` ix_factor=`expr $size / $x_size` else scale_cmd="-ysize $size" y_factor=`expr $y_size / $size` iy_factor=`expr $size / $y_size` fi elif [ $x = 1 ]; then scale_cmd="-xsize $size" x_factor=`expr $x_size / $size` ix_factor=`expr $size / $x_size` elif [ $y = 1 ]; then scale_cmd="-ysize $size" y_factor=`expr $y_size / $size` iy_factor=`expr $size / $y_size` fi esac if [ $x_factor -ge $smooth_threshold -o $y_factor -ge $smooth_threshold ]; then $trans_cmd "$1" 2>/dev/null | $cut_cmd 2> /dev/null | pnmsmooth 2>/dev/null | pnmscale $scale_cmd 2>/dev/null | $pre_cmd 2> /dev/null | $out_cmd 2>/dev/null elif [ $ix_factor -ge $smooth_threshold -o $iy_factor -ge $smooth_threshold ]; then $trans_cmd "$1" 2>/dev/null | $cut_cmd 2> /dev/null | pnmscale $scale_cmd 2>/dev/null | pnmsmooth 2>/dev/null | $pre_cmd 2> /dev/null | $out_cmd 2>/dev/null else $trans_cmd "$1" 2>/dev/null | $cut_cmd 2> /dev/null | pnmscale $scale_cmd 2>/dev/null | $pre_cmd 2> /dev/null | $out_cmd 2>/dev/null fi