Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Manipulating Images (height and width)

by Anonymous Monk
on Sep 08, 2002 at 14:37 UTC ( #196025=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:


Is there a way in which whenever a person uploads an image file, I can re-adjust its height and width if both these are greater than x pixels?


Replies are listed 'Best First'.
Re: Manipulating Images (height and width)
by LTjake (Prior) on Sep 08, 2002 at 14:49 UTC
    Might I also suggest GD? You can get the image size with
    ($width,$height) = $image->getBounds()
    and resize it with
    $image->copyResized($sourceImage, $dstX, $dstY, $srcX, $srcY, $destW, +$destH, $srcW, $srcH)
    See the docs for more info.
      Might I also suggest GD?

      Sure, unless of course, the image happens to be a .gif image, in which case you'll need to patch the stock GD to add that capability back in.

      Note: This path is NOT recommended due to the restrictive licensing and moral issues associated with using .gif images, and circumventing that license by patching GD to restore the capability that was removed due to it.

      In short, use the ImageMagick Perl API to do the conversions and resizing you need.

      .. may I also suggest a pair of scissors?
Re: Manipulating Images (height and width)
by Aristotle (Chancellor) on Sep 08, 2002 at 14:40 UTC
    Sounds like a job for ImageMagick. There's a Perl binding for it in the form of the Image::Magick module as well.

    Makeshifts last the longest.

Re: Manipulating Images (height and width)
by sauoq (Abbot) on Sep 08, 2002 at 19:22 UTC

    If you only really care how the image is displayed on a web page, another, lighter, solution would be to use Image::Info to get the dimensions and some simple arithmetic to determine the desired height and width attributes for your img tag.

    If you actually want to change the size of the image use one of the other solutions mentioned. Just so you know, Image::Magick is a sledgehammer and GD is somewhat limited in the formats it will deal with.

    "My two cents aren't worth a dime.";
Re: Manipulating Images (height and width)
by OeufMayo (Curate) on Sep 09, 2002 at 06:17 UTC

    You can also use Imager, which is far more easier to install than ImageMagick, has most of the features you will use 90% of the time, and is also easier to use, IMO.

    The scale() method allows you to resize image with a maximum height or width. Or if it is not exactly what you want to do, you can still do maths with the getheight() and getwidth() methods.

    #!/usr/bin/perl -w use strict; use Imager; my $image = Imager->new; # Load the original image $image->open(file => $ARGV[0]) or die $image->errstr; # Get the dimensions of the picture print "Size: ", $image->getwidth, 'x', $image->getheight, "\n"; # Scale the picture to at most 150px in width or height my $resized = $image->scale( width => 150, height => 150, type => 'min +' ); # Save the resized picture to disk $resized->write(file=>"tn_$ARGV[0]") or die $resized->errstr; __END__
Re: Manipulating Images (height and width)
by Jeppe (Monk) on Sep 09, 2002 at 10:55 UTC
    For the sake of the quality, I highly recommend you use GD or ImageMagick for scaling. You CAN do this using the height and width of the image tag in your HTML, but then the images will be at the mercy of the resizer within the end-user browser.

    More importantly, you should make sure you maintain the h/w ratio when you scale an image. Otherwise, people will get flat faces and other unwanted artifacts.

    For moral strength, you should also consider auto-converting to PNG graphics.

      Not only do you jeopardize the image quality, there's another much more important point: filesizes. Why send an 80kb 400x400 image scaled to 40x40 with IMG attributes when a real 40x40 one would be 4kb? Even visitors using cable are no counter argument: someone has to pay for the bandwidth on the server side..

      Makeshifts last the longest.

        Why send an 80kb 400x400 image scaled to 40x40 with IMG attributes when a real 40x40 one would be 4kb?

        One possible reason is if the 400x400 image is expected to be viewed later. Why send 4kb 100 times and then 80kb 95 times when sending 80kb 100 times will do? In your example, this justification would only work if the larger image were requested about 95% of the time (though that would still win with protocol overhead) but, with different images, that ratio could be a lot lower. There is no substitute for traffic analysis.

        Another reason might be because generating thumbnails on the fly is expensive in terms of both CPU and time. You might argue that one should then generate the smaller images ahead of time but that approach could be costly in terms of disk and, perhaps more importantly, maintenance issues. These problems could be compounded by the need for multiple thumbs of different sizes.

        Having many ways to do things is so desirable because which of those ways is best usually depends on the problem at hand.

        "My two cents aren't worth a dime.";
Re: Manipulating Images (height and width)
by Anonymous Monk on Sep 09, 2002 at 12:06 UTC
    Well, I use Image::Magick for that
    my $thumb= new Image::Magick; open(IMAGE, "</path/$photof"); $thumb->Read(file=>\*IMAGE); close(IMAGE); $thumb->Resize(geometry=>'120X90'); $thumb->Write("/path/thumb_$photof");

    Of course, what is missing in there for your usage is simply a:
    my ($height,$width) = $thumb->Get('width','height);

    before the resize, and add there your checks.
    (the last part is untested).


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://196025]
Approved by Aristotle
Front-paged by wil
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (4)
As of 2017-01-19 23:54 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (173 votes). Check out past polls.