Click to See Complete Forum and Search --> : Random Image


majikku
03-14-2005, 03:11 PM
I am trying to get a code for PHP that will allow me to have a random icon in a forum. I have tried 2 so far but they dont work. I need it so maybe it has the "random.jpg" extention instead of the "random.php"

can someone help me?

the tree
03-14-2005, 03:24 PM
Are you wanting to generate images on the fly or pick on of a selection of already saved random images? The later would be easier, but it sounds like the code that you've got is for the former.

bokeh
03-14-2005, 03:44 PM
Much of this code is thanks to "phpnovice".

<?php

// name 10 jpeg files as 1.jpg through 10.jpg
// put them in the same directory as this file

//Call the random image using this html tag <img src="this_file.php">

$n = rand(1, 10);

header("Content-Type: image/jpeg");
header("Content-Disposition: inline");
@readfile($n . '.jpg');

?>

DJsAC
03-14-2005, 05:07 PM
If you want to call the .php file .jpg and still have it function as php code (in case 'dynamic' images aren't accepted) you can put this in a .htaccess file in the same folder as wel.

RewriteEngine on
RewriteBase /server_root/path/to/imagefolder/
RewriteRule ^random\.jpg$ random.php [T=application/x-httpd-php]


If you include <img src="../images/etc/random.jpg"> in your file, it will run the .php file, and tell the server it's still a jpeg file, instead of .php
That way it still an 'image' instead of a dynamic-stream :D

bokeh
03-14-2005, 05:59 PM
Could you explain that a bit more comprehensively please as I can't get it to work on my server.

DJsAC
03-15-2005, 12:26 AM
1st line: Turn Mod ReWrite Engine On
2nd Line: Tell the server which folder it should change
so
/www/myfiles/images/ on the server corresponds to http://www.mysite.tld/myfiles/images/

3rd line: Tell the server that whenever it gets a request for the file 'random.jpg' It should open and execute 'random.php'. But then save the results of that file as a normal jpeg header.

That's all I know about it, It only works on one of my servers, so I'm guessing different versions of different servers require a slightly modified script.:eek: It's all just a simple .htaccess rewrite rule. Apache - mod_rewrite.html (http://httpd.apache.org/docs/mod/mod_rewrite.html)

bokeh
03-15-2005, 12:51 PM
The bit I am having trouble with is this:
RewriteRule ^random\.jpg$ random.php [T=application/x-httpd-php]

random.php is the file that exist, right? and and random.jpg is something apache pretends exists, correct?
Also should this:

RewriteBase /server_root/path/to/imagefolder/
be the samt as $_SERVER['document_root'] plus any sub directories?

DJsAC
03-15-2005, 01:12 PM
RewriteRule ^random\.jpg$ random.php [T=application/x-httpd-php]
RewriteRule ^XXXXXX\.xxx$ YYYYYY.YYY [T=application/x-httpd-php]

^XXXXXX\.xxx$ is a normal REGEX expression saying that whatever files they try to open that match should open the file YYYYY.YYY

^XXXXXX Means the file starts (^) with XXX followed by a . (\.) and then the next part of the filename xxx which should also be the last part of the match: $ ($ means whatever comes right before that should be the last part of the string)

^random\.jpg$ Will match random.jpg, but NOT match to randomfile.jpg random.jpeg random.jpg?id=4 or file_random.jpg The string has to start (^) with random, followed by a period (\.) and end with jpg ($)

IF the file matches It will 'get' you YYYYYY.YYY (in your case random.php which is the existing file on your server) And eventhough the browser asked for an image, open it as [T=application/x-httpd-php] (MIME TYPE for PHP files)
----------------------------------------
The RewriteBase should be relative to the site's 'home' page.
lets say your site is www.mysite.com
you'll have a www or htdocs directory which has the webfiles in it.
so if you have the images in www/images/random/ the RewriteBase is /images/random/
(more info here, because I suck at explaining that kind of things..)
http://httpd.apache.org/docs/mod/mod_rewrite.html#RewriteBase

bokeh
03-15-2005, 02:50 PM
My server refused to work with:RewriteRule ^random\.jpg$ random.php [T=application/x-httpd-php] but worked perfect with:RewriteRule ^/random\.jpg$ /random.php [T=application/x-httpd-php]
I only managed to find this out because I set up a temporary RewriteLog with RewriteLogLevel 9.

By the way to majikku how did you get on with this?

bokeh
03-15-2005, 02:53 PM
The forum stripped the slashes. Lets try again:
This didn't work
RewriteRule ^random\.jpg$ random.php [T=application/x-httpd-php]
and this did
RewriteRule ^/random\.jpg$ /random.php [T=application/x-httpd-php]

DaiWelsh
03-16-2005, 08:17 AM
I don't remember the details but IIRC apache sometimes treats rules as directory relative and sometimes as root relative depending on where they are specified, so maybe that is why it is behaving differently for different people?