#!/usr/bin/perl -w
###############################################################################
# 
# webbilder.pl
#
# converts picture-size 
#
# It searches for jpg-files in the given directory. 
# Found jpg-files will be stripped from any comment, ITPC- or EXIF-data. Then 
# it will be resized to 600x800/800x600 and stored on the subdirectory "gross"
# and resized to 271x370/370x271 and stored in the subdirectory "klein". 
# Resizing is done regarding to alignment.
# The resized format is usually used at my webpage.
# Files with the same name will be overwritten.
# 
#
# 10.07.2004	Christian 'Rana' Schlegel
###############################################################################

# import ImageMagick
use Image::Magick;


use strict;

# define variables
my ($image, $x);

my $pathname;
my @jpgfiles;
my $picfile;
my ($width, $height);

exit 1 if !$ARGV[0];

$pathname = $ARGV[0];

-d "$pathname/klein" or print "Directory $pathname/klein doesn't exist: will be created now\n";
-d "$pathname/klein" or mkdir "$pathname/klein";
-d "$pathname/gross" or print "Directory $pathname/gross doesn't exist: will be created now\n";
-d "$pathname/gross" or mkdir "$pathname/gross";

opendir (PICPATH, $pathname) or die "Can't open the directory $pathname: $!\n";
@jpgfiles = grep /\.jpg$/i, readdir PICPATH;
closedir PICPATH;
 foreach $picfile (@jpgfiles) {
						    # define and read the image
    $image = Image::Magick->new;
    $x = $image->Read("$pathname" . "/" . "$picfile");

						    # Warn if the image couldn't be read
    warn "$x" if "$x";
		    
						    # Erase comment, IPTC and EXIF-data
						    # Löscht Kommentar, IPTC- und EXIF-Daten aus dem Bild
    $x = $image->Strip;

    ($width, $height) = $image->Get('columns', 'rows');
#    print "$picfile: Breite x Höhe: $width x $height\n";


						    # Set a comment for the image
						    # Schreibt eine Kommentar in das Bild	    
    $x = $image->Comment("Done with ImageMagick");

    if ($width > $height) {
						    # Resize the image
	$x = $image->Resize(width=>800, height=>600);

						    # Warn if the image couldn't be resized
	warn "$x" if "$x";
    }
    else {
						    # Resize the image
	$x = $image->Resize(width=>600, height=>800);

						    # Warn if the image couldn't be resized
	warn "$x" if "$x";
    }
	

						    # Write the image to a file compressed 
						    # with JPEG-algorithm and quality 50
    $x = $image->Write(filename=>"$pathname" . "/gross/" . "$picfile", compression=>'JPEG', quality=>'70');

						    # Warn if the image couldn't be written.
    warn "$x" if "$x";

    if ($width > $height) {
						    # Resize the image
        $x = $image->Resize(width=>370, height=>271);

						    # Warn if the image couldn't be resized
	warn "$x" if "$x";
    }
    else {
						    # Resize the image
        $x = $image->Resize(width=>271, height=>370);

						    # Warn if the image couldn't be resized
	warn "$x" if "$x";
    }

						    # Write the image to a file compressed 
						    # with JPEG-algorithm and quality 50
    $x = $image->Write(filename=>"$pathname" . "/klein/" . "$picfile", compression=>'JPEG', quality=>'50');

						    # Warn if the image couldn't be written.
    warn "$x" if "$x";
}
