Hey everyone,
I have a huge favour to ask and I am hoping someone out there knows a few tricks
I've got a few days off and I am busy working on the Polycount stuff. One of the new features is our headers will be rolling. (Heads will roll! Ha!). Anyway, the artwork for the headers will be of artwork by you guys (more on that later).
Currently, with what I have, its working and looks great. However, the images aren't caching, causing the images to be downloaded each time. I am hoping someone with extensive PHP knowledge could point me in the right direction for remedying this problem.
Here's the code I am using:
$folder = '.';
$extList = array();
$extList['gif'] = 'image/gif';
$extList['jpg'] = 'image/jpeg';
$extList['jpeg'] = 'image/jpeg';
$extList['png'] = 'image/png';
$img = null;
if (substr($folder,-1) != '/') {
$folder = $folder.'/';
}
if (isset($_GET['img'])) {
$imageInfo = pathinfo($_GET['img']);
if (
isset( $extList[ strtolower( $imageInfo['extension'] ) ] ) &&
file_exists( $folder.$imageInfo['basename'] )
) {
$img = $folder.$imageInfo['basename'];
}
} else {
$fileList = array();
$handle = opendir($folder);
while ( false !== ( $file = readdir($handle) ) ) {
$file_info = pathinfo($file);
if (
isset( $extList[ strtolower( $file_info['extension'] ) ] )
) {
$fileList[] = $file;
}
}
closedir($handle);
if (count($fileList) > 0) {
$imageNumber = time() % count($fileList);
$img = $folder.$fileList[$imageNumber];
}
}
if ($img!=null) {
$imageInfo = pathinfo($img);
$contentType = 'Content-type: '.$extList[ $imageInfo['extension'] ];
header ($contentType);
readfile($img);
} else {
if ( function_exists('imagecreate') ) {
header ("Content-type: image/png");
$im = @imagecreate (100, 100)
or die ("Cannot initialize new GD image stream");
$background_color = imagecolorallocate ($im, 255, 255, 255);
$text_color = imagecolorallocate ($im, 0,0,0);
imagestring ($im, 2, 5, 5, "IMAGE ERROR", $text_color);
imagepng ($im);
imagedestroy($im);
}
}
Thanks to anyone who can help me!
Replies
Seems like it should just randomly pick a URL to an existing image and display that, would be better for caching them. I may have a look tomorrow if I have time, but PHP isn't really my fort
Having a dir with all the headers and picking a randomised jpg from there should be a lot easier to do, but have php simply echo out the raw html to display the image as an <img> tag, rather than the php file becoming the image file the browser sees.
Rick is correct, him and I tried 3 different php files and 2 had the same result, the other would only generate a new image on a hard-refresh of the page (F5, not hitting the refresh button).
So ya..
Thoughts? Thanks everyone.
Paul: its the polycount.adam theme
Example of it working here (you'd basically just use that code wherever you wanted the random image to go):
http://www.greveson.co.uk/test/randomimage1.php
Edit: Added a loop to scan a folder for images matching the convention "/images/image1.jpg", and use that as the array of things to choose from, so you'd just add/remove images from the FTP folder to add/remove them from the loop, eg "image14.jpg".
Could probably be improved, currently this would stop if your folder had image1.jpg, image2.jpg and image4.jpg (it'd never reach image4)
I'm guessing that the hard-refresh problems you were having was because the script wrote to the same image file each time it switched it out (meaning that the browser would display the cached version even if it had changed server-side)?
This will generate a truly random image each time and will also allow the browser to cache the image.
Also, I want to add that a few of those variables can easily be cut down but I put them in there for the sake of it being easier to read.
EDIT2: Also, you will never have to generate a list of images or do anything. Just drop any image at all in the folder and it'll be added
Tried changing $path = getcwd() . to the folder its in (/headers, not /banners) and that didn't solve it either.
Or maybe you need to modify the echo code for where you are putting it? The code I made displays this and this only:
I just tested it out on my server and it works fine
You'll also need to change the part where it says
to
If /headers is the directory!
the file is located in /images/rotate.php
The images I want to use are in /images/headers
The code for rotate.php is this:
Edit: Even with codetags some things (like $ banner) arent showing up in the thread.
Are you doing an include for rotate.php on the main page? If so, a lot of the time it will change the relative path to the file that's including it rather than the file included. Maybe try putting an absolute path or make the relative path go from the root instead of the "images" directory.
For example if that is the case, see if this works:
It's nothing to do with the PHP code being wrong, it's just being included in the HTML wrongly... I am working on it now!
Looking forward to the new design.
edit: oh shit, I thought this was Bearkub's thread. So Adam's the noob? Well stop the presses! (I kid )