PDA

View Full Version : Automatic Image Resizing Javascript


Dorign
08-17-2005, 10:00 PM
This is my first mod. So.. God be with you all. :ninja:


Description: This is, to my knowledge, an image resize script that works any time an image is displayed in a post on your forum. It uses a simple javascript and the onload function found in many body tags. I have heard that there is some problem with the WYSIWYG, and though I'm not sure what that means, surely this modification corrects that.
This javascript resizes images when the following occurs:
- If an image appears over the limit you specify on any given post..
--- when in showthread mode (...showthread.php?t=####).
--- when in showpost mode (...showthread.php?p=####).
--- when in newreply mode while replying (topic review display).

Difficulty: Oughtta be fairly easy, just a few simple steps. Five minutes, if that.

Edits: Three PHP file edits, and one template edit.
root/showthread.php
root/includes/functions_showthread.php
root/includes/functions_editor.php
template:headinclude

Pros: Fast modification, doesn't slow down the forum display, resizes the images on every instance of the post, cached or not. Works in both IE and Mozilla browsers (fairly old and simple javascript, probably works with old browsers as well), and also on Apple's Safari.

Cons: The user must have java enabled or they will not see the code work, the page must be fully loaded before the resizing takes place, it will only resize images on posts (not private messages or other areas, though this could be added if needed).

Support: I have agreed to support this, as if it breaks your forum, I'd naturally want to help; that's not going to happen, though! :up: Simple, easy code. If I can't support you, I'll break my back trying.

Warning: Again, this is my first mod, so I'm sorry if it's a bit crude. It works 100% for me, but I'm posting it in beta since.. well, since no one else has tested it; since I'm not really a coder, only advanced coders should install this until someone looks at it and says "It's good to go!" :nervous:

Version History:
8/18/05 - 1.00 - It's born and submitted as a beta. :ninja:
8/20/05 - 1.01 - No additions, but tested on cross platforms. Works! Released, no longer beta!

After I've had a few installs and :up:'s from people, I'll request to have it made "official." ;)
And without further delay, away we go.

------
START
------
STEP 1
------
In template headinclude
Scroll to bottom of file - Paste
<script language="javascript" type="text/javascript">
<!--
function resize_images()
{
for (i = 0; i < document.images.length; i++)
{
while ( !document.images.complete )
{
break;
}
if ( document.images[i].width > XREPLACEX )
{
document.images[i].width = XREPLACEX;
}
}
}

//-->
</script>
Be sure to change both [i]XREPLACEX instances to a width, such as "500."
------
STEP 2
------
In php file (root) showthread.php
Find
// workaround for header redirect issue from forms with enctype in IE
// (use a scrollIntoView javascript call in the <body> onload event)
$onload = '';
Replace with, or modify to
// workaround for header redirect issue from forms with enctype in IE
// (use a scrollIntoView javascript call in the <body> onload event)
$onload = 'resize_images();';
Still in php file (root) showthread.php
Find
if (!$QRrequireclick AND $WYSIWYG >= 1)
{
$onload .= " editInit();";

}
Replace with, or modify to
if (!$QRrequireclick AND $WYSIWYG >= 1)
{
$onload .= " resize_images(); editInit();";

}
Save file showthread.php, upload to root directory.
------
STEP 3
------
In php file (includes) functions_showthread.php
Find
$onload = " if (is_ie || is_moz) { fetch_object('currentPost').scrollIntoView(!is_moz ); }";
Replace with, or modify to
$onload = "resize_images(); if (is_ie || is_moz) { fetch_object('currentPost').scrollIntoView(!is_moz ); }";
Save file functions_showthread.php, upload to includes directory.
------
------
STEP 4
------
In php file (includes) functions_editor.php
Find
// set the onload event for the body tag
$onload = ' onload="editInit();"';
Replace with, or modify to
// set the onload event for the body tag
$onload = ' onload="resize_images(); editInit();"';
Still in php file (includes) functions_editor.php
Find
$onload = ' onload="editInit();"';
Replace with, or modify to
$onload = ' onload="resize_images(); editInit();"';
Save file functions_editor.php, upload to includes directory.
------
DONE
------
If you use it, please hit install! :P
------

If you have questions, please ask in this thread.
If you appreciate the modification, as a thanks, you could always link to my forum or try and get me more members. ^_^

http://www.forum-central.com/

For those of you who need screenshots, here you go. :nervous:

Antivirus
08-19-2005, 01:50 AM
great idea Dorign,
I havent instyalled yet myself, as i'm on 3.5 rc2 but i was just wondering if it constrains proportions when resizing the images, or if it only resizes, say the wqidth, and not the height?

Dorign
08-19-2005, 03:00 AM
Thanks. ^_^ It resizes the width and proportionally resizes the height, keeping the image looking realistic. ;)

Allan
08-19-2005, 12:19 PM
work on 3.5 RC2 ?

Dorign
08-19-2005, 12:30 PM
I don't know, I don't have 3.5 RC2. I would imagine if 3.5 RC2 has a headinclude, and $onload functions in .php files, then yes. ;) I've never seen the RC2 code, though, so don't take my word for it.

