#!/usr/bin/perl -w ######################################################################################### # # # Program: thumbs.pl # # Generate an html page with thumbnails for images. # # License: # # GPL! # # Credit: # # Inspired by Garrett LeSage's photo pages. # # Html layout made to look like his output (blatent copy). # # # ######################################################################################### ################# # configuration # ################# # vars prompted in interactive mode - edit those for sure if you don't use --interactive # $default{"tmp_dir"} = "/home/gerard/tmp"; # dir where your pictures are $default{"base_dir"} = "/home/gerard/photos";# dir to put finished product in $default{"thumb_size"} = "100x75"; # size for the thumbnails $default{"columns"} = 4; # number of columns of images $default{"page_heading"} = "My Photos"; $default{"page_desc"} = "Some pictures I took with a camera."; $default{"convert_sizes"} = "640x480 800x600 1024x768 1600x1200"; # first size is thumb size ############# # general config vars that are not prompted in --interactive mode # $config{"page_bg_color"} = "#bec6c4"; # color for the bg of the page $config{"color_link"} = "#975112"; # color for link in body tag $config{"color_alink"} = "#781413"; # color for alink in body tag $config{"color_vlink"} = "#1230a2"; # color for vlink in body tag $config{"color_text"} = "#000000"; # text for color $config{"font"} = "helvetica,arial,sans-serif"; # font for page $config{"convert_xoptions"} = "-quality 75"; # extra options for conver program $config{"html_filename"} = "index.html"; # name for html file created $config{"min_columns"} = 1; # avoid an error, can ignore $config{"max_columns"} = 10; # avoid an error, can ignore ########################################################################################## ########## Probably don't need to edit below this line for normal use ########## ########## but if you're in a hackin' mood go for it, it's GPL! ########## ########################################################################################## # do a couple general things # $version = "0.1.2"; # set version number $| = 1; # autoflush output (for progress meter) ################### # user prefs code # ################### # # sets all the user's prefs # sub thumbs_get_prefs { print "###################\n"; print "## configuration ##\n"; print "###################\n\n"; print "* hit enter for [default] *\n\n"; # tmp dir where image are located # print "Directory with images to be 'thumbed' [$default{tmp_dir}]\n"; print "\t\t\t\t: "; $user{"tmp_dir"} = ; chomp($user{"tmp_dir"}); if (!$user{"tmp_dir"}) { $user{"tmp_dir"} = $default{"tmp_dir"}; } # dir base to put finished product in # print "Directory to put converted images [$default{base_dir}]\n"; print "\t\t\t\t: "; $user{"base_dir"} = ; chomp($user{"base_dir"}); if (!$user{"base_dir"}) { $user{"base_dir"} = $default{"base_dir"}; } # pref thumb size # print "Thumb image size (wxh) [$default{thumb_size}]\n"; print "\t\t\t\t: "; $user{"thumb_size"} = ; chomp($user{"thumb_size"}); if (!$user{"thumb_size"}) { $user{"thumb_size"} = $default{"thumb_size"}; } # number of columns pref # print "Number of image columns [$default{columns}]\n"; print "\t\t\t\t: "; $user{"columns"} = ; chomp($user{"columns"}); if (!$user{"columns"}) { $user{"columns"} = $default{"columns"}; } # page heading # print "Page heading [$default{page_heading}]\n"; print "\t\t\t\t: "; $user{"page_heading"} = ; chomp($user{"page_heading"}); if (!$user{"page_heading"}) { $user{"page_heading"} = $default{"page_heading"}; } # page image description # print "Images description [$default{page_desc}]\n"; print "\t\t\t\t: "; $user{"page_desc"} = ; chomp($user{"page_desc"}); if (!$user{"page_desc"}) { $user{"page_desc"} = $default{"page_desc"}; } # convert size prefs # print "Conversion sizes [$default{convert_sizes}]\n"; print "\t\t\t\t: "; $user{"convert_sizes"} = ; chomp($user{"convert_sizes"}); if (!$user{"convert_sizes"}) { $user{"convert_sizes"} = $default{"convert_sizes"}; } } # # verify the user's prefs # sub thumbs_verify_prefs { my(@error, $err); # verify valid dirs # if (! -d $user{"tmp_dir"}) { push(@error, "invalid tmp dir"); } if (! -d $user{"base_dir"}) { push(@error, "invalid base dir"); } # check thumb size # if (!$user{"thumb_size"}) { push(@error, "need a thumb size"); } # check for decent # of columns # if (($user{"columns"} < $config{"min_columns"}) || ($user{"columns"} > $config{"max_columns"})) { push(@error, "bad number of columns"); } # check the page heading and description # if (!$user{"page_heading"} || !$user{"page_desc"}) { push(@error, "need heading and description"); } # check the conversion sizes (format: wxh wxh wxh) # if ($user{"convert_sizes"}) { @convert_sizes = split(" ", $user{"convert_sizes"}); foreach $size (@convert_sizes) { if ($size !~ /^(\d{1,4}x\d{1,4})$/) { push(@error, "conversion sizes in bad format"); last; } } } else { push(@error, "must have conversion sizes"); } if (@error) { print "\n"; print "Errors encountered in your preferences:\n"; foreach $err (@error) { print "* $err\n"; } print "\n\n"; exit; } } ############## # conversion # ############## # # creates the dir for the new set # returns: the dir created on success # sub thumbs_dir_prepare { my($date, $dir, $set_count, $set_dir); $set_count = 1; # check for current dirs # $date = `date +"%Y%m%d"`; chomp($date); foreach $dir (<$user{"base_dir"}/*>) { if ((-d $dir) && ($dir =~ /$date/)) { ++$set_count; } } while (-d "$user{base_dir}/$date-set$set_count") { ++$set_count; } $set_dir = "$user{base_dir}/$date-set$set_count"; if (mkdir($set_dir, 0755)) { chmod(0755, $set_dir); mkdir("$set_dir/thumbs", 0755); chmod(0755, "$set_dir/thumbs"); mkdir("$set_dir/images", 0755); chmod(0755, "$set_dir/images"); mkdir("$set_dir/originals", 0755); chmod(0755, "$set_dir/originals"); return($set_dir, "$date-set$set_count"); } else { die("Error making new set dir: $!"); } } # # convert the images and place them in the proper location # also populate the $images hash with info we'll need throughout # sub thumbs_convert_images { my($image_count, $image_num, $image_file, $image_file_newname); $image_count = 0; print "\n######################\n"; print "## image conversion ##\n"; print "######################\n\n"; print "Converting images"; foreach $image_file (<$user{"tmp_dir"}/*>) { # check if binary... probably an image =) if (-B $image_file) { $image_num = $image_count + 1; $image_file_newname = $set_name."_".$image_num; $images[$image_count]{"base_name"} = $image_file_newname; $images[$image_count]{"filename"} = $image_file; $images[$image_count]{"image_num"} = $image_num; $_ = $image_file; /.{1}\.([a-zA-Z]{3,4})/; $images[$image_count]{"file_suffix"} = $1; $images[$image_count]{"thumb_file"} = $image_file_newname."_thumb.$1"; thumbs_convert_image_sizes($images[$image_count]); ++$image_count; } } print "Done\n"; } # # convert the image to all the different sizes # sub thumbs_convert_image_sizes { my($image) = @_; my($size, $image_name); # convert to each size foreach $size (@convert_sizes) { $image_name = $image->{"base_name"} . "_" . $size . ".$image->{file_suffix}"; system("convert $config{convert_xoptions} -geometry $size $image->{filename} ". "$set_dir/images/$image_name\n"); } # create a thumbnail system("convert $config{convert_xoptions} -sample $user{thumb_size} ". "$image->{filename} $set_dir/thumbs/$image->{thumb_file}"); # progress meter =) print "."; } ###################### # html file creation # ###################### # # create the html file with thumbnail index # sub thumbs_create_html_file { my($date, $count, $num_images, $total_rows, $extra); my($image_base, $num_left, $td_width, $td_width_extra); my($i, $j); # grab date, add to heading $date = `date +"%B %e, %Y"`; chomp($date); $user{"page_heading"} .= ": $date"; # status printing # print "\n########################\n"; print "## html file creation ##\n"; print "########################\n\n"; print "Creating html file"; open(HTML_FILE, ">$set_dir/$config{html_filename}") || die("Unable to create $set_dir/$config{html_filename}: $!"); # # write header information # print HTML_FILE "\n"; print HTML_FILE "\n\n"; print HTML_FILE "\n"; print HTML_FILE "\t$user{page_heading}\n"; print HTML_FILE "\t\n"; print HTML_FILE "\n\n"; print HTML_FILE "\n\n"; print HTML_FILE "
\n\n"; print HTML_FILE "

$user{page_heading}

\n"; print HTML_FILE "$user{page_desc}

\n"; print HTML_FILE "Clicking thumbnail will view top resolution listed.

\n"; # set some needed vars # $count = 0; $num_images = @images; $td_width = int(100 / $user{"columns"}); $total_rows = int($num_images / $user{"columns"}); $extra = $num_images % $user{"columns"}; if ($extra > 0) { ++$total_rows; } # print some information for script to use to create main image page (code not done) # print HTML_FILE "\n\n"; print HTML_FILE "\n"; print HTML_FILE "\n"; print HTML_FILE "\n"; print HTML_FILE "\n\n"; # # generate tables for images # for ($i = 0; $total_rows > $i; $i++) { print HTML_FILE "\t\n"; print HTML_FILE "\t\t\n"; $num_left = $num_images - $count; if ($num_left == $extra) { # incomplete 'row' for ($j = 0; $extra > $j; $j++) { print "."; $td_width_extra = int(100 / $extra); thumbs_create_html_print_column($images[$count], $td_width_extra); ++$count; } } else { # full 'row' for ($j = 0; $user{"columns"} > $j; $j++) { print "."; thumbs_create_html_print_column($images[$count], $td_width); ++$count; } } print HTML_FILE "\t\t\n"; print HTML_FILE "\t
\n\n"; } # # write footer information # print HTML_FILE "
\n\n"; print HTML_FILE "\n\n"; print HTML_FILE "\n"; close(HTML_FILE); print "Done\n\n"; } # # prints out individual columns for html file creation # sub thumbs_create_html_print_column { my($image, $td_width) = @_; my($thumb_width, $thumb_height, $size, @filesize, $filesize_kb); # get thumb size for html width and height tags to be compliant # $_ = $user{"thumb_size"}; /(\d{1,4})x(\d{1,4})/; ($thumb_width, $thumb_height) = ($1, $2); print HTML_FILE "\t\t

\n"; # print thumb, link to smallest image size # print HTML_FILE "\t\tImage $image->{image_num}
\n"; print HTML_FILE "\t\t{base_name}_$convert_sizes[0].$image->{file_suffix}\">"; print HTML_FILE "{thumb_file}\" width=\"$thumb_width\" ". "height=\"$thumb_height\" alt=\"image $image->{image_num}\">"; print HTML_FILE "
\n"; print HTML_FILE "\t\t\n"; # go through and print link to each size # foreach $size (@convert_sizes) { print HTML_FILE "\t\t{base_name}_$size.$image->{file_suffix}\">". "$size "; @filesize = split(" ", `du -k $set_dir/images/$image->{base_name}_$size.$image->{file_suffix}`); $filesize_kb = $filesize[0]; print HTML_FILE "($filesize_kb kb)
\n"; } print HTML_FILE "\t\t
\n"; print HTML_FILE "\t\t\n"; } # # copies the original images to $set_dir/originals # sub thumbs_originals_copy { my($image_file); print "\n###########################\n"; print "## original image backup ##\n"; print "###########################\n\n"; print "Backing up orignals to $set_dir/originals"; foreach $image_file (<$user{"tmp_dir"}/*>) { if (-B $image_file) { system("cp $image_file $set_dir/originals"); print "."; } } print "Done\n"; } ##################### # general functions # ##################### # # make the default prefs, the user prefs # sub thumbs_default_prefs { my($pref_option); foreach $pref_option (keys(%default)) { $user{$pref_option} = $default{$pref_option}; } } # # print out program usage, exit # sub thumbs_usage { print "Usage: thumbs.pl [OPTION]\n\n"; print "\t-i, --interactive\t\tsome config settings prompted\n"; print "\t-d, --default\t\t\tdefault mode, uses defaults within file\n"; print "\t-h, --help\t\t\tdisplay help (this menu) and exit\n"; print "\t-v, --version\t\t\toutput version and exit\n\n"; exit; } # # print out the version number, exit # sub thumbs_version { print "\nthumbs.pl version $version\n\n"; exit; } # # print out little intro ascii # sub thumbs_intro { print "\n"; print "###############################\n"; print "###############################\n"; print "#### ----- thumbs.pl ----- ####\n"; print "###############################\n"; print "###############################\n"; print "\t[version $version]\n\n"; } # # check ARGV for options # sub thumbs_option_check { my($arg_count); $arg_count = @ARGV; if ($arg_count == 1) { $_ = $ARGV[0]; if (/^-{1,2}i/) { $mode_interactive = 1; } elsif (/^-{1,2}d/) { $mode_interactive = 0; } elsif (/^-{1,2}h/) { thumbs_usage(); } elsif (/^-{1,2}v/) { thumbs_version(); } else { thumbs_usage(); } } else { thumbs_usage(); } } ################ # # # main control # # # ################ # check ARGV thumbs_option_check(); # print intro =) thumbs_intro(); # user prefs if ($mode_interactive) { thumbs_get_prefs(); } else { thumbs_default_prefs(); } thumbs_verify_prefs(); # make the new set dir ($set_dir, $set_name) = thumbs_dir_prepare(); # convert images and place them in the new set dir thumbs_convert_images(); # html creation thumbs_create_html_file(); # copy originals to set dir thumbs_originals_copy();