PDA

View Full Version : Dynamic Image Resize Hack


dlst
28 Jan 2002, 07:21
Ok, so your users DEMAND that you have tags activated, so you finally gave in, but now people are including huge 2000 pixel wide images that screw up your forum tables and generally make things a mess.

So, what's an easy way of preventing this huge image from beaking the page? Dynamically resize it!

The way it works is this:

When the IMG tag is parsed with the vbcode parser, the image is give a name="" attribute. It is assigned a random name, beginning with "ri_".

A Javascript function is run "onload" and "onresize"... when the page loads and when the user resizes their browser.

The function looks for all the image in the page beginning with "ri_" filename and resizes them based on some maximum width you specify. The height is calculated automatically in proportion to the width.

All those image resize, and VIOLA! Your page format stays intact and no one has to scroll forever to the right to read the posts.

Now, for the bad news:

This ONLY works on newer browsers, basically 4.0+ across the board, and I have NOT tested them all (that's your job).

This only [I]resizes the image, not reduce them... in other words, it will take just as long to download the image as it normally would. So those 3MB 2000 pixel monsters some dummy took with his new Mavica will still take forever to load. The reason this doesn't matter to me, is that most of the time other forum users will complain to the poster, not the admin.

Whew! All legalities aside, here's the good stuff:

First, you need to modify admin/functions.php. Go to about line 784 and look for the following line:

$bbcode = preg_replace("/(\[)(img)(])(\r\n)*([^\"".iif($allowdynimg,"","\?\&")."]*)(\[\/img\])/siU", "<img s

(I didn't copy the whole line, this section is unique enough for a search)

REPLACE that line with the following:



Code:
---------------
Code is only visible to licensed users, and only when logged into the forums.
---------------



Got it so far? Cool. Now, go to the "headinclude" template. Find the following line (it's near the end):

</style>

and add the following BELOW that line:



Code:
---------------
Code is only visible to licensed users, and only when logged into the forums.
---------------



Before you close that menu, edit the "fixed_width" variable (in the line that reads var fixed_width = 350;) to the "reserved space" in your page.

Now let me repeat... since this is the confusing part: There is a certain number of horizontal space taken up by your page, say by the navigation bar at the left, by the space where the username and user info is when view a post, etc. All that stuff takes up a certain amount of space, measured in pixels. That's the number to put in here.

And, of course, experiment. It varies a little from browser to browser. 350 worked well for me, and I have a pretty much stock forum with a 120 pixel nav bar on the left.

And that's it! Pretty easy ay?

As always, remember this is the beta forum for a reason, so please provide as much feedback as possible, the badder the better :) and let me know what works and what doesn't!

-dlst

Gutspiller
28 Jan 2002, 18:42
error:

Warning: Unknown modifier 'g' *******/functions.php on line 751

Fatal error: Cannot redeclare randomstring() in *******/functions.php on line 737

line 751:



Code:
---------------
Code is only visible to licensed users, and only when logged into the forums.
---------------



line 737:



Code:
---------------
Code is only visible to licensed users, and only when logged into the forums.
---------------



:confused:

dlst
29 Jan 2002, 04:21
I don't get that problem on mine...

Try renaming the function to something else, and modify the call (it's a couple lines down) as well, and see what happens.

Let me know if you need "baby step" instructions.

epic
29 Jan 2002, 05:15
no any change after install it

dlst
29 Jan 2002, 05:35
Originally posted by epic
no any change after install it

Let's verify, shall we?

Go to a page in your forum where users have posted images via the [IMG ] tag.

View the source of that page, and see if the images have the name= attribute in the image tag... you're looking for a tag that looks something like this:

<img src="/images/testimage.gif" border="0" name="ri_L1nNQBYG9I5ZPxu" alt="">

See the name="ri_L1nNQBYG9I5ZPxu" part? That's what you're looking for.

Also, please report the following:

What browser are you using?
Do you have vbCode turned on?
Do you have IMG code turned on?
What is the URL to your test forum?

-dlst

Crapforum
29 Jan 2002, 13:36
This hack can be done without changing any documents.

insert the following in your style sheet(headerinclude):
.imgfix {width:expression(document.body.clientWidth -330)}

change the number "330" when the fixed images are still to large/small. (test this number with different screen resolutions)


create a vbcodetag [IMGFIX]
use this code replacement:
<img src="{param}" class=imgfix><br>
<a href="{param}">Click for full image</a><br>


to finish it, add an IMGFIX in your vbcodebuttons
See a demo at http://www.crapforum.nl

Good luck.

dlst
29 Jan 2002, 15:54
Originally posted by Crapforum
This hack can be done without changing any documents.


Hey Crap, thanks for posting.

I don't think we're talking about the same thing. Go to http://crapforum.caveo.nl/showthread.php?threadid=381 and take a look at the second post on the page. You will see there a very wide image that forces the user to scroll if the browser window is set narrow.

I'm not saying your solution is a bad, ineffective one. It just appears we are not trying to do the same things.

Thanks.

epic
29 Jan 2002, 16:34
Originally posted by dlst


Let's verify, shall we?

Go to a page in your forum where users have posted images via the [IMG ] tag.

View the source of that page, and see if the images have the name= attribute in the image tag... you're looking for a tag that looks something like this:

<img src="/images/testimage.gif" border="0" name="ri_L1nNQBYG9I5ZPxu" alt="">

See the name="ri_L1nNQBYG9I5ZPxu" part? That's what you're looking for.

Also, please report the following:

What browser are you using?
Do you have vbCode turned on?
Do you have IMG code turned on?
What is the URL to your test forum?

-dlst
Sorry,i mean attachment is image.

Gutspiller
29 Jan 2002, 19:11
Originally posted by dlst
I don't get that problem on mine...

Try renaming the function to something else, and modify the call (it's a couple lines down) as well, and see what happens.

Let me know if you need "baby step" instructions.

yes, baby steps would be good for my noobie a$$. :D

Thanks!

dlst
29 Jan 2002, 20:52
Originally posted by Gutspiller
yes, baby steps would be good for my noobie a$$. :D


Ok, here goes:

In the line below that reads:



Code:
---------------
Code is only visible to licensed users, and only when logged into the forums.
---------------



CHANGE that to



Code:
---------------
Code is only visible to licensed users, and only when logged into the forums.
---------------



Then find the line that reads:



Code:
---------------
Code is only visible to licensed users, and only when logged into the forums.
---------------



and CHANGE that to



Code:
---------------
Code is only visible to licensed users, and only when logged into the forums.
---------------



And then let me know what happens.

dlst
29 Jan 2002, 20:56
Originally posted by epic

Sorry,i mean attachment is image.

Epic, I see you're a man of few words. Please make an exception for me and try to explain, in more than 6 words, what exactly you're having trouble with.

Do you mean your image attachments are images? Isn't that, by definition, what they are?

Do you mean your attachments are being viewed as images? If so, what's the problem?

And what about answers to the rest of my questions? URL to your test forum? Browser you are using to view your pages?

I don't mean to be short, but it's hard to help with so little input, so be a little bit more verbose!

Thanks!

Gutspiller
29 Jan 2002, 23:31
Originally posted by dlst


Ok, here goes:

In the line below that reads:



Code:
---------------
Code is only visible to licensed users, and only when logged into the forums.
---------------



CHANGE that to



Code:
---------------
Code is only visible to licensed users, and only when logged into the forums.
---------------



Then find the line that reads:



Code:
---------------
Code is only visible to licensed users, and only when logged into the forums.
---------------



and CHANGE that to



Code:
---------------
Code is only visible to licensed users, and only when logged into the forums.
---------------



And then let me know what happens.

Warning: Unknown modifier 'g' in **********/admin/functions.php on line 785

Fatal error: Cannot redeclare randomstring12345() in **************/admin/functions.php on line 771

:confused:

dlst
30 Jan 2002, 00:27
Totally weird. Can you attach your functions.php file so I can take a look?

dlst
30 Jan 2002, 00:28
Oh yeah, and what version of PHP are you running? In older versions you can't declare a function within a function...

epic
30 Jan 2002, 05:10
Originally posted by dlst


Epic, I see you're a man of few words. Please make an exception for me and try to explain, in more than 6 words, what exactly you're having trouble with.

Do you mean your image attachments are images? Isn't that, by definition, what they are?

Do you mean your attachments are being viewed as images? If so, what's the problem?

And what about answers to the rest of my questions? URL to your test forum? Browser you are using to view your pages?

I don't mean to be short, but it's hard to help with so little input, so be a little bit more verbose!

Thanks!
I attach a image in post.
when i viewing the post the image(attachment) do not been auto resized.
Thx.Can you help me?

dlst
30 Jan 2002, 06:33
Originally posted by epic

I attach a image in post.
when i viewing the post the image(attachment) do not been auto resized.
Thx.Can you help me?

What is the URL to your forum?

epic
30 Jan 2002, 10:26
Oh,i have some error when edit file.
Now it is be fixed.
Thx.dlst

Gutspiller
30 Jan 2002, 16:16
How do I find out what php version I am using? I am using Ventures Online and I would think they are using the latest php, since many other VB users are using them.

here's my functions.php, I am using 2.0.3 of VB.

Lesane
30 Jan 2002, 16:42
Originally posted by Gutspiller
How do I find out what php version I am using? I am using Ventures Online and I would think they are using the latest php, since many other VB users are using them.


make a file called for example info.php and put this code in it:



Code:
---------------
Code is only visible to licensed users, and only when logged into the forums.
---------------



save it , upload it to your webserver and execute the file and then you will get a long table with information about the installation of your php.

dlst
30 Jan 2002, 17:07
Originally posted by Gutspiller
here's my functions.php, I am using 2.0.3 of VB.

This version of function.php is not modified... post your modified version so I can find the error.

Gutspiller
30 Jan 2002, 18:38
Originally posted by dlst


This version of function.php is not modified... post your modified version so I can find the error.

OK I have PHP Version 4.1.0 (Thanks Lesane)

and here is the attachment of the modified index.php.


I just found the line you said, made sure the entire line was selected and pasted in the new code, I don't see how I could of messed anything up. :confused:

dlst
30 Jan 2002, 19:56
Originally posted by Gutspiller


OK I have PHP Version 4.1.0 (Thanks Lesane)

and here is the attachment of the modified index.php.


I just found the line you said, made sure the entire line was selected and pasted in the new code, I don't see how I could of messed anything up. :confused:

Hey Gutspiller...

I'm really sorry, but I can't diagnose your problem any further. I'm using vb 2.2.1 and it has a different functions file... I don't have a copy of the old vb, so I can't test your file any further. Sorry man.

Is there anyone out there that is still using the old version of vb that would like to help diagnose this?

Synicide
01 May 2003, 19:10
Well, talk about bringing a thread out from the dead... :p
Does this hack work with any versions after 2.2.1? Because I believe I have a different functions.php file. o_O;