...

View Full Version : image resize



beckybear01
12-28-2005, 06:44 PM
i need a code that can resize images by %. like if i have a picture thats normally 640x480 but i only want it to be 320x240 i would want it to be resized by 50%. but i dont want to just write "width:320; height:240;" if that makes sense.

i tried "width:50%;height:50%;" and it did not work. i know there is a code for this, bc i used to use it all the time. but its coming up as a blank at the moment. thanks.

my code looks like this

.img {border: 10px solid #ffffff; height:240; width:320;}
and then at the image it looks like this, which works... but i want to use a % instead of px.

< img src="image.jpg" class="img" >

_Aerospace_Eng_
12-28-2005, 11:37 PM
Using percent will only be a percentage of the element that is holding the image not a percent of the image. Resizing images in html is never good because quality could be lost due to pixelization. You can use some javascript to resize the images on page load.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
<script type="text/javascript">
function setImage(){
var theimgs = document.getElementsByTagName('img');
var i;
for(i=0;i < theimgs.length;i++){
var theimgwidth=theimgs[i].offsetWidth;
var theimgheight=theimgs[i].offsetHeight;
if(theimgs[i].className=='theimg'){
theimgs[i].style.width=(theimgwidth/2)+'px'
theimgs[i].style.height=(theimgheight/2)+'px';
}
}
}
window.onload=setImage;
</script>
</head>

<body>
<img src="image1.jpg" class="theimg" alt=""><br>
<img src="image2.jpg" class="theimg" alt=""><br>
<img src="image3.jpg" class="theimg" alt=""><br>
</body>
</html>

The way this works is any image that has class="theimg" in it will get resized by 50% of itself, rather than the element that holds it.

beckybear01
12-29-2005, 05:59 AM
okay, but is there a way to put the border in there too?

i really dont care if its not the best quality bc im only reducing it by half, and im just trying to do everything as easy as possible. or else i would just go into psp and resize all the images myself... but i dont want to do all the work lol. :)

my current coding works most of the time, but sometimes i have random pictures that are 480x640 instead of the normal 640x480. and i dont want them to be stretched.

_Aerospace_Eng_
12-29-2005, 06:09 AM
Are you wanting the width of the image to include the border or are you wanting the width to be the width of image plus the border? You could just use the class like you were doing previously in CSS.

beckybear01
12-29-2005, 07:04 AM
i want the border to be additional to the width. but i dont want to even set a limit to what the image's width can be, just want it half as big as whatever its natural size is. plus a 10px border.

and wouldnt

<img src="image1.jpg" class="theimg" class="img" >

mess things up?

_Aerospace_Eng_
12-29-2005, 07:12 AM
Sighs. Then just use .theimg as the class.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
<style type="text/css">
body {
margin:0;
padding:10px;
background:#000000;
color:#FFFFFF;
}
.theimg {
border:10px solid #FFF;
}
</style>
<script type="text/javascript">
function setImage(){
var theimgs = document.getElementsByTagName('img');
var i;
for(i=0;i < theimgs.length;i++){
var theimgwidth=theimgs[i].offsetWidth;
var theimgheight=theimgs[i].offsetHeight;
if(theimgs[i].className=='theimg'){
theimgs[i].style.width=((theimgwidth+10)/2)+'px'
theimgs[i].style.height=((theimgheight+10)/2)+'px';
}
}
}
window.onload=setImage;
</script>
</head>

<body>
<img src="image1.jpg" class="theimg" alt=""><br>
<img src="image2.jpg" class="theimg" alt=""><br>
<img src="image3.jpg" class="theimg" alt=""><br>
</body>
</html>
It doesn't matter what the width is. The javascript will get the width of the image add 10px then divide by 2, it will do the same thing for the height so the image width + image border will be divided by 2 as will the height.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum