Jump to content


Photo

[Javascript]Keeping images small


  • Please log in to reply
2 replies to this topic

#1 Mastermind

Mastermind

    Server Technician

  • Undead
  • 7,014 posts
  • Location:Cambridge, MA
  • Projects:MasterNews 3
  •  The Man Behind the Curtain

Posted 26 April 2006 - 05:44 AM

Sick of giant images posted by someone screwing up your wonderful layout? Just use this little snippet of javascript to take care of them, and scale them to something more reasonable. A word of caution though, if you use this with Internet Explorer, any images who have their display style set to none have width and height 0. If you want to fix images that are hidden at one time or another you will want to run the fixImages() again after you show them. This just scales to the size of the page, but someone with a little trickery could scale it to a certain width or height, and even restrict it to certain elements and such. But I leave that as an exercise for the reader.
<script type="text/javascript">
// Code to cap the size of images on a web page
// rather obviously is javascript and requires it
// written by Mastermind of the Revora Creative Network
// some code (getSize()) taken from an online tutorial
// This code comes with no warranty and may be redistributed freely
function open_page()
{
  getSize();
  fixImages();
}
var myWidth;
var myHeight;
// windowwidth-this gives the maximum image size
var contentRemove = 450;
function getSize() {
  myWidth = 0;
  myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
	//Non-IE
	myWidth = window.innerWidth;
	myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
	//IE 6+ in 'standards compliant mode'
	myWidth = document.documentElement.clientWidth;
	myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
	//IE 4 compatible
	myWidth = document.body.clientWidth;
	myHeight = document.body.clientHeight;
  }
}

function fixImages() {
	var images = document.body.getElementsByTagName("IMG");
	var maxWidth = myWidth-contentRemove;
	var fixcount=0;
	var nullcount=0;
	var wantalert=true;
	var badImgs = "";
	for(i=0; i<images.length; i++)
	{
		if(images[i].width > maxWidth || images[i].clientWidth>maxWidth)
		{
			fixcount = fixcount+1;
			var w = images[i].width
			var h = images[i].height
			var rat = maxWidth/w
			images[i].height = h*rat
			images[i].width = maxWidth
		}
	}
}
window.onload= open_page;
</script>

Posted Image

Well, when it comes to writing an expository essay about counter-insurgent tactics, I'm of the old school. First you tell them how you're going to kill them. Then you kill them. Then you tell them how you just killed them.

Too cute! | Server Status: If you can read this, it's up |

#2 Legion

Legion

    AHL2mod Admin

  • Banned
  • 1,225 posts
  • Location:USA
  • Projects:HL2 Community Hub
  •  There should be PINK text. :D

Posted 06 June 2006 - 02:05 AM

Anything like this for IPB? I'm sick of big images wrecking my beautiful IPB skin that I spent over a month on.
Posted Image
Made all my userbars myself :D

#3 Mastermind

Mastermind

    Server Technician

  • Undead
  • 7,014 posts
  • Location:Cambridge, MA
  • Projects:MasterNews 3
  •  The Man Behind the Curtain

Posted 06 June 2006 - 03:16 AM

In theory you could hack around in the template to add this to the javascript. But I don't know how well it would work.
Posted Image

Well, when it comes to writing an expository essay about counter-insurgent tactics, I'm of the old school. First you tell them how you're going to kill them. Then you kill them. Then you tell them how you just killed them.

Too cute! | Server Status: If you can read this, it's up |




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users