Forum Moderators: coopster

Message Too Old, No Replies

creating a thumbnail based on percentage scaled

thumbnails

         

newb2seo

6:08 pm on Jul 24, 2009 (gmt 0)

10+ Year Member



Hi.

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

sasori

9:06 pm on Jul 24, 2009 (gmt 0)

10+ Year Member



just a quick suggestion to swap the height and width; you really want the resize to occur based on the height. Looks like you can do the swap inside of getImageResizeDimensions and at $scaled.

newb2seo

2:31 am on Jul 26, 2009 (gmt 0)

10+ Year Member



hi and thanks for the suggestion. I'll try that.

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.

lexipixel

6:14 am on Jul 27, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month


Here's some PERL code I wrote a few years ago. It's a fuction that scales images... maybe you can scrape a few ideas from it.

#=============
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);
#
}

newb2seo

12:57 pm on Jul 27, 2009 (gmt 0)

10+ Year Member



hey Thanks!
I love perl code.
Great comments too. I think this will allow me to make this in php.
I'll post the code back here once I do that.
Thanks!

lexipixel

9:35 pm on Jul 28, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



No problem, glad to help. I have years worth of old code hanging around -- someone should use it.