Home General Discussion

In this thread, we figure out some PHP issues.

polycounter lvl 19
Offline / Send Message
adam polycounter lvl 19
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

  • MoP
    Options
    Offline / Send Message
    MoP polycounter lvl 18
    Hmm, that PHP looks like it actually makes a whole new image out of a supplied path (unless I'm reading it wrong) which would explain the redraw every time.

    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
  • Farfarer
    Options
    Offline / Send Message
    Yeah, it looks like it generates a new image on the fly and streams it to the browser. I believe that sort of thing is generally used to composite images together or to generate an image with text in it, resize images automatically... stuff like that.

    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 Stirling
    Options
    Offline / Send Message
    Rick Stirling polycounter lvl 18
    Just a note - Adam has tried at least 3 different versions of a PHP rotate and all had different issues. One would only rotate on a page refresh which was hardly ideal.
  • adam
    Options
    Offline / Send Message
    adam polycounter lvl 19
    What would I do to that code to have it not-make a new image each time, but rather use one of the existing gifs in that folder?

    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.
  • MoP
    Options
    Offline / Send Message
    MoP polycounter lvl 18
    I will try some stuff out. Surely you just want something that will choose a new image every time someone browses to a new page of the forum?
  • adam
    Options
    Offline / Send Message
    adam polycounter lvl 19
    Ya, that folder has about 10 images inside of it. I'd like it to pick one of those and display it, nothing more.

    Paul: its the polycount.adam theme
  • MoP
    Options
    Offline / Send Message
    MoP polycounter lvl 18
    Uh, am I being stupid? Here's my solution, it almost seems too simple to be true...
    <?php
        $num = 1;
        $imageList = array();
        while ( file_exists( "images/image" . $num . ".jpg" ) ) { 
            array_push( $imageList, ( "images/image" . $num . ".jpg" ) );
            $num++;
        }
        echo "<img src=" . $imageList[ mt_rand( 0, count( $imageList ) - 1 ) ] . ">";
    ?>
    
    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)
  • MoP
    Options
    Offline / Send Message
    MoP polycounter lvl 18
    Looks like your original code contained a more catch-all method of scanning a folder for images of various types, with a more flexible naming convention. Might wanna use that in conjunction with my oh-so-simple "echo" :)
  • Farfarer
    Options
    Offline / Send Message
    Yeah, that's pretty much how I'd have done it. As it spits out a new filename each time, then your browser will change the image even if it's cached. 'Cause it sees that it's a different file.

    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)?
  • adam
    Options
    Offline / Send Message
    adam polycounter lvl 19
    Paul, all that does it have a link to the main site with the word "Polycount" acting as that link.
  • sonic
    Options
    Offline / Send Message
    sonic polycounter lvl 18
    I'm guessing you don't need image file checking because all of the files in the folder will be images.

    This will generate a truly random image each time and will also allow the browser to cache the image.
    <?
    
    $path = getcwd() . "/banners";
    
    $dir_handle = @opendir($path) or die("Unable to open $path");
    
    $banner_no = 0;
    
    while ($file = readdir($dir_handle)) 
    {
    	if($file!="." && $file!="..")
    	{             	
    		$banner_array[$banner_no] = $file;
    		$banner_no++;
    	}
    	
    	
    }
    $banners_end = $banner_no - 1;
    $banner_to_use = rand(0,$banners_end);
    echo "<img src='banners/" . $banner_array[$banner_to_use] . "'>";
    
    closedir($dir_handle);
    
    ?>
    
  • sonic
    Options
    Offline / Send Message
    sonic polycounter lvl 18
    EDIT: now it works!

    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 :)
  • adam
    Options
    Offline / Send Message
    adam polycounter lvl 19
    Sonic, yours is doing the same as Pauls and just displaying the word "polycount" and linking to the main page of the forums.

    Tried changing $path = getcwd() . to the folder its in (/headers, not /banners) and that didn't solve it either.
  • sonic
    Options
    Offline / Send Message
    sonic polycounter lvl 18
    That PHP should be in a root directory with a subdirectory named "banners." Maybe the folder tree is funky?

    Or maybe you need to modify the echo code for where you are putting it? The code I made displays this and this only:
    <img src='banners/RANDOMBANNER.jpg'>
    

    I just tested it out on my server and it works fine :(
  • adam
    Options
    Offline / Send Message
    adam polycounter lvl 19
  • MoP
    Options
    Offline / Send Message
    MoP polycounter lvl 18
    Yeah all this stuff works fine on my web server. Maybe you need to change some PHP settings somewhere? Or like sonic says, check the file paths.
  • sonic
    Options
    Offline / Send Message
    sonic polycounter lvl 18
    adamBrome wrote: »
    Sonic, yours is doing the same as Pauls and just displaying the word "polycount" and linking to the main page of the forums.

    Tried changing = getcwd() . to the folder its in (/headers, not /banners) and that didn't solve it either.

    You'll also need to change the part where it says
    echo "<img src='banners/" . $banner_array[$banner_to_use] . "'>";
    

    to
    echo "<img src='headers/" . $banner_array[$banner_to_use] . "'>";
    

    If /headers is the directory!
  • adam
    Options
    Offline / Send Message
    adam polycounter lvl 19
    Still the same thing :(

    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:
    <?
    
     = getcwd() . "/headers";
    
     = @opendir() or die("Unable to open ");
    
     = 0;
    
    while ( = readdir()) 
    {
        if(!="." && !="..")
        {                 
             = ;
            ++;
        }
        
        
    }
     =  - 1;
     = rand(0,);
    echo "<img src='headers/" .  . "'>";
    
    closedir();
    
    ?>
    

    Edit: Even with codetags some things (like $ banner) arent showing up in the thread.
  • sonic
    Options
    Offline / Send Message
    sonic polycounter lvl 18
    Turn off the parse links and embed media thing.

    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:
    <?
    
    $path = getcwd() . "/images/headers";
    
    $dir_handle = @opendir($path) or die("Unable to open $path");
    
    $banner_no = 0;
    
    while ($file = readdir($dir_handle)) 
    {
    	if($file!="." && $file!="..")
    	{             	
    		$banner_array[$banner_no] = $file;
    		$banner_no++;
    	}
    	
    	
    }
    $banners_end = $banner_no - 1;
    $banner_to_use = rand(0,$banners_end);
    echo "<img src='images/headers/" . $banner_array[$banner_to_use] . "'>";
    
    closedir($dir_handle);
    
    ?>
    
  • MoP
    Options
    Offline / Send Message
    MoP polycounter lvl 18
    I figured out the problem, I think.
    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!
  • adam
    Options
    Offline / Send Message
    adam polycounter lvl 19
    :icon_exclaim:
  • Michael Knubben
    Options
    Offline / Send Message
    I thought MoP was just going to be mean and say: 'Bearkub is a noob'.

    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 <3)
Sign In or Register to comment.