Solved

Dynamically resizing image to Max height/width

Posted on 2006-07-12
5
322 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Read text on Table 7 50
Put javascript to get dropdown value 3 34
close pop up windows when logout or main window closes 17 88
How do I split a variable to newline 2 44
In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
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…

710 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