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
Related Posts
-
Porn Webmaster Forums
Is your blog suffering from these three problems?
Google Webmaster Tools for SEO
JupiterImage’s Micro Payments










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…