Jump to content

[SOLVED] Image Resizer


mcmuney

Recommended Posts

Can anyone provide a javascript that will resize any image that exists on a page that exceeds a given number to a different size? For example, lets say that an image exists on the page which has a width of 1200. On the script, my criteria for the width is 600. In this example, since the image being displayed at 1200, which exceeds the 600 maximum, it'll display the image at 600.

 

Thanks.

Link to comment
Share on other sites

try this (but you will have to give each of your pictures a unique id - you can do this dynamically or statically):

 


<script language="javascript">
function checkSize(wide,what)
{
if (wide > 600) {
document.getElementById(what).style.width="600";
}
}
</script>

<img id="pic1" src="picture1.jpg" onload="checkSize(this.width,this.id)">

Link to comment
Share on other sites

actually try this instead; this version will maintain the current height and only change the width:

 


<script language="javascript">
function checkSize(wide,high,what)
{
if (wide > 600) {
document.getElementById(what).style.width="600";
document.getElementById(what).style.height=""+high+"";
}
}
</script>

<img id="pic1" src="test.jpg" onload="checkSize(this.width,this.height,this.id)">

Link to comment
Share on other sites

its work in FFv2.0.0.4 and IE6 (I did not test in IE7, don't have it on this computer - but it should work the same way); because I tested in both. the second version should not distort the image; I made it, so it would only resize the width of the image. the first version I made resized the width with javascript and the browser was automatically resizing the height due to the width size. this was because the image (as you stated) would not have a standard width/height property.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.