<?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 "<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' => '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
?>
While searching on the internet for a viable PHP array randomizer function, this is what I ended up with. It works perfectly, there are url, alt, img_url.
I don’t know how to show text over/with the image?
Simple:
1) Add title to the array :
2) Replace your function with
If You want Your title to be next to the image or whatever, just make the relevant corrections in html :