[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Resizing images (Gimp???)




On Thu, 27 Jun 2002, Nate Reindl wrote:

> Quoting Steven Pritchard <steve@silug.org>:
> 
> > Kara's thing may be your best bet.
> 
> As far as I can tell, Kara has no thing.  Seriously.  Look at the attachment 
> and see if you can find any Perl code in there because honestly, I sure as hell 
> can't.  It only has the mailing list's signature in it.

Your mail reader is b0rked.

#!/usr/bin/perl -w

#########################################################################################
#
        #
#       Program: thumbs.pl
        #
#               Generate an html page with thumbnails for images.

<SNIP>

see!

-- 
Kara Pritchard                          Phone: 618-398-7360
Director of Exam Development            http://www.lpi.org/
--

#!/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"} = <STDIN>;
	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"} = <STDIN>;
	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"} = <STDIN>;
	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"} = <STDIN>;
	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"} = <STDIN>;
	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"} = <STDIN>;
	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"} = <STDIN>;
	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 "<!DOCTYPE HTML PUBLIC\"-//W3C//DTD HTML 4.0 Transitional//EN\">\n";
	print HTML_FILE "<html>\n\n";
	print HTML_FILE "<head>\n";
	print HTML_FILE "\t<title>$user{page_heading}</title>\n";
	print HTML_FILE "\t<style type=\"text/css\">\n";
	print HTML_FILE "\th1, p {font-family: $config{font}}\n";
	print HTML_FILE "\t</style>\n";
	print HTML_FILE "</head>\n\n";
	print HTML_FILE "<body bgcolor=\"$config{page_bg_color}\" marginwidth=15 marginheight=15 ".
			"link=\"$config{color_link}\" alink=\"$config{color_alink}\" ".
			"vlink=\"$config{color_vlink}\" text=\"$config{color_text}\">\n\n";
	print HTML_FILE "<center>\n\n";
	
	print HTML_FILE "<h1>$user{page_heading}</h1>\n";
	print HTML_FILE "$user{page_desc}<br><br>\n";
	print HTML_FILE "<small>Clicking thumbnail will view top resolution listed.</small><br><br>\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<!--THUMBS_DATA START-->\n";
	print HTML_FILE "<!--DATE $date-->\n";
	print HTML_FILE "<!--IMG_COUNT $num_images-->\n";
	print HTML_FILE "<!--DESC $user{page_desc}-->\n";
	print HTML_FILE "<!--THUMBS_DATA END-->\n\n";


	#
	# generate tables for images
	#
	for ($i = 0; $total_rows > $i; $i++) {
		print HTML_FILE "\t<table border=0 cellpadding=10 cellspacing=0 align=\"center\">\n";
		print HTML_FILE "\t\t<tr>\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</tr>\n";
		print HTML_FILE "\t</table>\n\n";
	}

	#
	# write footer information
	#
	print HTML_FILE "</center>\n\n";
	print HTML_FILE "</body>\n\n";
	print HTML_FILE "</html>\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<td width=\"$td_width%\" align=\"center\"><p>\n";
	
	# print thumb, link to smallest image size
	#
	print HTML_FILE "\t\tImage <strong>$image->{image_num}</strong><br>\n";
	print HTML_FILE "\t\t<a href=\"images/$image->{base_name}_$convert_sizes[0].$image->{file_suffix}\">";
	print HTML_FILE "<img src=\"thumbs/$image->{thumb_file}\" width=\"$thumb_width\" ".
			"height=\"$thumb_height\" alt=\"image $image->{image_num}\">";
	print HTML_FILE "</a><br>\n";
	
	print HTML_FILE "\t\t<small>\n";
	
	# go through and print link to each size
	#
	foreach $size (@convert_sizes) {
		print HTML_FILE "\t\t<a href=\"images/$image->{base_name}_$size.$image->{file_suffix}\">".
				"$size</a> ";
		@filesize	= split(" ", 
					`du -k $set_dir/images/$image->{base_name}_$size.$image->{file_suffix}`);
		$filesize_kb	= $filesize[0];
		print HTML_FILE "($filesize_kb kb)<br>\n";
	}

	print HTML_FILE "\t\t</small>\n";

	print HTML_FILE "\t\t</td>\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();

-
To unsubscribe, send email to majordomo@silug.org with
"unsubscribe silug-discuss" in the body.