Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have an image gallery, more or less, and some users upload HUGE photos exceeding 1000 pixels wide. I can deal with the physical size on the hard drive, I cannot allow images to span the viewable area on my page else it ruins my template and shifts all my ads.

I know how to determine height and width with Image::Info but I need to know how I can HTML resize (yes, just HTML. I know it'll load the whole image anyway but it'll fit better) if the image is greater than $max_height and $max_width.

The thing I can't figure out is how to keep the dimensions of the image PROPORTIONAL to it's original size so it doesn't look distorted when the HTML sizes it down.

For example, if the image is 1123x1243 and my max height/width is 800/600, I need to resize it so BOTH height and width are less than or equal to my max keeping the same ratio. I'd do an example here but I'm not sure how it's calculated.

  • Comment on HTML image resizing with Image::Info based on max height/width

Replies are listed 'Best First'.
Re: HTML image resizing with Image::Info based on max height/width
by almut (Canon) on Jun 12, 2007 at 01:30 UTC

    You might be interested in Image::Math::Constrain

    Update:  Example:

    #!/usr/bin/perl use Image::Math::Constrain; my $math = Image::Math::Constrain->new(800, 600); my ($width, $height, $scale) = $math->constrain(1123, 1243); printf "new width = %.4f\n", $width; printf "new height = %.4f\n\n", $height; printf "old aspect ratio = %.4f\n", 1123 / 1243; printf "new aspect ratio = %.4f\n", $width / $height; # same as old
Re: HTML image resizing with Image::Info based on max height/width
by GrandFather (Saint) on Jun 12, 2007 at 02:58 UTC

    The following snippet is taken from Generate thumbnail and small overstruck images for web publication. $thumbX, $thumbY are the desired dimensions. The $portrait stuff allows the rectangle to be rotated to generate a landscape or portrait image depending on the aspect ratio of the original image. You probably want to omit that for your purposes.

    my ($height, $width) = $image->Get ('height', 'width'); my $portrait = $height > $width; my ($x, $y) = ! $portrait ? ($thumbX, $thumbY) : ($thumbY, $thumbX); my $scaleX = $x / $width; my $scaleY = $y / $height; my $scale = $scaleX < $scaleY ? $scaleX : $scaleY; $thumb->Resize (width=>$width * $scale+0.5, height=>$height * $scale ++0.5);

    DWIM is Perl's answer to Gödel
Re: HTML image resizing with Image::Info based on max height/width
by aquarium (Curate) on Jun 12, 2007 at 03:19 UTC
    Some of my thoughts:
    - you may also want to upsize originals to the 800x600 ideal, not accepting images below a certain threshold, and...
    - fill in (with background color?) the parts required to give you the nominal 800x600 size, and aspect ratio. I think imagemagick composite image technique should work here.
    This will give you more control with layout. For a bit more spice, you could try using the imagemagick studio web service from within your script.
    btw, 4/3 ratio screens are still dominant but not exclusive anymore, as widescreen LCDs are very affordable now. if it's worthwhile, then softcode the ratio.
    the hardest line to type correctly is: stty erase ^H
Re: HTML image resizing with Image::Info based on max height/width
by sulfericacid (Deacon) on Jun 12, 2007 at 01:05 UTC
    Can someone see if this would work?
    #!/usr/bin/perl use warnings; use strict; my $height = "1000"; my $width = "1234"; my $maxh = "600"; my $maxw = "800"; if ($height > $maxh || $width > $maxw) { my $hratio = $height / $maxh; my $wratio = $width / $maxw; my $newh = $height / $hratio; my $neww = $width / $hratio; print "Height is now $newh, Width is now $neww"; }


    "Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"

    sulfericacid

      It's not quite as trivial as it might seem at first (most of us who have tried probably got it wrong the first time :)

      Just as an example, take a width of 1000 and a height of 100. Your algorithm would compute 6000x600 (W x H), although the expected result would be 800x80 (i.e. max width doesn't exceed 800 and aspect ratio stays at 10:1)

Re: HTML image resizing with Image::Info based on max height/width
by varian (Chaplain) on Jun 12, 2007 at 07:12 UTC
    I know how to determine height and width with Image::Info but I need to know how I can HTML resize (yes, just HTML. I know it'll load the whole image anyway but it'll fit better)
    Well, if you plan to send the entire high-resolution picture to the web browser anyway then there is no need to resize at all. :-)

    Just send either the maximum allowed height or the maximum width along with the image to the browser and the browser will calculate the other length such that the image is shown/resized proportionally.

    Note: Even though the above will work, you can save the network and the web client a lot of bandwidth and cpu cycles if you properly resize the picture server side and just transport a downsized picture over the network.

      There is an easy way to do it in HTML... If you want a fixed width or a fixed height: - Fixed height (the width will be adjusted automatically keeping the ratio of the picture) <img src="07cat.jpg" height="96" /> - Fixed width (the height will be adjusted automatically keeping the ratio of the picture) <img src="07cat.jpg" width="96" /> I hope this will help too...
        A bit of old school HTML. <img src="index.jpg" width="800" height="*"> The asterisk keeps it proportional, no matter the original size. Asterisks also work with table dimensions.
        It should also be noted that the following is very useful, dynamic resizing based on the window and element sizes:
        <img src="index.jpg" width="90%" height="*">