Jump to content

Random Image Generator


e1seix

Recommended Posts

Hello all,

 

I have obtained this handy code from another website. The following is basically saved as a php file in the same directory as the pictures you want to randomly appear, and you put the php filepath as the src code for your image elsewhere to generate the display...

 

?php 


    $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); 
    } 
} 
?> 

 

My question relates to: If I then wanted to turn the random image that displays into a link, with every potential random image linking to an individual link... exactly where can I go about placing this in the code. Would it be in the <a href=...> where i link to the php file above and just use an if statement of some sort... or would it be within the above code - somewhere - and use an if statement to declare to filepath.

 

do you understand or am i just rambling?

 

many thanks,

Link to comment
Share on other sites

Just select a random image and link to it directly?

 

Thanks for your help, but you will need to explain this to me in much greater depth so i can relate it to my website.

 

If you look at dogfightuk.com, the random images i want to turn into individual links are the adverts towards bottom of the lefthandside.

 

With the code you have given me, I don't know how to relate it to the images in my directory or indeed where to place it.

 

But many thanks in advance,

Link to comment
Share on other sites

Well, instead of

<?php
// ...
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); 
    } 
} 
?>

you could do

<?php
// ...
// instead of setting $img to the path you'd set $img to the URL of the image.
if ($img!=null) { 
    echo "<a href='{$img}'>{$img}</a>";
} else { 
    echo 'Image does not exist';
} 
?>

Link to comment
Share on other sites

Hey dude. The code works brilliantly on its own, except for when i have it linked into my menu for the site.

 

i have tried both ways of:

 

echo '<img src="http://www.dogfightuk.com/advert/advert.php" />';

 

and

 

include('http://www.dogfightuk.com/advert/advert.php');

 

neither seem to work as it just displays a blank picture. what's gone wrong? lol

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.