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

Please advise what slash I have wrong here? I am trying to resize an image but put a slash in the wrong place?
print "<img src=\"http://myserver/image.gif\" width=500 height=60>\n";

Replies are listed 'Best First'.
Re: Resizing image
by zigdon (Deacon) on Oct 30, 2002 at 13:59 UTC

    Seems ok to me - why do you think it's wrong? you didn't specify what isn't working here.

    On a related note, you shouldn't resize images by IMG tags, as that still sends the full sized image to each client, and forces their CPU to resize it to fit in the space provided. Instead, you should probably make a resized version of your image, perhaps using a module, like ImageMagick or GD.

    -- Dan

      Thank you, Dan.
Re: Resizing image
by davorg (Chancellor) on Oct 30, 2002 at 14:27 UTC

    Make your life easier by using the qq() operator.

    print qq(<img src="http://myserver/image.gif" width="500" height="60"> +\n);

    Also a good idea to get into the habit of quoting all attributes as that's what XHTML requires.

    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

      For that matter, correct XHTML would require a close tag and the mandatory alt attribute:

      <img src="foo.gif" width="10" height="10" alt="Foo" />

      A little picky, but once you get in the habit, very easy to do.

        Absolutely right. I usually just throw my output thru HTMLTidy to catch errors like that.

        --
        <http://www.dave.org.uk>

        "The first rule of Perl club is you do not talk about Perl club."
        -- Chip Salzenberg

Re: Resizing image
by hacker (Priest) on Oct 30, 2002 at 14:39 UTC
    Might I suggest using CGI.pm's method for this?
    # png good, gif bad print img({-src => 'http://myserver/image.png', -width => '500', -height => '60', -alt => 'My Image', -title => 'This is my image', });

    It will produce much more readable code, both in your script and in the browser, and you don't have to worry about escaping characters as rigidly as you do with "raw" print() statements.

    There's also the resize()/convert() options of Image::Magick, which can do things like:

    use strict; use Image::Magick; my $image = Image::Magick->new(); $x = $image->Read(filename =>'image.png'); $x = $image->Resize(geometry =>'640x480'); $x = $image->Write(filename =>'image-th.png', quality =>75); @$image = ();

    Lastly, there's HTMLThumbnail, by Benjamin Franz.

    Resizing the images sent to the user by sending them the entire "full-size" image, and forcing the browser to resize it as a thumbnail is bad for several reasons:

    • You're relying on the browser to "properly" render and quantize the image as a smaller or larger reproduction of the original, which may not look like you want it to.

    • If the user hits a page of 50 thumbnails, all full-size images, scaled down to 100x100 pixels, and only ever looks at some of them, he has to still download all of them into his local cache, full-size, before he can view any of them. Don't burdon your users with unnecessary bandwidth that can be better used to serve content to them.

    • Forcing full-size images down the pipe to the user slows their browsing experience, and the speed with which your page draws in their browser.

    In short, don't, especially when there are better alternatives.