e1seix Posted December 14, 2007 Share Posted December 14, 2007 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, Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted December 14, 2007 Share Posted December 14, 2007 Just select a random image and link to it directly? Quote Link to comment Share on other sites More sharing options...
e1seix Posted December 14, 2007 Author Share Posted December 14, 2007 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, Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted December 14, 2007 Share Posted December 14, 2007 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'; } ?> Quote Link to comment Share on other sites More sharing options...
e1seix Posted December 14, 2007 Author Share Posted December 14, 2007 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 Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted December 14, 2007 Share Posted December 14, 2007 You might want to create a function which handles getting a random image. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.