Dynamic Image Scaling (HTML Strict Compliant)

Kelicula 0 Tallied Votes 164 Views Share

Situation:
Ever wanted to get the size of an image?
Possibly to determine new height and width dimensions that DO NOT distort the proportion.
Well I have, and I overcame the math like so.

Solution:
First you must decide on an image width. The subroutine returns the proportional height equivalent. It uses the Image::Size module, which is available from CPAN and may be placed in a subfolder on your local server. And linked to from your script via the use lib "./yourfolder"; use Image::Size; command.

It takes to arguments, the fixed width, and the path to the image. (relative OK)

Usage:

use strict;
use CGI;
use Image::Size; #(if installed to lib) 

my $q = CGI->new();

my $height = &getImgHeight('../img/theImage.jpg', 200);

print $q->img({ -width => '200', height => $height});
sub getImgHeight {
use Image::Size;

my ($w, $h) = imgsize(shift);
my $temp = substr((shift / $w), 0, 5);
(my $height = ($h * $temp)) =~ s/\.+\d*$//;

return $height;
}
Kelicula 0 Newbie Poster

PLease delete this comment :)

KevinADC 192 Practically a Posting Shark

Image-Size has dependencies, so adding it via the lib pragma may not work unless the depenedencies are also added.

It is a good way though to get image dimensions.

Kelicula 0 Newbie Poster

The comment I would like deleted is the one that contains the text "Please delete this comment:)". NOT the one from KevinADC.

Moderator?? Help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.