Auto picture resize

liunx

Guest
Hi,<br />
<br />
Using HTML, is it possible to automaticly resize a picture to fit on a page?<br />
<br />
The script needs to be compatiable with frames. It must be HTML so all browsers can see the picture.<br />
<br />
Thanks,<br />
<br />
Neil<!--content-->Sure:<br />
<br />
<img src=http://www.webdeveloper.com/forum/archive/index.php/"123.png" alt="123" style="width:100%;" /><!--content-->Do you mean like this:<br />
<img alt="" src=http://www.webdeveloper.com/forum/archive/index.php/"images/an_image.gif" height="100%" width="100%" /><!--content-->If you specify just the width the height will automatically change to match the width proportionate to the original. Same thing if you specify just the height. So when it resizes its not out of proportion and all weird looking. If you specify both the width and the height as 100% the image will stretch completly and it will throw it out of proportion. Also I would not use alt="" you should really put something in there like the image name or something, because that alt tag is used by audiobraille browsers to catologe the iamges. Also if a url is wrong it is nice to have some text in the images place.<!--content-->Hi<br />
<br />
The picture needs to keep its origional ratios (which are 10:4, so if the width was 200 the height would be 80) and make itself as big as possible, but it must all fit on the screen at once. If you then resize the frame it's in it should resize as above.<br />
<br />
Eg.<br />
<br />
If the height of the frame was 200 and the width 400, the picture would be 400(width)X160(height).<br />
<br />
If you then resized the frame to 500X60, the picture would automaticly resize to 150X60.<br />
<br />
I've tried using this code:<br />
<br />
<img alt="piccy" src=http://www.webdeveloper.com/forum/archive/index.php/"mypic.jpg" style="width:100%" height='40%'><br />
<br />
But the height is always 40% of the frame height and the width 100% of the frame width with this code.<br />
<br />
Thanks for your help,<br />
<br />
Neil<!--content-->Specify one or the other thought not bots, because the frame width can change depending on screen size and resolution etc and they you dont have a perfect square anymore and the picture is no longer in proportion.<!--content-->My frames aren't resizable, and I don't want them to resize. They only change size if you change the size of the browser window, which is why I wan't the script.<br />
<br />
Thanks,<br />
<br />
Neil<!--content-->well like I mentioned before just say width="100%" will do the other size automatically and be proportionate to the original size.<!--content--><img alt="piccy" src=http://www.webdeveloper.com/forum/archive/index.php/"picture.jpg" width="100%"> nearly does what I want, but if the height of the frame is less then 40 of the width then you need to scroll down to see the whole picture. You shouldn't need to scroll, whatever the width or height.<br />
<br />
Thanks,<br />
<br />
Neil<!--content-->Lets see if we're on the right track here.<br />
Please try the following<br />
<br />
<br />
<br />
<HTML> <br />
<HEAD> <br />
<TITLE>Document Title</TITLE> <br />
<script> <br />
<br />
function init(){ <br />
width=document.body.clientWidth <br />
height=document.body.clientHeight <br />
<br />
my_image=new Image() <br />
my_image.src=http://www.webdeveloper.com/forum/archive/index.php/"seapic1.jpg" <br />
<br />
percent_width=my_image.width/width <br />
percent_height=my_image.height/height <br />
init2() <br />
} <br />
<br />
function init2(){ <br />
width2=document.body.clientWidth <br />
height2=document.body.clientHeight <br />
<br />
document.getElementById("mypic").width=width2*percent_width <br />
document.getElementById("mypic").height=height2*percent_height <br />
<br />
} <br />
onresize=init2 <br />
</script> <br />
</HEAD> <br />
<BODY onload="init()"> <br />
<img id="mypic" src=http://www.webdeveloper.com/forum/archive/index.php/"seapic1.jpg"> <br />
<br />
<br />
</BODY> <br />
</HTML><!--content-->could you post a link too it would be helpful. Also if the width="100%" causes you to have to scroll use this insted height="100%" the width wont be the complete width of the page but the image iwll be proportionate to the original and you wont have to scroll.<!--content-->Mr J, I adapted your code to get this:<br />
<br />
<br />
<HTML> <br />
<HEAD> <br />
<TITLE>Document Title</TITLE> <br />
<script> <br />
<br />
function init(){ <br />
width=document.body.clientWidth <br />
height=document.body.clientHeight <br />
<br />
if('width/height=>2.499'){document.getElementById("mypic").width=height*2.25; document.getElementById("mypic").height=height*0.9;};<br />
<br />
if('width/height=<2.5'){document.getElementById("mypic").width=width*0.9; document.getElementById("mypic").height=width*0.36;};<br />
<br />
} <br />
</script> <br />
</HEAD> <br />
<BODY onload="init()" onresize="init()"> <br />
<img id="mypic" src=http://www.webdeveloper.com/forum/archive/index.php/"elvisdayout.jpg" width='400' height='160'> <br />
<br />
<br />
</BODY> <br />
</HTML> <br />
<br />
<br />
<br />
Please put this code in a webpage to see it. The only problem is if the height of the frame (which I think document.body.clientWidth measures) is less then 40% of the width you need to scroll down. Please could you adapt my script to solve this?<br />
<br />
<br />
PeOfEo, the size of the frame can vary, so only using height='100%' wouldn't work.<br />
<br />
<br />
Thanks,<br />
<br />
Neil<!--content-->Please try the following<br />
<br />
<br />
<HTML> <br />
<HEAD> <br />
<TITLE>Document Title</TITLE> <br />
<script> <br />
<br />
function init(){ <br />
my_div=document.getElementById("mypic")<br />
width=document.body.clientWidth <br />
height=document.body.clientHeight <br />
<br />
my_image=new Image() <br />
my_image.src=http://www.webdeveloper.com/forum/archive/index.php/"seapic1.jpg" <br />
//my_image.width=400<br />
//my_image.height=160<br />
<br />
percw=my_image.width/my_image.height //2.5<br />
perch=my_image.height/my_image.width //0.4<br />
percent_width=my_image.width/width<br />
percent_height=my_image.height/height<br />
<br />
init2() <br />
} <br />
<br />
function init2(){ <br />
width2=document.body.clientWidth <br />
height2=document.body.clientHeight <br />
<br />
my_div.width=width2*percent_width <br />
my_div.height=(width2*percent_width )*perch<br />
<br />
if(height2<my_div.height){<br />
init3()<br />
}<br />
} <br />
<br />
<br />
function init3(){<br />
my_div.height=height2-20<br />
my_div.width=my_div.height*percw<br />
}<br />
onresize=init2 <br />
</script> <br />
</HEAD> <br />
<BODY onload="init()"> <br />
<img id="mypic" src=http://www.webdeveloper.com/forum/archive/index.php/"elvisdayout.jpg"> <br />
<br />
<br />
</BODY> <br />
</HTML><!--content-->Originally posted by neil9999 <br />
<br />
<br />
PeOfEo, the size of the frame can vary, so only using height='100%' wouldn't work.<br />
The whole point of only useing one is so that the image will be in proportion. The frame will not be a perfect square so useing two will throw the image out of proportion, this is especially true if the frame's size is not constant. It is better that the image be a little smaller then out of proportion correct?<!--content-->neil9999<br />
<br />
Have a play with the file in the zip.<br />
<br />
Now NS7 and Mozilla<!--content-->
 
Back
Top