Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

determining size of image

by Anonymous Monk
on Feb 22, 2007 at 17:00 UTC ( [id://601588]=perlquestion: print w/replies, xml ) Need Help??

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

I know how to get the size of an image via Image::Info, but I want to add a little snippet that does a little more.

I have a picture posting site where users can upload images to and some are too large and ends up making horizontal scrollbards on the browser (not acceptable!). I can now detect which images are larger than 800x600, which is what I want to test against, but how can I dynamically resize the HTML renderation of the image?

I'm NOT trying to resize the image itself, just the HTML code of the image. Ie. <code> I'm just using HTML to make it appear smaller on screen so the user can click the image to make it appear full in a new window.

So let's say the image was 1100x900, I want to use the HTML image size to show the image at the first size in which both dimensions are smaller than my required size (800x600). In short, I want the resize to be proportioned to the size of the image itself. I COULD just resize everything that's too large to 800x600 but that'd cause a lot of crappy images, I want to keep the same proportions.

Can anyone help me with this?

Replies are listed 'Best First'.
Re: determining size of image
by ikegami (Patriarch) on Feb 22, 2007 at 17:06 UTC
    my ($width, $height ) = ...; my ($max_width, $max_height ) = (800, 600); my ($width_factor, $height_factor) = ($max_width/$width, $max_height/$ +height); if ($width_factor < 1 || $height_factor < 1) { my $factor = ($width_factor < $height_factor ? $width_factor : $height_factor ); $width = int($width * $factor + 0.5); $height = int($height * $factor + 0.5); }

    The above was extracted from a script I use to resize images.

Re: determining size of image
by zentara (Cardinal) on Feb 22, 2007 at 17:22 UTC
    Just as an aside, the ImageMagick ping method is the most efficient way to get size.
    #!/usr/bin/perl -w use Image::Magick; my $x = $ARGV[0]; my $image = Image::Magick->new; #$image->Read($x); #my ($w,$h)= $image->Get('columns','height'); #print $x,' is ',$w.'x'.$h,"\n"; #this is very inneficient memory wise, use Ping ($width, $height, $size, $format) = $image->Ping($x); print $width,"\n", $height,"\n" ,$size,"\n", $format,"\n";
    and here is a script I had in my library to do an actual distortion free resize.
    #!/usr/bin/perl # I need to adjust an image to a minimum size, but without distoring i +t, # so I basically want to add space evenly around the existing image. # Using Resize is out since that will distort the image, so I gave # "Composite" a shot, use Image::Magick; use strict; use warnings; my $im = Image::Magick->new(); my $bg = Image::Magick->new(size => "200x400"); my $rc = $bg->Read("xc:white"); $rc = $im->Read($ARGV[0]); die $rc if $rc; $rc = $im->Resize("200x400"); warn $rc if $rc; $rc = $bg->Composite(gravity => "Center", image => $im); warn $rc if $rc; $rc = $bg->Write("$ARGV[0]-resized.png"); die $rc if $rc;

    I'm not really a human, but I play one on earth. Cogito ergo sum a bum
Re: determining size of image
by dorward (Curate) on Feb 22, 2007 at 17:19 UTC

    I'm NOT trying to resize the image itself, just the HTML code of the image.

    I suggest not doing that. Browsers suck at scaling images, and transfering all those bytes across a network just to have detail thrown away is a waste of bandwidth.

Re: determining size of image
by kwaping (Priest) on Feb 22, 2007 at 17:42 UTC
    Of course, there's always Image::Size. :)

    ---
    It's all fine and dandy until someone has to look at the code.
Re: determining size of image
by mreece (Friar) on Feb 22, 2007 at 18:53 UTC
    i typically use something like the following (using Image::Size)
    use Image::Size; my ($w, $h) = imgsize("blort.jpg"); if ($w > 800) { my $f = $w / 800; $w = int($w/$f); $h = int($h/$f) }; if ($h > 600) { my $f = $h / 600; $w = int($w/$f); $h = int($h/$f) };

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://601588]
Approved by ikegami
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2025-07-10 14:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.