PDA

View Full Version : restrict image to maximum size inputed by me...



sWo0p
December 17th, 2004, 05:13 AM
im getting along with my new system script very nicec now.. im allmost finished but found a little problem wile inputing a image url.. if i input an url of a image that has bin uploaden to a desination regading size.. i like to inplant a function that looks at a image pixel size and if its bigger then 400px width or height it must scale it (without streching) the image so it'll fit....

i dont have GDlyb on this php server so i think its best to use the width="" in a tag.. but how can i do this best?

ironikart
December 17th, 2004, 06:22 AM
You can do this with javascript, although this is a quick and dirty method and probably not cross browser compatible (don't think it will work in NN4).

It requires an id tag to be set for the image element...



<body onload="resize()">
<img src="image.jpg" name="test" id="test">
<script type="text/javascript">
function resize()
{
maxX = 400;
maxY = 400;
height = document.test.height;
width = document.test.width;
scale = Math.min(maxX/width, maxY/height);

if (scale < 1)
{
newWidth = Math.floor(scale*width);
newHeight = Math.floor(scale*height);
document.test.height = newHeight;
document.test.width = newWidth;
}
}

</script>
</body>

sWo0p
December 17th, 2004, 07:18 AM
well i can try it but i dont think this wil work cos of my use on several sections and directors...

i searched a little on the net but most scripts are based on GDlyb.. so i think the best way and quickest is using normal html tags... but then not specify height but only specify width.. i im correct the height wil automaticly resize with the width.....

but thx for you info..