#!/usr/bin/perl -w
#
# make_variant: create a resized variant of each image named in @ARGV
#
# usage e.g.:
#     make_variant 128 tn *.jpg
#     make_variant 300 sm *.jpg
#     make_variant 480 med *.jpg
#
# WARNING: this script prunes embedded metadata from the variants it
# generates, to reduce image sizes. It also has a bunch of stuff hardcoded
# that you will want to change before using it, e.g. my name and URIs.
#
# Gerald Oskoboiny, 27 Nov 2001
#
# source: http://impressive.net/software/photo/source/make_variant
#
# $Id: make_variant,v 1.16 2022/10/26 22:36:43 gerald Exp $
#

use strict;

my $height		= shift;		# arg 1
my $suffix		= "-" . shift;		# arg 2
my $identify		= "identify";
my $convert		= "convert";
my $quality_opts	= " -quality 75";
my $verbose		= 0;

my $exiftool		= '/opt/local/bin/exiftool';
( -x $exiftool ) or die "$exiftool does not exist or is not executable";
$exiftool .= " -q -overwrite_original ";

foreach my $file (@ARGV) {			# remaining args

    if ( ! -r $file ) {
        print STDERR "Can't read $file! skipping it...\n";
	next;
    }

    my $variant_name = $file;
    $variant_name =~ s/-(sq|tn|sm|med)\.jpg$/.jpg/; # avoid variants of variants

    my $uri = "http://impressive.net/photos";
    if ( $ENV{PWD} =~ m,^/home/gerald/www/people/gerald/(.*), ) {
	$uri = "http://impressive.net/people/gerald/" . $1 . "/". $variant_name;
	1 while $uri =~ s,[^\.^/]+/\.\./,,;
	$uri =~ s/\.jpg$/-sm.html/;
    }

    $variant_name =~ s/\.jpg/${suffix}.jpg/;

    if ( -f $variant_name ) {
        print STDERR "Variant $variant_name already exists! skipping it...\n"
	    if $verbose;
	next;
    }

    my $rotate_opts = "";
    chomp( my $orientation = `epinfo -T Orientation $file 2>/dev/null`);
    if ( length $orientation and $orientation == 6 ) {
	$rotate_opts .= " -rotate 90";
    }
    elsif ( length $orientation and $orientation == 8 ) {
	$rotate_opts .= " -rotate 270";
    }

    my $shave_opts = "";
    if ( $suffix eq "-sq" ) {	# special handling for square-cropped images
	my ($width,$height) = split('x',`$identify $file | cut -d" " -f3`);
	chomp($height);
	my $shave_x = ($width - $height) / 2; $shave_x = 0 if $shave_x < 0;
	my $shave_y = ($height - $width) / 2; $shave_y = 0 if $shave_y < 0;
	$shave_opts = " -shave ${shave_x}x${shave_y} ";
    }
    # @@ add support for varying gravity for sq regions
    #
    # command line kludge to regenerate sq.jpg's with top gravity:
    # rm the sq.jpg's you want top-gravitied, then run:
    # for med in *med.jpg ; do sq=`echo $med | sed 's/med/sq/'`; [ -f $sq ] && continue; convert -crop 320x320+0+0 $med temp; convert -geometry 100x100 temp $sq ; rm temp; jhead -dt $sq; done

    if ( $suffix =~ "-(sq|tn)" ) {	# special handling for smaller variants
	$quality_opts = " -quality 50 -strip";
    }

    system "$convert $shave_opts $quality_opts $rotate_opts +repage -resize x$height $file $variant_name";

    if ( $suffix eq "-med" ) { # others are derived from *-med variants
	# set the copyright field and remove orientation since it was rotated
	system "$exiftool -Orientation= -d %Y -copyright'<Copyright \$createdate Gerald Oskoboiny https://impressive.net/photos' $variant_name";
	# copy the copyright to the comment field
	system "$exiftool -comment'<Copyright' $variant_name";
    }

    print "Done $variant_name\n" if $verbose;

}

exit;

#
# changelog:
#
# $Log: make_variant,v $
# Revision 1.16  2022/10/26 22:36:43  gerald
# fix logic of removing orientation metadata after rotation
#
# Revision 1.15  2022/10/25 23:35:03  gerald
# fix bug in previous commit and simplify copyright
#
# Revision 1.14  2022/10/25 22:38:23  gerald
# strip embedded thumbnails and reduce quality of tiny images to reduce file sizes
#
# Revision 1.13  2009/06/10 02:12:19  gerald
# removed -dither since more modern imagemagicks seem to complain about it
# and have it enabled by default anyway
#
# Revision 1.12  2008-11-08 16:06:17  gerald
# use more sensible quality settings; started stripping embedded metadata;
# added a copyright and comments field with a pointer to the web page
# associated with the image
# http://impressive.net/archives/fogo/20081108160459.GE30192@impressive.net
#
# Revision 1.11  2006/10/10 04:16:24  gerald
# bug in logic of ignoring orientation from med files
#
# Revision 1.10  2006/10/09 04:42:51  gerald
# added +repage and removed -size since no worky with more modern ImageMagick
# distributed with ubuntu dapper; also a couple changes to deal with
# make_variants now generating smaller variants based on foo-med.jpg
#
# Revision 1.9  2006/01/09 06:53:51  gerald
# avoid generating variants of variants
#
# Revision 1.8  2005/12/09 21:41:17  gerald
# added special handling for square-cropped images (those with suffix 'sq')
#
# Revision 1.7  2005/01/27 20:58:57  gerald
# compensate for change in jhead's output
#
# Revision 1.6  2004/12/03 14:57:00  gerald
# added a pointer to block_until_load_less_than source
#
# Revision 1.5  2003/06/16 05:19:35  gerald
# (hopefully) speed things up by specifying -size before -resize; see
# http://impressive.net/archives/fogo/20030613222634.GC29059@impressive.net
#
# Revision 1.4  2002/08/28 05:56:30  gerald
# added load checker
#
# Revision 1.3  2002/08/08 03:58:03  gerald
# added a pointer to the source url
#
# Revision 1.2  2002/04/29 04:55:22  gerald
# added changelog
#

