Jump to content

[SOLVED] Random Image w/ PHP5


Shadow Wolf

Recommended Posts

My server host just upgraded to PHP5 and mySQL5. Unfortunately this means it broke a few scripts that were working so I need a new method to get the same effect.

 

I was using rid.php (random image script) that was fairly simple. I drop it into a directory with images, then link to it and it will pull the images up in the directory. There was never any need for me to update the script when I added images, I just needed to add the images to the directory.

 

<?php

//read folder
$folder=opendir("."); 
while ($file = readdir($folder)) 
$names[count($names)] = $file; 
closedir($folder);

//sort file names in array
sort($names);

//remove any non-images from array
$tempvar=0;
for ($i=0;$names[$i];$i++){
$ext=strtolower(substr($names[$i],-4));
if ($ext==".jpg"||$ext==".gif"||$ext=="jpeg"||$ext==".png"){$names1[$tempvar]=$names[$i];$tempvar++;}
}

//random
srand ((double) microtime() * 10000000);
$rand_keys = array_rand ($names1, 2);

//random image from array
$slika=$names1[$rand_keys[0]]; 

//image dimensions
$dimensions = GetImageSize($slika); 
if (isset($pic)){header ("Location: $slika");}
else {echo "<img src=\"$slika\" $dimensions[3]>";}
?>

 

I would then use the following code in my html to call upon it and it worked fine with years... except now it will not work anymore:

<?
function view_logo() {
global $mklib;
return <<<EOF

<!-- begin logostrip -->
  
<tr>
  <td id="mklogostrip" height="100" width="100%">
  
<a href="$mklib->siteurl/index.php"><img src="http://www.protoculturex.net/images/header/rid.php?pic=random" border="0" alt="PCX" /></a>

          </td>
</tr>

<!-- end logostrip -->

EOF;
}
?>

 

I used the script in a <img tag because it basically tricked it into thinking it was an image and didn't require me to make any modifications to the script trying to mess with an echo or get it to display the image with the way the .php is setup in the Portal software.

 

 

I tried to use a random .php script and replace the image tag using the following below. But for some reason it isn't working. It displays all the images, not a random one and it seems the closing tags for the .php part isn't working right. It treats it like regular HTML.

 

<?
function view_logo() {
global $mklib;
return <<<EOF

<!-- begin logostrip -->
  
<tr>
  <td id="mklogostrip" height="100" width="100%">

<?
$bannerAd[1] = '<a href="http://www.protoculturex.net/" target="_blank"><img src="/images/header/bleach.jpg" width="903" height="259" border="0"></a>';
$bannerAd[2] = '<a href="http://www.protoculturex.net/" target="_blank"><img src="/images/header/bucktick2.jpg" width="903" height="259" border="0"></a>';

$adCount = count($bannerAd);
$randomAdNumber = mt_rand(1, $adCount);
echo $bannerAd[$randomAdNumber];
?>

    </td>
</tr>

<!-- end logostrip -->

EOF;
}
?>

 

 

So is there an easier way to do this or a method to update the way I was doing it earlier? Any help and suggestions would be appreciated, thank you. Keep in mind I'm pretty much a newbie when it comes to php/mysql, this just seemed like the easiest way to do it at the time.

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.