Solved

Dynamically resizing image to Max height/width

Posted on 2006-07-12
5
321 Views
Last Modified: 2008-03-17
Hello.

I want to resize an image that is displayed with JavaScript.

   Here is some code that should do what I need done:
   http://www.experts-exchange.com/Web/Web_Languages/CSS/Q_21917056.html

            function resize(image) {
              // Thanks, GrandSchtroumpf
              var maxwidth = 200;
              var maxheight = 100;
              var scalewidth = 1;
              var scaleheight = 1;
              if (image.width > maxwidth)  scalewidth = maxwidth/image.width;
              if (image.height > maxheight)  scaleheight = maxheight/image.height;
              var scale = (scalewidth < scaleheight)? scalewidth : scaleheight;
              image.width = Math.round(scale * image.width);
              image.height = Math.round(scale * image.height);
            }

Here is the JS that I need it integrated with:

               <script>
      var par = window.parent.document;
      var images = par.getElementById('images');
      var imgdiv = images.getElementsByTagName('div')[<?=(int)$_POST['imgnum']?>];
      var image = imgdiv.getElementsByTagName('img')[0];
      imgdiv.removeChild(image);
      var image_new = par.createElement('img');
      image_new.src = 'showimage.php?pic=<?=$oname?>';
      image_new.className = 'loaded';
      imgdiv.appendChild(image_new);
      </script>



Any ideas?

Thanks!
0
Comment
Question by:hankknight
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 30

Accepted Solution

by:
GrandSchtroumpf earned 500 total points
ID: 17094476
you can try something like this:
...
     var image_new = par.createElement('img');
     image_new.onload = function() { resize(this); };
     image_new.src = 'showimage.php?pic=<?=$oname?>';
...
0
 
LVL 16

Author Comment

by:hankknight
ID: 17094643
Thanks!
0
 
LVL 30

Expert Comment

by:GrandSchtroumpf
ID: 17094672
You are welcome  :)
0
 
LVL 16

Author Comment

by:hankknight
ID: 17094701
By the way, I have posted a related but different question here:
http://www.experts-exchange.com/Web/Web_Languages/JavaScript/Q_21917309.html
0
 
LVL 30

Expert Comment

by:GrandSchtroumpf
ID: 17094765
wow, seems like you really want me to get a certification in javascript ;)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
AJAX ModalPopupExtender will Not Hide in Asp.net 1 35
HTML auto refresh to another site 4 29
Popup write two lines 3 22
Table header must be on top 2 27
Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question