Allan
08-19-2005, 12:40 PM
ok, thank you :)

ps: very nice your forum ;)

Dorign
08-19-2005, 02:03 PM
Thank you. I was tinkering with it while you were looking, probably. The footer was all goofy. Was trying a new one out, but.. meh. I give up. :P

Aeolian_X
08-19-2005, 03:04 PM
This is certainly something I need. I have a question though. Since you still have to wait until the page fully loads, this won't help with load times for dial up users when someone posts a huge image, correct?

Dorign
08-19-2005, 08:03 PM
If the dialup user does not let the page load, that is correct; they will still see large images that break forum width. However, as there are a few dialup users on my forums (god bless the patient souls ;)) who were happy when this fix was implemented, so I suppose all's well there.

Seems five people have installed. Everybody getting things sorted okay? Any troubles?

yoyoyoyo
08-20-2005, 01:24 AM
great job- thanks for sharing! :D

Dorign
08-20-2005, 04:54 AM
Unless anyone has any objections, I think I'll be making this a full version. No code edits would be required; I just can't seem to find any errors with the code.

Dorign
08-20-2005, 08:39 PM
I've just been told this modification also works when viewed with Safari. Yay. ^_^ I'd say this is done being beta tested. Release, woo.

dai-kun
08-20-2005, 09:03 PM
Nice, Only problem is that my forum logo gets resized too as well :(

Dorign
08-20-2005, 09:15 PM
Well, for that, you could simply split your logo into parts, and then put them in a table. That's what I did for Forum Central. ^_^

ggiersdorf
09-04-2005, 06:45 PM
Nice Hack though I have a request can you make it clickable to show you full size in another window???

Dorign
09-05-2005, 02:40 PM
The script would need to be rewritten to make posted images clickable, and it would slow the speed of the script down by over 200% surely. As for your header image, this script isn't programmed to be integrated with the vB post code (which slows forums down if you do it that way), so it will resize every single image over your set width, regardless if it's in a post or on the page. The work around to this, and the one I use at www.forum-central.com, is to break the header up into a 2 or 3 part image and use tables to align it. That would be more webfriendly than having an 800 pixel wide image, anyway. ;)

vietfancy
09-06-2005, 02:08 AM
is this work with 3.07?

Dorign
09-08-2005, 01:07 AM
Works with any 3.0.x version.

wirewolf
09-11-2005, 10:56 AM
Nice hack Dorign. Simple and edits very easy (clicks' install).
Here's an example on my forum (3.0.9):
http://www.shipmodeling.net/vb_forum/thread907.html
Just what I needed. The original image my user posted was over 1100 px's wide. I set my limit to no more than 600 px's.
Tested it with IE, Firefox and Nutscape. All seem ok

PS, I added a little notice to my users in the "Post Thread' and in the 'Edit Post' templates to let them know that any over sized image they link to will be resized automatically so they don't think they're going crazy.

Inferno Dragon
09-26-2005, 05:31 PM
is there any way of adding a small notice below the resized image like "Click here to View the Full Image" and then it's open in a new window?

ggiersdorf
09-29-2005, 12:09 PM
Exactly what I was asking for.. something when you click that actually does not effect the current window but opens the image in a seperate window (tab) at full resolution..

Hawk7173
10-10-2005, 12:43 AM
Just installed it and it works like a charm....Thanks much!

Codeman05
12-08-2005, 01:01 PM
works great! Thanks!

funinthesun
12-08-2005, 05:45 PM
How could I use this to resize my banner?

chris_m
08-22-2007, 04:02 PM
When I just try to do step 1 of this mod, I get this error:

Not Acceptable
An appropriate representation of the requested resource /forum/admincpanel/template.php could not be found on this server.

:confused: