Forum Moderators: coopster
I'm generating thumbnails on the fly with a thumbnail/gallery script.
This thing allows me to set a maximum width and then it just does all the images set to not go over that width.
But what would be better I think is if it figured out the percentage it needed and then adjusted both the height and the width accordingly ... so if there was a very tall image it would be in proportion to the others that may be wide.... instead of them all being the same width basically.
I've almost got it figured out but I'm stuck.
Here's the code that figures the maximum width:
//--------------------------------------------------------
// calculate thumbnail size
$new_width = $thumbWidth;
$new_height = floor( $height * ( $thumbWidth / $width ) );
//--------------------------------------------------------
And I've used this function before to figure up a percentage...
But I don't know how to stick these together so the height and width are in proportion from one image to the next... instead of all being the same width... hope this makes sense.
Here's the percentage function I have.
//--------------------------------------------------------
function resize_image($origUrl,$limit) {
if (!$limit) { $limit = "200"; }
#$imageURI = "/path/to/image.jpg";
$imageURI = $origUrl;
list ($width,$height) = getimagesize($imageURI);
list ($newWidth,$newHeight) = getImageResizeDimensions($width,$height,$limit,$limit); //constrain to #echo "newWidth: $newWidth\n";
#echo "newHeight: $newHeight\n";
$scaled = percent($newWidth,$width);
# echo ("<a href='$url' target='imagezoom'><img src=$url width=$newWidth height=$newHeight border=1></a> ($scaled% of <a href='$url' target='imagezoom'>actual size</a>)");
return array($origUrl,$newWidth,$newHeight,$scaled);
} // end resize_image
function percent($num_amount, $num_total) {
$count1 = $num_amount / $num_total;
$count2 = $count1 * 100;
$count = number_format($count2, 0);
return $count;
} // end percent function
//--------------------------------------------------------
function getImageResizeDimensions($width,$height,$constraintWidth = 0,$constraintHeight = 0) {
$constraintWidth = ($constraintWidth === 0) ? $width : $constraintWidth;
$constraintHeight = ($constraintHeight === 0) ? $height : $constraintHeight;
if (($width > $constraintWidth) ¦¦ ($height > $constraintHeight)) {
while (($constraintWidth < $width) ¦¦ ($constraintHeight < $height)) {
if ($constraintWidth < $width) {
$height = floor((($constraintWidth * $height) / $width));
$width = $constraintWidth;
}
if ($constraintHeight < $height) {
$width = floor((($constraintHeight * $width) / $height));
$height = $constraintHeight;
}
}
}
return array ($width,$height);
}
//--------------------------------------------------------
Any help is appreciated.
Thanks.
-j
It seems like what I need to do is do a single newheight newwidth and then find out what the percentage changed was... and then do a routine that adjusts all the rest of them similarly?
Or if I just knew what percentage and could specify that as the limiter instead of the maximum h/w that also makes more sense.
#=============
sub scaleimg {
#=============
#
# calling method:
#
# ($w,$h) = scaleimg($img_w,$img_h,$tn_w,$tn_h);
#
# where $w and $h are the returned values for
# the scaled image's width and height and;
#
# $img_w and $img_h are the original image
# dimensions (width and height), and;
#
# $tn_w and $tn_h are the maximum dimensions,
# (width and height), for the thumbnail image.
#
my $img_w = $_[0];
my $img_h = $_[1];
my $tn_w = $_[2];
my $tn_h = $_[3];
#
# the basic idea is to find which dimension,
# (width or height), is better to use to
# calculate scaling factor for reduction.
# This insures the largest possible image
# displayed, regardless of portrait or
# landscape, (or square), orientation of
# desired maximum sized thumbnail images.
#
# initialize variables
#
my $scale_factor_w = 0;
my $scale_factor_h = 0;
my $scale_factor = 0;
my $img_tn_w = 0;
my $img_tn_h = 0;
#
# first see if we even need to scale the image,
# (if either original image dimension is larger
# than thumbnail dimensions, we do).
#
if (($img_w gt $tn_w) ¦¦ ($img_h gt $tn_h)) {
#
# next figure out which dimension produces
# the largest "scaling factor", (the number
# we will divide both the width and height
# of the original image by to scale).
#
$scale_factor_w = ($img_w / $tn_w);
$scale_factor_h = ($img_h / $tn_h);
#
# it's a +66% shot that the one dimension will
# be larger or that they will both be the same,
# so we default to "width" and switch to "height"
# (scaling factor) if that's a larger number.
#
$scale_factor = $scale_factor_w;
if ($scale_factor_h gt $scale_factor_w) {
$scale_factor = $scale_factor_h;
}
#
# Divide both dimensions by the scaling
# factor to pass back to the calling code.
#
$img_tn_w = int($img_w / $scale_factor);
$img_tn_h = int($img_h / $scale_factor);
#
#
} else {
#
# if we're here, image doesn't need scaling
#
$img_tn_w = $img_w;
$img_tn_h = $img_h;
#
#
}
#
# return width and height of thumbnail
#
return ($img_tn_w, $img_tn_h);
#
}