We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,672 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?

Dynamic Image Scaling (HTML Strict Compliant)

0
By Kelicula on Sep 12th, 2008 3:50 am

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;
}

PLease delete this comment :)

Kelicula
Newbie Poster
17 posts since Jul 2007
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 0

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.

KevinADC
Posting Shark
921 posts since Mar 2006
Reputation Points: 246
Solved Threads: 67
Skill Endorsements: 0

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

Moderator?? Help!

Kelicula
Newbie Poster
17 posts since Jul 2007
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 0

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.5160 seconds using 2.64MB