|  Please subscribe to the blog if you haven't already

Reducing Oversize Images on Forums

Add

I am now maintaining this here: http://web-professor.net/wp/resize-images-javascript/


Forums that allow people to post images are subject to social attacks. People can post oversize images either intentionally of unintentionally to cause the thread page to be unreadable.

I found a solution to this problem of at some forum [can't find it now !! sorry for no link back] while googling that is
simple using Javascript.

The solution is a function that itterates over the image array of the document, after its fully loaded, and resizes if they exceed the max width. This function is nice too since it maintains the aspect ratio of the photo.

So simple.. why didn’t I think of it.

Here’s the code …

The function you put in your head
<script language='Javascript'>

     function resizeImages()
     {
          if (document.images.length > 0)          //Has images, validate sizes
          {
		var maxWidth = 400;
               var imgHeight;
               var imgWidth;
               for (var loop = 0; loop < document.images.length; loop++)
               {

                         imgWidth = document.images[loop].width;
                         imgHeight = document.images[loop].height;

                         if (imgWidth > maxWidth )
                         {
                              document.images[loop].width = maxWidth ;
                              //Proportionalize Height
                              imgHeight = imgHeight / (imgWidth/ maxWidth );
                              document.images[loop].height = imgHeight;
                         }

               }
          }
     }

</script>
Use this for your body element to fire the function off after the page loads
<body onload='resizeImages()' >

Like it? Subscribe to the blog if you haven't already
Filed under: General, Misc — Scott @ August 17, 2005 10:04 am Sphinn Digg!

1 Comment »
2005-08-18 08:21:01

Bilder in Foren mit Javascript verkleinern

Der Web Professor hat ein nettes Javascript ausgegraben, das sich sämtliche Bilder einer Seite anschaut und prüft, ob die Maße größer sind als erwünscht. Wenn es ein zu großes Bild findet, wird es verkleinert. Damit kann man verhindern, dass ein…

 
Name (required)
E-mail (required - never shown publicly)
URI
Your Comment (smaller size | larger size)
You may use <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> in your comment.