Cable Forum

Cable Forum (https://www.cableforum.uk/board/index.php)
-   General IT Discussion (https://www.cableforum.uk/board/forumdisplay.php?f=19)
-   -   Resize images on page with javascript bookmarklet (https://www.cableforum.uk/board/showthread.php?t=33622977)

Alien 26-10-2007 14:37

Resize images on page with javascript bookmarklet
 
I don't really have a head for coding, but I need a bit of javascript modified. I was wondering if perhaps someone might know how to change it to get the desired result.

What I have at the moment is a "bookmarklet" that rescales/shrinks images on a page. It's useful for when people post images that are too large on forums, etc. The 1 problem with it is that it appears to shrink all images on the page, not just the ones that are too large.
Heres what I have.
Code:

javascript:(function(){ function zoomImage(image, amt) { if(image.initialHeight == null) { /* avoid accumulating integer-rounding error */ image.initialHeight=image.height; image.initialWidth=image.width; image.scalingFactor=1; } image.scalingFactor*=amt; image.width=image.scalingFactor*image.initialWidth; image.height=image.scalingFactor*image.initialHeight; } var i,L=document.images.length; for (i=0;i<L;++i) zoomImage(document.images[i],.5); if (!L) alert(%22This page contains no images.%22); })();
The .5 in the code seems to be a scaling factor, which I've experimented with, & .75 seems to be better, as it's not often that posted images are twice as wide as they should be to fit on a page without making the page wider than the window.

I'm guessing the simplest way to do it would be some sort of image width check, & only resize images with a width => a set limit [e.g. 800]. If anyone can help it would be appreciated.

Mr_love_monkey 26-10-2007 14:53

Re: Resize images on page with javascript bookmarklet
 
try this
Code:

javascript:(function(){ function zoomImage(image, amt) { if(image.initialHeight == null) { /* avoid accumulating integer-rounding error */ image.initialHeight=image.height; image.initialWidth=image.width; image.scalingFactor=1; } image.scalingFactor*=amt; image.width=image.scalingFactor*image.initialWidth; image.height=image.scalingFactor*image.initialHeight; } var i,L=document.images.length; for (i=0;i<L;++i) {if (document.images[i].width>800) zoomImage(document.images[i],.5);} if (!L) alert(%22This page contains no images.%22); })();
if you want a different limit just change the 800

---------- Post added at 13:53 ---------- Previous post was at 13:52 ----------

also if you want height taken into account you change just replace the bit with the width with
Code:

(document.images[i].width>800 && document.images[i].height>400)

Alien 26-10-2007 15:28

Re: Resize images on page with javascript bookmarklet
 
Quote:

Originally Posted by Mr_love_monkey (Post 34422341)
try this
Code:

javascript:(function(){ function zoomImage(image, amt) { if(image.initialHeight == null) { /* avoid accumulating integer-rounding error */ image.initialHeight=image.height; image.initialWidth=image.width; image.scalingFactor=1; } image.scalingFactor*=amt; image.width=image.scalingFactor*image.initialWidth; image.height=image.scalingFactor*image.initialHeight; } var i,L=document.images.length; for (i=0;i<L;++i) {if (document.images[i].width>800) zoomImage(document.images[i],.5);} if (!L) alert(%22This page contains no images.%22); })();
if you want a different limit just change the 800

Cheers dude, much appreciated, greeny headed your way. :cool: I've changed the limit to 750, & the scaling factor to .75, & bound it to a mouse gesture. Thanks to you adding the limit, if the scaling factor isn't enough to reduce a really big image, I can just use the mouse gesture again without shrinking images that aren't too big. Much appreciated. :tu:

Quote:

Originally Posted by Mr_love_monkey (Post 34422341)
also if you want height taken into account you change just replace the bit with the width with
Code:

(document.images[i].width>800 && document.images[i].height>400)

Height was never an issue, but thanks anyway. :)

v0id 26-10-2007 15:34

Re: Resize images on page with javascript bookmarklet
 
http://www.huddletogether.com/projects/lightbox/


http://5thirtyone.com/archives/549

Alien 26-10-2007 15:50

Re: Resize images on page with javascript bookmarklet
 
The problem with those is they're meant for the webmaster, not the person viewing the page. The bookmarklet which MLM very helpfully modified can be used on any page by the person viewing the page. I agree that it would be ideal if all sites included some code that detected the size of the window & scaled images accordingly, but this bookmarklet is a good substitute for that.


All times are GMT +1. The time now is 23:13.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2026, vBulletin Solutions Inc.
All Posts and Content are © Cable Forum