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 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Oct 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Image resizing, must refresh to display image

    Hi all,

    I'm not the most advanced of javascript coders. my issue is that we get images posted to us of all different sizes. but we try and manipulate them so that they meet our standards. we need to do this in a fashion that keeps the proper resolution. so if an image comes in as 480x2000 we want to make that image fit properly int a 4x3 ratio box.

    below is the code i'm using. it works properly, in that the images are in the correct ration. the problem is that you MUST hit refresh in order for the images to display, because when the page renders it displays the image EXTREMELY small, so small you almost cant see it. even adding some automatic refresh code wont make it work.

    Code:
        function seeme(location)
        {	imgobj = new Image();
    		imgobj.src = location;
    		//alert (location);
    		if (imgobj.width > imgobj.height){
    			var percentage = 440 /  imgobj.width; }
    		else {
    			var percentage = 330 / imgobj.height;  }
    			
    		newX= Math.round(imgobj.width * percentage);
    		newY= Math.round(imgobj.height * percentage);
    		//alert ("imgobj.width: " + imgobj.width)
    		//alert("imgobj.height: " + imgobj.height)
    		//alert("New width: " + newX)
    		//alert("New height: " + newY)
    		var return_value = "<img name='detailpic' src='" + location + "'  width=" + newX + " Height=" + newY + " >";
    		//alert (return_value)
    		return return_value;
    	}
    the problem is that you MUST hit refresh in order for the images to display properly. even adding some automatic refresh code wont make it work(that code is below)

    Code:
    var is_input = document.URL.indexOf('&');
    if (is_input == -1 || is_input == 1)
    { 	location.href = location.href + "&";
    	location.reload;
    }
    Here is how i am calling this function:

    Code:
    <script>
    document.write(seeme(image_location));
    </script>
    does anyone have an idea on what is wrong or a better way to do this?
    Last edited by casperpage; 10-14-2008 at 03:34 PM.

  • #2
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,534
    Thanks
    3
    Thanked 513 Times in 500 Posts
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    
    <head>
      <title></title>
    <script language="JavaScript" type="text/javascript">
    /*<![CDATA[*/
    
    function LoadMain(id,maxtime){
     var obj=document.getElementById(id);
     if (!obj.main){
      obj.main=new Image();
      obj.main.src=obj.src;
      obj.cnt=0;
      obj.maxtime=maxtime||100;
      CkLoad(obj);
     }
    }
    
    function CkLoad(obj){
     if (!obj.main.complete&&obj.cnt<=obj.maxtime){
      obj.cnt++;
      setTimeout(function(){ CkLoad(obj); },100);
     }
     else {
      if (obj.main.complete&&obj.main.width>50) MyFunction(obj);
     }
    }
    
    function MyFunction(obj){
     var imgobj=obj.main,percentage;
     if (imgobj.width > imgobj.height) percentage = 440 /  imgobj.width;
      else  percentage = 330 / imgobj.height;
     obj.width=Math.round(imgobj.width * percentage);
     obj.height=Math.round(imgobj.height * percentage);
    }
    /*]]>*/
    </script></head>
    
    <body onload="LoadMain('tst',100);" >
    <img id="tst" src="http://www.vicsjavascripts.org.uk/StdImages/Three.gif" width="50" height="50"
    
    >
    </body>
    
    </html>
    Edit

    you will need to change the time to the maximum time it will take the image to load
    Last edited by vwphillips; 10-14-2008 at 12:22 PM.
    Vic

    God Loves You and will never love you less.

    http://www.vicsjavascripts.org/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/

  • #3
    Senior Coder coothead's Avatar
    Join Date
    Jan 2004
    Location
    chertsey, a small town 25 miles south west of london, england.
    Posts
    1,742
    Thanks
    0
    Thanked 244 Times in 239 Posts
    Hi there casperpage,

    and a warm welcome to these forums.

    Here is another example for you to try...
    Code:
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
       "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <meta http-equiv="Content-Style-Type" content="text/css">
    <meta http-equiv="Content-Script-Type" content="text/javascript">
    
    <style type="text/css">
    #box {
        width:400px;
        height:300px;
        border:3px double #999;
        margin:auto;
     }
    #mypic {
        display:block;
     }
    .wider {
        width:400px;
     }
    .higher {
        height:300px;
     }
    </style>
    
    <script type="text/javascript">
    
    seeme('http://www.coothead.co.uk/images/blood.jpg');
    
    function seeme(url){
       imgobj=new Image();
       imgobj.src=url;
     }
    window.onload=function() {
    
    alert('src="'+imgobj.src+'"\n\n'+
          'width="'+imgobj.width+'px"\n\n'+
          'height="'+imgobj.height+'px"');
    
       obj=document.getElementById('mypic');
    
    if(imgobj.width>imgobj.height) {
       obj.className='wider';
     }
    else {
       obj.className='higher';
      }
       obj.src=imgobj.src;
     }
    </script>
    
    </head>
    <body>
    
    <div id="box">
    <img id="mypic" src="" alt="">
    </div>
    
    </body>
    </html>
    
    coothead

  • #4
    New to the CF scene
    Join Date
    Oct 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thank you very much. i didnt even think about the time it took to load the images.


  •  

    Posting Permissions

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