getButterfly Logo getButterfly

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
// https://getbutterfly.com/
//
// 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 '';
}
// Edit the following values accordingly
$ads_array = array(array(
'url' => 'http://www.google.com/',
'alt' => 'Google',
'img_url' => 'images/1.png'
),
array(
'url' => 'http://www.yahoo.com/',
'alt' => 'Yahoo!',
'img_url' => 'images/2.png'
),
array(
'url' => 'http://www.msn.com/',
'alt' => 'MSN',
'img_url' => 'images/3.png'
)
);
$ads_array_1 = array( // add or remove accordingly
array(
'url' => 'http://www.google.com/',
'alt' => 'Google',
'img_url' => 'images/1.png'
),
array(
'url' => 'http://www.yahoo.com/',
'alt' => 'Yahoo!',
'img_url' => 'images/2.png'
),
array(
'url' => 'http://www.msn.com/',
'alt' => 'MSN',
'img_url' => '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.

Note: This function does not require GD library.

Update:

line 16 should be like this:

return '<a href="' . $link_url . '" target="_blank" rel="external" rel="nofollow"><img src="' . $random_img_url . '" width="' . $img_width . '" height="' . $img_height . '" alt="' . $alt_tag . '" /></a>';

in order for the link to open in a new window.


Subscribe to getButterfly Blog

Once a week or so we send an email with our best content. We never bug you, we just send you our latest piece of content.



If you found any value in this post, agree, disagree, or have anything to add - please do. I use comments as my #1 signal for what to write about. Read our comment policy before commenting! Comments such as "Thank you!", "Awesome!", "You're the man!" are either marked as spam or stripped from URL.

33 thoughts on “PHP Random Images With Links

  1. 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

  2. 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.

  3. Hey, thanks for this php random image files

  4. how do u use the include tag?

    ?

    Warning: getimagesize(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 :

  5. 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

  6. 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.

  7. i carnt seem to get images to link parent _blank. any help

  8. @jonathan: Why? What happens? Did you try changing line 16, and add target="_blank"?

  9. 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?

  10. 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 :)

    1. 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?

    2. 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!

  11. 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.

Leave a Reply

Your email address will not be published. Required fields are marked *