I’ve been trying to enhance one of my current themes with a dynamic header. The header was supposed to show different images with different links to section of the blog. While searching on the internet for a viable PHP array randomizer function, this is what I ended up with. It works perfectly.
<?php // Random Image With Link // Blog Tycoon // http://www.blogtycoon.net/ // // Usage: // // Save this file as ads.php and use the include function to call it inside your web site function display_random_img($array) { $key = rand(0 , count($array) -1); $link_url = $array[$key]['url']; $alt_tag = $array[$key]['alt']; $random_img_url = $array[$key]['img_url']; list($img_width, $img_height) = getimagesize($random_img_url); return "<a href=\"$link_url\"><img src=\"$random_img_url\" width=\"$img_width\" height=\"$img_height\" alt=\"$alt_tag\" /></a>"; } // Edit the following values accordingly $ads_array = array( array( 'url' => 'http://www.google.com/', 'alt' => 'Google', 'img_url' => '/php_random_images_with_links/images/1.png' ), array( 'url' => 'http://www.yahoo.com/', 'alt' => 'Yahoo!', 'img_url' => '/php_random_images_with_links/images/2.png' ), array( 'url' => 'http://www.msn.com/', 'alt' => 'MSN', 'img_url' => '/php_random_images_with_links/images/3.png' ) ); $ads_array_1 = array( // add or remove accordingly array( 'url' => 'http://www.google.com/', 'alt' => 'Google', 'img_url' => '/php_random_images_with_links/images/1.png' ), array( 'url' => 'http://www.yahoo.com/', 'alt' => 'Yahoo!', 'img_url' => '/php_random_images_with_links/images/2.png' ), array( 'url' => 'http://www.msn.com/', 'alt' => 'MSN', 'img_url' => '/php_random_images_with_links/images/3.png' ) ); echo display_random_img($ads_array); echo display_random_img($ads_array_1); // add or remove accordingly ?>
Download the PHP script with included images here: PHP Random Images (84)
Note: This function does not require GD library.
Hey, thanks for this php random image files, everything is working perfectly in my website, but now because the images have a link attached, there´s now a blue border around the image that I don´t know how to get it off the images. do you have any idea of what I can do? I aprecciate any help
You could place in you CSS stylesheet “img {border:0}”, without the quotes. Or add “border=”0″” inside the image tags, again without the quotes.
Hey, thanks for this php random image files
how do u use the include tag?
?
Warning: getimagesize(/php_random_images_with_links/images/3.png) [function.getimagesize]: failed to open stream: No such file or directory in /home/content/s/i/m/simonshum/html/wp-content/themes/SR3/ads.php on line 15
It confuses me because that’s exactly where i put the php file.. Help? i’m doing this in wordpress
When i did this, it returned something like this :
Is the image too big? Is the path correct? What is on line 15?
i have the same problem than Chip. The line 15 is
list($img_width, $img_height) = getimagesize($random_img_url);
My pictures are 237×178
Thanks the scripts sounds good
Guys, I am using this script for random ads shown on several of my sites. I don’t have any problem with it. It seems that the array is not defined correctly. This code
$random_img_url = $array[$key]['img_url'];
extracts the images from the folder using url, alt and img_url keys.That’s why I’m asking if the path is correct, if the images exist and if you didn’t forgot a single-quote or something. Could you put the script online and point me to it? I’ll look in the source and tell you if anything is broken.
i carnt seem to get images to link parent _blank. any help
@jonathan: Why? What happens? Did you try changing line 16, and add
target="_blank"
?I got the script working on my site… it kept telling me the imagesize cannot be blank… so I removed that from script. How can I use this script to call up multiple images from the same random-ads.php script?
Thanks for feedback! The imagesize function issue could have been caused by some corrupt or unknown file format.
@Mike Wilday: this function calls the random image, right?
echo display_random_img($ads_array);
What you need to do is duplicate the $ads_array array – like $ads_array_1 – and call the function using your array:
echo display_random_img($ads_array);
echo display_random_img($ads_array_1);
echo display_random_img($ads_array_2);
Duplicate as many arrays as you want. Oh, and see the updated article
Will the multiple arrays avoid posting duplicate images? Or do I have to keep separate lists for each image? I’m hoping to create one list of images and links and then have the code call randomly from that one list, posting two images, but not the same ones. Any help?
Thanks for a great script. I’m also interested in adapting it so that it will check for duplicates. Right now I have 6 images, so I just put 3 in each array, but I’d like each array to pull from all 6, but avoid duplicating. Thanks!
Thank you Becki for using my script. Right now, you cannot do what you want. By having 2 arrays, you have 2 functions, so there’s no way of checking the current array item in both functions at the same time. I’d suggest you use 6 differrent images, so there’s no worry of duplicating.
Thanks, but script not working properly, same image displaying twice sometimes.
It’s a basic script and it needs more functionality and checks in order to display diferrent images.
I have tried adding target=”_blank” on lines 16 and 12 but the script crashes each time. I tried
return “ and
return “
both crash the script and prevent my page from continuing to load.
Can you tell me how I can set the script so the link opens in a new tab?
Thanks for the awesome script, Chip
Nevermind, I found my mistake. I needed to use single quotes around the _blank code.
Heh, glad it worked out for you. You could have escaped the double quotes with a backslash \”.