Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6

Thread: image resize

  1. #1
    New to the CF scene
    Join Date
    Dec 2005
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    image resize

    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" >

  • #2
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    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.
    Code:
    <!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.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #3
    New to the CF scene
    Join Date
    Dec 2005
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    Last edited by beckybear01; 12-29-2005 at 07:02 AM.

  • #4
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    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.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #5
    New to the CF scene
    Join Date
    Dec 2005
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?

  • #6
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Sighs. Then just use .theimg as the class.
    Code:
    <!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.
    ||||If you are getting paid to do a job, don't ask for help on it!||||


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •