#!/usr/bin/perl -w use strict; use CGI qw/:standard/; use Template; use Image::Magick; use Image::MetaData::JPEG; my $config = { ABSOLUTE=>1, RELATIVE=>1, INCLUDE_PATH=>"./src" }; my $template = Template->new($config); my %template_vars; my $thumbspp = 8; my $pic_dir = "gallerys/Gallery_1"; my @photos; my $output_dir = "./gallerys/Gallery_1"; my $i=0; $template_vars{'photos'} = \@photos; my $thumb_width = 180; my $thumb_height = 135; my $norm_width = 600; my $gallerys_dir = "./gallerys"; my $gallerys_url = "gallerys"; my $src_dir = "./src"; opendir (GALLERYS, $gallerys_dir) or die $!; foreach my $gallery_dir (sort(readdir GALLERYS)) { $template_vars{'gallery'} = {title=>$gallery_dir,url=>"$gallerys_url/$gallery_dir"}; $template_vars{'gallery'}->{'title'}=~s/_/ /g; $template_vars{'gallery'}->{'page'} = 0; my $gallery_url = "$gallerys_url/$gallery_dir"; $gallery_dir = "$gallerys_dir/$gallery_dir"; if (-d $gallery_dir && $gallery_dir!~/^$gallerys_dir\/\.\.?$/) { print "Processing $gallery_dir...\n"; opendir (GALLERY, "$gallery_dir/photos") or die $!; foreach (sort(readdir GALLERY)) { if ($_!~/^\./) { if ($_=~/^(.*)\.jpg$/ && $_!~/\.thumb\.jpg$/) { my $file = $1; push (@photos,get_metadata({name=>$1,filepath=>"$gallery_dir/photos/",filename=>"$file.jpg",large=>"$gallery_url/photos/$file.jpg",thumb=>"$gallery_url/photos/$file.thumb.jpg",html=>"$gallery_url/photos/$file"})); $i++; if (!(-e "$gallery_dir/photos/$file.shtml") || (stat("$gallery_dir/photos/$file.shtml"))[9] < (stat("$gallery_dir/photos/$file.jpg"))[9]) { print "Creating $gallery_dir/photos/$file.shtml...\n"; $template_vars{'base_prefix'} = "../../../"; $template_vars{'photo'} = $photos[$#photos]; $template->process("$src_dir/photo.html.tt2",{%template_vars},"$gallery_dir/photos/$file.shtml") or die $template->error(); } if (!(-e "$gallery_dir/photos/$file.thumb.jpg") || (stat("$gallery_dir/photos/$file.thumb.jpg"))[9] < (stat("$gallery_dir/photos/$file.jpg"))[9]) { my $image = Image::Magick->new; print "Creating $gallery_dir/photos/$file.thumb.jpg...\n"; $image->Read(filename=>"$gallery_dir/photos/$file.jpg"); my $sf = $thumb_width / $image->Get('columns'); if (($image->Get('rows')*$thumb_width/$image->Get('columns')) > $thumb_height) { $sf = $thumb_height / $image->Get('rows'); } $image->Scale($sf * $image->Get('columns') ."x". $sf*$image->Get('rows')); $image->Write(filename=>"$gallery_dir/photos/$file.thumb.jpg"); $image = Image::Magick->new; $image->Read(filename=>"$gallery_dir/photos/$file.jpg"); if ($image->Get('columns') > $norm_width) { print "Shrinking $gallery_dir/photos/$file.jpg...\n"; if (($image->Get('columns')) > ($image->Get('rows'))) { $image->Scale($norm_width."x".($norm_width*$image->Get('rows')/$image->Get('columns'))); } else { $image->Scale(($norm_width*(3/4)*$image->Get('columns')/$image->Get('rows'))."x".(3/4)*$norm_width); } $image->Write(filename=>"$gallery_dir/photos/$file.tmp.jpg"); rename("$gallery_dir/photos/$file.jpg","$gallery_dir/photos/.$file.orig.jpg"); rename("$gallery_dir/photos/$file.tmp.jpg","$gallery_dir/photos/$file.jpg"); } } if ($i % $thumbspp == 0) { print "Creating $gallery_dir/".($i/$thumbspp).".shtml...\n"; $template_vars{'base_prefix'} = "../../"; $template_vars{'gallery'}->{'page'}++; $template->process("$src_dir/gallery.html.tt2",{%template_vars},"$gallery_dir/".($i/$thumbspp).".shtml"); undef(@photos); } } elsif ($_=~/^(.*)\.shtml$/) { unless (-e "$gallery_dir/photos/$1.jpg") { print "Could not find $gallery_dir/photos$1.jpg therefore removing $gallery_dir/photos/$1.shtml\n"; unlink ("$gallery_dir/photos/$1.shtml") } } elsif ($_=~/^(.*)\.thumb\.jpg$/) { unless (-e "$gallery_dir/photos/$1.jpg") { print "Could not find $gallery_dir/photos$1.jpg therefore removing $gallery_dir/photos/$1.jpg\n"; unlink ("$gallery_dir/photos/$1.thumb.jpg"); } } } } print "Creating $gallery_dir/".($i/$thumbspp).".shtml...\n"; $template_vars{'base_prefix'} = "../../"; $template_vars{'gallery'}->{'page'}++; $template->process("$src_dir/gallery.html.tt2",{%template_vars},"$gallery_dir/".(1+($i - $i % $thumbspp)/$thumbspp).".shtml") or die ($template->error()); print "Creating $gallery_dir/nav.shtml...\n"; $template_vars{'gallery'}->{'total_pages'} = $template_vars{'gallery'}->{'page'}; $template->process("$src_dir/nav.shtml.tt2",{%template_vars},"$gallery_dir/nav.shtml") or die ($template->error()); undef(@photos); closedir GALLERY; } } closedir GALLERYS; exit(0); sub get_metadata { # Create a new JPEG file structure object my $uri = shift; my $image = new Image::MetaData::JPEG($uri->{'filepath'}.$uri->{'filename'}); die 'Error: ' . Image::MetaData::JPEG::Error() unless $image; # Get a list of references to JPEG segments my $hash_ref; foreach ($image->get_segments()) { # Get EXIF data into hash ref $hash_ref = $_->get_Exif_data('IMAGE_DATA', 'TEXTUAL'); last if ($hash_ref) } # Get comment fields data into array, and then into single variable my @comments = $image->get_comments(); my $comment; foreach(@comments) { $comment = $comment.$_; } # Set all metadata to unknown apart from caption and description my %metadata = ( 'uri'=>$uri, 'description'=>"", 'caption'=>"", 'date'=> { 'year'=>"", 'month'=>"", 'day'=>"", 'hour'=>"", 'minute'=>"", 'second'=>"" }, 'camera'=> { 'make'=>"", 'model'=>"", 'settings'=> { 'flash'=>"", 'focallength'=>"", 'aperture'=>"", 'ISO'=>"", 'exposure'=>"" } }, 'resolution'=> { x=>"", y=>"" }, 'copyright'=>"", 'subject'=>"", 'photographer'=>"" ); if (!defined($hash_ref->{'DateTime'}->[0]) || $hash_ref->{'DateTime'}->[0] eq "") { $hash_ref->{'DateTime'} = $hash_ref->{'DateTimeOriginal'}; } if (defined($hash_ref->{'DateTime'}) && $hash_ref->{'DateTime'}->[0]=~/([0-9]{4}):([01][0-9]):([0123][0-9]) ([012][0-9]):([0-5][0-9]):([0-5][0-9])/) { $metadata{'date'}->{'year'} = $1; $metadata{'date'}->{'month'} = $2; $metadata{'date'}->{'day'} = $3; $metadata{'date'}->{'hour'} = $4; $metadata{'date'}->{'minute'} = $5; $metadata{'date'}->{'second'} = $6; } else { (undef, undef, undef, undef, undef, undef, undef, undef, undef, undef,my $ctime,undef, undef) = stat($uri->{'filename'}); ($metadata{'date'}->{'second'},$metadata{'date'}->{'minute'},$metadata{'date'}->{'hour'},$metadata{'date'}->{'day'},$metadata{'date'}->{'month'},$metadata{'date'}->{'year'},undef,undef) = gmtime($ctime); $metadata{'date'}->{'month'}++; $metadata{'date'}->{'year'} += 1900; print "WARNING- $uri->{'filename'} had no EXIF date information so I used the file systems creation date instead ($metadata{'date'}->{'year'}:$metadata{'date'}->{'month'}:$metadata{'date'}->{'day'}: $metadata{'date'}->{'hour'}:$metadata{'date'}->{'minute'}:$metadata{'date'}->{'second'})\n"; # die("Error with files exif date! [$original_filename $hash_ref->{'DateTime'}->[0]]"); } $metadata{'camera'}->{'make'} = $hash_ref->{'Make'}->[0] if ($hash_ref->{'Make'}->[0]); while ($metadata{'camera'}->{'make'}=~s/\0//) {} $metadata{'camera'}->{'model'} = $hash_ref->{'Model'}->[0] if ($hash_ref->{'Model'}->[0]); while ($metadata{'camera'}->{'model'}=~s/\0//) {} if ($hash_ref->{'Flash'}->[0]) { $metadata{'camera'}->{'settings'}->{'flash'} = "no"; $metadata{'camera'}->{'settings'}->{'flash='} = "yes" if (unpack("B32", pack("N", $hash_ref->{'Flash'}->[0])) =~/1$/); } $metadata{'camera'}->{'settings'}->{'focallength'}=$hash_ref->{'FocalLength'}->[0] if ($hash_ref->{'FocalLength'}->[0]); $metadata{'camera'}->{'settings'}->{'focallength'}=$hash_ref->{'FocalLength'}->[0]/$hash_ref->{'FocalLength'}->[1]."mm" if ($hash_ref->{'FocalLength'}->[0] and $hash_ref->{'FocalLength'}->[1]); $metadata{'camera'}->{'settings'}->{'focallength'}=~s/(\d+)\.(\d{2})(\d*)/$1\.$2/; $metadata{'camera'}->{'settings'}->{'aperture'} = "f/".2**($hash_ref->{'ApertureValue'}->[0] / (2 * $hash_ref->{'ApertureValue'}->[1])) if ($hash_ref->{'ApertureValue'}->[0] and $hash_ref->{'ApertureValue'}->[1]); $metadata{'camera'}->{'settings'}->{'aperture'}=~s/(\d+)\.(\d{2})(\d*)/$1\.$2/; $metadata{'camera'}->{'settings'}->{'ISO'} = $hash_ref->{'ISOSpeedRatings'}->[0] if ($hash_ref->{'ISOSpeedRatings'}->[0]); $metadata{'camera'}->{'settings'}->{'exposure'} = $hash_ref->{'ExposureTime'}->[0]."/".$hash_ref->{'ExposureTime'}->[1]."s" if ($hash_ref->{'ExposureTime'}->[0] and $hash_ref->{'ExposureTime'}->[1]); $metadata{'resolution'}->{'x'} = $hash_ref->{'PixelXDimension'}->[0] if ($hash_ref->{'PixelXDimension'}->[0]); $metadata{'resolution'}->{'y'} = $hash_ref->{'PixelYDimension'}->[0] if ($hash_ref->{'PixelYDimension'}->[0]); return \%metadata; }