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

I've read some SOPWs on this question and someone said as long as you pass the max HEIGHT or max WIDTH to the browser, the other size will automatically be resized.

I'm trying to make sure my images are less than max_h and max_w, if they are I want them both resized so they're the same aspect ratio but under my requirements.

I tried the below and I'm guessing my problem lies in it only checks one side, not both. So if BOTH sides are too big, only one is resized leaving the other one POSSIBLY too big.

Can someone help me?

############################## # Image Resize ############################## use Image::Info qw(image_info dim); my $img_info = image_info("pictures/$pic_id.jpg"); my ($height, $width) = dim($img_info); my ($max_width, $max_height ) = (600,800); if ($height >= $max_height) { print qq(<center><a href="http://www.site.com/viewer2.cgi?id=$pic_i +d" target="new"><img src="$img_src" height="800" border="0"></href></ +center>); } elsif ($width >= $max_width) { print qq(<center><a href="http://www.site.com/viewer2.cgi?id=$pic_i +d" target="new"><img src="$img_src" width="600" border="0"></a></cent +er>); } else { print qq(<center><a href="http://www.site.com/viewer2.cgi?id=$pic_i +d" target="new"><img src="$img_src" border="0"></a></center>); }

Replies are listed 'Best First'.
Re: Can't automaticaly HTML-resize my images
by grep (Monsignor) on Jul 20, 2007 at 02:19 UTC
    If I'm reading your question (and HTML) right, you're creating (as esteemed monk merlyn points out) a dumbnail. Please don't let the browser resize your image. It will generally not look as good as an actual image resize and you'll be wasting bandwidth downloading a larger image than you want.

    A good strategy is to pre-size your images to 3 or 4 standard sizes and then send the image you want. It's quite easy to to use something like Image::Magick to resize your images (in ratio) as you add images.

      The problem with that is finding a web host that has Image::Magic. Image::Magick is almost completely out of the minds of any web host you'll come by, and most will REFUSE to install it if you ask them to.

      I have no ability to resize and store more images, besides I have far more bandwidth than I do storage space so I can't make copies of all my images.

        I have no ability to resize and store more images, besides I have far more bandwidth than I do storage space so I can't make copies of all my images.

        So why not resize them before you upload them? That would save you storage space as well...  On your own machine, it should be easy to install ImageMagick or some other tool that is able to batch-resize images.

Re: Can't automaticaly HTML-resize my images
by GrandFather (Saint) on Jul 20, 2007 at 01:56 UTC
      I've read those already and the reply on the 2nd one you posted doesn't work properly. I would rather not use a module for something like this either.

      It's just the ratios that are causing me problems I think