Image Constraint

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
fractalvibes
Forum Contributor
Posts: 335
Joined: Thu Sep 26, 2002 6:14 pm
Location: Waco, Texas

Image Constraint

Post by fractalvibes »

Given that users will upload images of roughly the same size, can anyone point me in the direction of a script or snippet that:
a.) Given that I have an ideal size, say 200W X 300H
b.) Given that the images may be a fair bit larger/smaller/wider/taller
c.) I want to determine the best height and width for the image tag
that is proportional, as close to 200 X 300.

thanks,

Phil J.
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

I don't have a script but take a look at imagesx() and imagesy() - or getimagesize()

Resizing an image in the browser by setting height and width in html isn't ideal - although it might be good enough for your needs. You either risk getting a blurry mess if you increase the size, or you waste time sending a larger image than you need to the browser if you decrease size.
pootergeist
Forum Contributor
Posts: 273
Joined: Thu Feb 27, 2003 7:22 am
Location: UK

Post by pootergeist »

If you have GD2+ try

http://www.phpclasses.org/browse.html/package/1007.html

if you create a base image of 200px by 300px, then every transformed image will be resized to fit within those dimensions - eg a 600x450 image would become 200x150 thus fitting the 200 width definer with height determined proportionally.
can also add a transformation if you want - eg bevel, drop-shadow, motion-blur, frame, greyscale etc.

basic online demo page unfortunately unaccessible due to server snags.
User avatar
d1223m
Forum Commoner
Posts: 80
Joined: Mon Mar 31, 2003 5:15 am
Location: UK, West Sussex

simple math

Post by d1223m »

the maths is quite simple. just look at the width first, scale it to the desired width ( 300 ) remembering the magic scaling number. Then scale the height by the same number if this is greater than the desired height ( 200 ) then you want to determine the scaling number from the height . This way you will get full scaling in at least one direction, which is what you want ;)
Post Reply