Ive got that generic script that you put in an image folder, reference it on your html page and it rotates thru all the images in the folder on refresh. I was wondering if I could make each image clickable so the user can go to a different website after clicking an image? Heres the script. <?php /* AUTOMATIC IMAGE ROTATOR Version 2.2 - December 4, 2003 Copyright (c) 2002-2003 Dan P. Benjamin, Automatic, Ltd. All Rights Reserved. http://www.hiveware.com/imagerotator.php http://www.automaticlabs.com/ DISCLAIMER Automatic, Ltd. makes no representations or warranties about the suitability of the software, either express or implied, including but not limited to the implied warranties of merchantability, fitness for a particular purpose, or non-infringement. Dan P. Benjamin and Automatic, Ltd. shall not be liable for any damages suffered by licensee as a result of using, modifying or distributing this software or its derivatives. ABOUT This PHP script will randomly select an image file from a folder of images on your webserver. You can then link to it as you would any standard image file and you'll see a random image each time you reload. When you want to add or remove images from the rotation-pool, just add or remove them from the image rotation folder. VERSION CHANGES Version 1.0 - Release version Version 1.5 - Tweaked a few boring bugs Version 2.0 - Complete rewrite from the ground-up - Made it clearer where to make modifications - Made it easier to specify/change the rotation-folder - Made it easier to specify/change supported image types - Wrote better instructions and info (you're them reading now) - Significant speed improvements - More error checking - Cleaner code (albeit more PHP-specific) - Better/faster random number generation and file-type parsing - Added a feature where the image to display can be specified - Added a cool feature where, if an error occurs (such as no images being found in the specified folder) *and* you're lucky enough to have the GD libraries compiled into PHP on your webserver, we generate a replacement "error image" on the fly. Version 2.1 - Updated a potential security flaw when value-matching filenames Version 2.2 - Updated a few more potential security issues - Optimized the code a bit. - Expanded the doc for adding new mime/image types. Thanks to faithful ALA reader Justin Greer for lots of good tips and solid code contribution! INSTRUCTIONS 1. Modify the $folder setting in the configuration section below. 2. Add image types if needed (most users can ignore that part). 3. Upload this file (rotate.php) to your webserver. I recommend uploading it to the same folder as your images. 4. Link to the file as you would any normal image file, like this: <img src="http://example.com/rotate.php"> 5. You can also specify the image to display like this: <img src="http://example.com/rotate.php?img=gorilla.jpg"> This would specify that an image named "gorilla.jpg" located in the image-rotation folder should be displayed. That's it, you're done. */ /* ------------------------- CONFIGURATION ----------------------- Set $folder to the full path to the location of your images. For example: $folder = '/user/me/example.com/images/'; If the rotate.php file will be in the same folder as your images then you should leave it set to $folder = '.'; */ $folder = '.'; /* Most users can safely ignore this part. If you're a programmer, keep reading, if not, you're done. Go get some coffee. If you'd like to enable additional image types other than gif, jpg, and png, add a duplicate line to the section below for the new image type. Add the new file-type, single-quoted, inside brackets. Add the mime-type to be sent to the browser, also single-quoted, after the equal sign. For example: PDF Files: $extList['pdf'] = 'application/pdf'; CSS Files: $extList['css'] = 'text/css'; You can even serve up random HTML files: $extList['html'] = 'text/html'; $extList['htm'] = 'text/html'; Just be sure your mime-type definition is correct! */ $extList = array(); $extList['gif'] = 'image/gif'; $extList['jpg'] = 'image/jpeg'; $extList['jpeg'] = 'image/jpeg'; $extList['png'] = 'image/png'; // You don't need to edit anything after this point. // --------------------- END CONFIGURATION ----------------------- $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); /* fixes cahce issue, thanks to dchapiesky */ header( "Expires: Mon, 20 Dec 1998 01:00:00 GMT" ); header( "Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT" ); header( "Cache-Control: no-cache, must-revalidate" ); header( "Pragma: no-cache" ); 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); } } ?> PHP:
The only way I can think of to do it is sort of complicated. You could use javascript to create a random string and pass it on in the query string in the call to the script. Then modify the script a bit to write the name of the file to a file or database along with the random id that was generated with javascript. Then use an ajax call after the page has been loaded to read the file names and make the image linkable using javascript. There has to be an easier way, but that's the only way I can think of right now.
i would think he would have to be... the header is saying it is outputting an image if you look at the comments on the script it tells you that it is used like <img src=""> etc. .... i mean i knwo he can just open up the thing and it will generate a random image but i think he is calling it using the <img> tag as for a solution to your problem... i would say you could name the images like... www.url.com.jpg that way you could use PHP to obtain the url right from the image name... kind of a weird way to do it but it is eassy enough to do
Yeah it's almost certainly being used somewhat like this <img src="script_name.php" /> . The script lets you input the image name if you want, so if you know the image name ahead of time you can just do this <a href="filename.gif"><img src="script_name.php?img=filename.gif" /></a> . I think. I haven't actually run the script but that's how it looks like it works. If you don't specify the img parameter in the query string it returns a random image.
dont want to sound degrading, but this isnt what he is asking... he wants to know how to make the images into links
That should make a link of the image if he knows the name of the image ahead of time. I'm assuming he wants a random image, so he won't know the image name ahead of time, so the ajax solution was all I could think of for that. How am I missing what he's asking?
Oh, doh, I just reread it. He wants to link out to another site. Well that depends. Is each image associated with a different site? Are all images related to one site? If it's the latter then simply do <a href="http://theurl.com/thepage.html"><img src="the_image_script.php" /></a>. If you have multiple sites to link to and different images are associated with different sites, then you should probably create a database table to hold the image name and the url associated with it to link out to. Then when an image is chosen you can choose the correct url from the database to link out to. But I think you'd still have to use javascript an ajax and query the server after the image has been served to get the actual link from the server.
Thanks for the input, sounds like its getting beyond this script and it might be easier just to get a different script. I wanted each image linkable to a site relating to the image. Like right now I have an anmesty international banner, ubuntu banner...... and I want people to be able to click on the image and be forwarded to that site.
If the page(s) that are going to actually generate the link code are php you can also break that script up and just include the script and run functions to generate the link code and the image, instead of using javascript. But it sounds like you just want a banner rotator. Why not check out openx or something similar? You should be able to do what you want without any coding.
I do agree, I have a script which is not longer than 10 lines of code and images are clickable, if you want it let me know and I will be posting it
This is too complicated, I myself would do: $directory = 'images/'; $links = array('www.google.com','www.yahoo.com','www.lycos.com'); $image = glob($directory.'*'); $image = $image[rand(0,count($image)-1)]; $link = $links[rand(0,count($links)-1)]; echo '<a href="'.$link.'"><img src="'.$image.'"></a>'; PHP: This takes a random link, you can specify links for images easily too. Peace,
you may be able to use curl to go to google enter the image name not the extenstion and create the anchor from the first result but it would be slightly complicated if you really want this to happen you could pay and get it done!
Here's a version that associates images with links: <?php $directory = 'images/'; $links = array('Image1.gif'=>'www.google.com', 'Image2.gif'=>'www.yahoo.com','Image3.gif'=>'www.lycos.com'); $image = glob($directory.'*'); $image = $image[rand(0,count($image)-1)]; $link = $links[basename($image)]; echo '<a href="'.$link.'"><img src="'.$image.'"></a>'; ?> PHP:
I just tried that and nothing showed up. I made a folder with all the images I saved it as rotate.php changed the $directory = 'images/'; to the directory I have the images in put this in the body <img src="http://plentyoftorrents.com/images/rotate.php"> and nothing happened.
This script works differently than the first one you posted. These are the three lines that create a linked image. Just copy and paste them whenever you want another link. $image = $image[rand(0,count($image)-1)]; $link = $links[basename($image)]; echo '<a href="'.$link.'"><img src="'.$image.'"></a>'; PHP: