/    Sign up×
Community /Pin to ProfileBookmark

random Image Script with different links per image

hey guys,

i am having problems trying to get my head around a problem.

on my website, i have adverts running down one side and because they can do upto say 5 deep i have a random image script with randomly selects one of the jpgs in a certain folder:

Script:

[code=php]<?php

$folder = ‘.’;

$extList = array();
$extList[‘gif’] = ‘image/gif’;
$extList[‘jpg’] = ‘image/jpg’;
$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);
}
}
?>

[/code]

it works great but my problem is, with each different image theres going to be a different link.

been trying to think of ways of doing this, like maybe adding a function to the page which checks the name of the image displayed then puts the herf link as required, but i dont know where to start with it.

any help at all would be awesome

thanks

to post a comment
PHP

9 Comments(s)

Copy linkTweet thisAlerts:
@Kyleva2204Aug 07.2008 — The current way your script is, it cannot be done.. Mainly because you are using a PHP Script as a proxy for other images, meaning you can't get the currently viewed images filename..
Copy linkTweet thisAlerts:
@VadeauthorAug 07.2008 — hi thanks for the reply,

tbh i thought that would be the problem so i did some googling and found this script which i cant get to work ( parse error on the first echo line apprently)

[code=php]<?PHP
/* *****************
Random Image with Link 2.01
[email protected]
This uses $level from my code for
the link level determiner
***************** */

function display_random_img($align)

{

//seed the random number generator
mt_srand((double)microtime()*1000000);

//get a random number
$rand_image = mt_rand(0 , 1);

// +++Array of URLs+++
//Corresponding numbers will start with 0.
//When adding images and directories
//remember to increase mt_rand max number

$arr_numlinks = array(
array('num' => '0' ,
'url' => 'http://www.tiger24.com' ,
'alt' => 'best surf cams in the world!'
),
array('num' => '1' ,
'url' => 'http://www.tiger24.com/forum.html' ,
'alt' => 'Tiger24 Awesome Forum'
)
);

//match random number with a URL and assign it a string value
$key = $rand_image;
$rand_image_num = $arr_numlinks[$key][num];
$link_url = $arr_numlinks[$key][url];
$alt_tag = $arr_numlinks[$key][alt];

//use number to create a image URL string
//create directory /rotators in the images directory
//name rotating images rotate[number].jpg


$rand_image_url = "images Files/Adverts/Camera Pages/Folder3/ad${rand_image_num}.gif";

//figure out the image size (thanks jsalido)
$imgsize = GetImageSize ("$rand_image_url");
$img_w = $imgsize[0];
$img_h = $imgsize[1];

//echo the image and its URL
echo "<a href="${link_url}">";
echo "<img src="$rand_image_url" align ="$align" border="0" ";
echo "width="$img_w" heigth="$img_h" ";
echo "alt="$alt_tag">";
echo "</a>";
};
?>[/code]
Copy linkTweet thisAlerts:
@Kyleva2204Aug 07.2008 — I'm thinking that the person who made the script didn't debug it..
[code=php]
<?PHP
/* *****************
Random Image with Link 2.01
[email protected]
This uses $level from my code for
the link level determiner
***************** */

function display_random_img($align)

{

//seed the random number generator
mt_srand((double)microtime()*1000000);

//get a random number
$rand_image = mt_rand(0 , 1);

// +++Array of URLs+++
//Corresponding numbers will start with 0.
//When adding images and directories
//remember to increase mt_rand max number

$arr_numlinks = array(
array('num' => '0' ,
'url' => 'http://www.tiger24.com' ,
'alt' => 'best surf cams in the world!'
),
array('num' => '1' ,
'url' => 'http://www.tiger24.com/forum.html' ,
'alt' => 'Tiger24 Awesome Forum'
)
);

//match random number with a URL and assign it a string value
$key = $rand_image;
$rand_image_num = $arr_numlinks[$key][num];
$link_url = $arr_numlinks[$key][url];
$alt_tag = $arr_numlinks[$key][alt];

//use number to create a image URL string
//create directory /rotators in the images directory
//name rotating images rotate[number].jpg


$rand_image_url = "images Files/Adverts/Camera Pages/Folder3/ad${rand_image_num}.gif";

//figure out the image size (thanks jsalido)
$imgsize = GetImageSize ("$rand_image_url");
$img_w = $imgsize[0];
$img_h = $imgsize[1];

//echo the image and its URL
echo "<a href="$link_url">";
echo "<img src="$rand_image_url" align ="$align" border="0" ";
echo "width="$img_w" heigth="$img_h" ";
echo "alt="$alt_tag">";
echo "</a>";
};
?>
[/code]


Try that. He had a lot of quotes in quotes that werent backslashes:

echo "<img src="myurl.com">"; -- WRONG

echo "<img src="myurl.com">"; -- Right ^_^
Copy linkTweet thisAlerts:
@VadeauthorAug 07.2008 — cheers again buddy,

but it hink the whole script is full of fail, nothing seems to be working on it, the image link is comingout as adArray as opposed to ad1 etc, kind of newish to php so i cant really debug it myself very easily ?
Copy linkTweet thisAlerts:
@VadeauthorAug 07.2008 — nevermind, i fixxed it somehow
Copy linkTweet thisAlerts:
@VadeauthorAug 07.2008 — Sorry for the third reply in a row ?

but can you think of anyway i can do this without the need to have 5 different funtions on page?

beacuse my layout is 5 ads down the right all with about 5 different possible adverts.

would it be possible to have one function with a few different variables as arguments for example the folder location of the image? say $imgLoc in the () at top and then when you put the url of the image you can put

img_url = 'images files/some folder/'.$imgLoc.'/folder3/ad${something}.gif'


the other thing is the link itself and alt tags in the array,

because say potentially i have 20 adverts on the page which appear in 5 locs? would i need to have an array with 20 enteries per pages or 5 different funtions with 4 arrays so that the same image and link isnt shown twice on the same page?

cheers
Copy linkTweet thisAlerts:
@Kyleva2204Aug 07.2008 — I didnt see your previous reply until now.. I'm working on it ? It wont be too hard.

Edit again -- im not quite sure I understand what your asking..

Last edit-- Maybe this is what you needed??

Ok maybe not last edit -- There were a few bugs with my script, sorry.. This works perfectly, I also deleted unnecessary code.
---


[code=php]<?PHP

function display_random_img($align,$array)

{
$key = rand(0 , count($array) -1); // pick out an ad
$link_url = $array[$key]['url']; // Set the link url
$alt_tag = $array[$key]['alt']; // Set the alt tag
$random_img_url = $array[$key]['img_url']; // Set the image url.
list($img_width, $img_height) = getimagesize($random_img_url); // Get image size
return "<a href="$link_url"><img src="$random_img_url" align="$align" border="0" width="$img_width" height="$img_height" alt="$alt_tag"></a>"; // return the text.
};

$first_set_of_ads = array(
array(
'url' => 'http://www.tiger24.com',
'alt' => 'best surf cams in the world!',
'img_url' => '/1350.jpg'
),
array(
'url' => 'http://www.tiger24.com/forum.html',
'alt' => 'Tiger24 Awesome Forum',
'img_url' => '/3871.jpg'
)
);
echo display_random_img('left',$first_set_of_ads);

$second_set_of_ads = array(
array(
'url' => 'http://www.webdeveloper.com',
'alt' => 'great site!',
'img_url' => '/1350_second.jpg'
),
array(
'url' => 'http://www.webdeveloper.com/forum.html',
'alt' => 'Webdeveloper's Awesome Forum!',
'img_url' => '/3871_second.jpg'
)
);
echo display_random_img('left',$second_set_of_ads);
?>[/code]
Copy linkTweet thisAlerts:
@VadeauthorAug 07.2008 — thanks alot for that

it makes perfect sense what you have put there, with the different arrays the only thing i am worried about is having lots of arrays etc in the header of my page will that slow down the page at all?

again forgive any "newbie" mistakes but kinda of a large learning curve for me here ?

the other question is can you have a script like the awesome you have written but say outside the page and then called in on page load so if i have say 10 pages which all need that script on i can just call it in rather than have the same script in the header of each page?

thanks alot for all your help so far

vade
Copy linkTweet thisAlerts:
@Kyleva2204Aug 07.2008 — arrays wont slow down your script, atleast not noticeable to a human. To include that script VIA a file just put it in another PHP file, and use this line of code:
[code=php]require('my_ad_script.php');[/code]

where my_ad_script.php is, replace with the name of the php file and directories.. You can also put the arrays in that file if you want to save on clutter ^_^
×

Success!

Help @Vade spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.24,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...