SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    ********* obeah makeda's Avatar
    Join Date
    Jun 2001
    Location
    rollin' on dubs
    Posts
    492
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Can someone explain this? PHP/Images

    I came across a blog the other day and noticed that when I clicked the refresh button, images on the page were being randomly generated. Bored that I was, I clicked the 'ViewSource'(IE) and noticed that the image tags looked like this:

    <img src="thumb2/rotate.php" width="80" height="60" />

    The source attribute on the image tag is set to a php file. So I loaded up this 'rotate.php' file and all it displays is the image. When you hit refresh on this page, the image updates. The 'view source' on that page is unavailable.

    So, I figure that there is some server side code that randomly picks a photo...what I don't understand is if I could view the source, what is being outputed? Does it just output:
    Code:
    theSelectedPicture.jpg
    ?

  2. #2
    pie??? PIE!!!! rsdl's Avatar
    Join Date
    May 2001
    Location
    Vancouver, BC Canada
    Posts
    502
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    it outputs an actual image; that's assuming the script uses GD functions and it does some on-the-fly image manipulations using those functions.

  3. #3
    ********* obeah makeda's Avatar
    Join Date
    Jun 2001
    Location
    rollin' on dubs
    Posts
    492
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'll have to read up on GD functions. Thanks.

  4. #4
    ********* Wizard silver trophy Cam's Avatar
    Join Date
    Aug 2002
    Location
    Burpengary, Australia
    Posts
    4,495
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    I doubt it would use GD, just something simple like selecting a random image file from a directory, and then sending appropriate HTTP headers and the binary image data. Something like this would do the job.
    PHP Code:
    header('Content-type: image/jpeg');
    header('Content-Disposition: inline; filename="picture.jpg"');
    header('Content-Length: '.filesize('picture.jpg'));

    readfile('picture.jpg'); 

  5. #5
    SitePoint Member
    Join Date
    Sep 2003
    Location
    London
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by DJ P@CkMaN
    I doubt it would use GD, just something simple like selecting a random image file from a directory, and then sending appropriate HTTP headers and the binary image data. Something like this would do the job.
    Yup thats the way I do it:

    PHP Code:
    <? 

    $array
    [200]; 
    $elem 0
    $dirname "images"// Image directory
    $dh opendir($dirname); 
    while (
    $file readdir($dh)) { 
    if (!
    is_dir("$dirname/$file")) 
    $array[$elem] = $file
    $elem $elem 1


    srand((double)microtime() * 10000000); 
    $rand_keys array_rand($array); 
    $temp $array[$rand_keys]; 

    header("Location: $dirname/$temp"); 

    ?>
    The only problem with them, is they don't get cached

  6. #6
    SitePoint Evangelist Daijoubu's Avatar
    Join Date
    Oct 2002
    Location
    Canada QC
    Posts
    454
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Incorrect, 304 Moved Content (Location) gets cached
    The correct way to read a dir is:
    while (false !== ($file = readdir($dh))) {
    PHP Code:
    <?php

    $array
    [200];
    $elem 0;
    $dirname "images"// Image directory
    $dh opendir($dirname);
    while (
    false !== ($file readdir($dh))) {
        if (
    $file != '.' && $file !== '..')
        {
            
    $array[$elem] = $file;
            
    $elem++;
        }
    }
    srand((double)microtime() * 10000000);
    header('Location: '.$dirname.'/'.$array[array_rand($array)]);
    ?>
    (What's your $elem for? o_O)

  7. #7
    SitePoint Member
    Join Date
    Sep 2003
    Location
    London
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That was just an example I copied and pasted.
    You might have to use a absolute path

  8. #8
    SitePoint Evangelist Daijoubu's Avatar
    Join Date
    Oct 2002
    Location
    Canada QC
    Posts
    454
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you don't want to use Location header (adding one more request), you can use the readfile/fpassthru method, make sure to use ETag (could be mtime/crc32/filesize) to enables caching
    Example:
    PHP Code:
    $file_name $dirname.'/'.$array[array_rand($array)];
    $myETag '"'.@filemtime($file_name).'"';
    $foundETag = isset($_SERVER['HTTP_IF_NONE_MATCH']) ? stripslashes($_SERVER['HTTP_IF_NONE_MATCH']) : '';
    if (
    strpos($foundETag$myETag) !== false) {
        
    // Not modified!
        
    header('HTTP/1.0 304');
        exit;
    }
    else
    {
        
    header('ETag: '.$myETag);
        
    $filenum = @fopen($file_name'rb');
        
    fpassthru ($filenum);


  9. #9
    ********* obeah makeda's Avatar
    Join Date
    Jun 2001
    Location
    rollin' on dubs
    Posts
    492
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wow! Some incredibly smart people in this forum. Thanks!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •