My random image isn't as random anymore My random image isn't as random anymore
 

News:

cpg1.5.48 Security release - upgrade mandatory!
The Coppermine development team is releasing a security update for Coppermine in order to counter a recently discovered vulnerability. It is important that all users who run version cpg1.5.46 or older update to this latest version as soon as possible.
[more]

Main Menu

My random image isn't as random anymore

Started by aldog, November 08, 2007, 07:35:53 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

aldog

I added a random image script to control my header for my site/gallery, it was VERY random at first, but now it seems to be a lot less random.

http://ad6storieshigh.com/gallery/

Now the thing is, when I first put it on there it was very random, everytime I would refresh the page it would be different, which is exactly what I wanted. But then I started doing different things, updating all my thumbs and making mini thumbs, and enabling some options in my config that were added with modpack and now it's very rare that the image changes.

http://ad6storieshigh.com/gallery/
user: test
pass: pass

Is there a setting I might have enabled that would hinder my random php script?

In case you want to know what my random code is, here you go.
<?
/*/////////////////////////////////////////////////////////////////////////////////////////
Created by: Mr_density

Description:
A simple random image generator.
I wrote this script because i had some trouble with other similar scripts that didn't work
as well as this one.

Instructions:
1. Just put this script in the directory containing your images and connect via a
browser to this script. The script wil only show images (jpg, gif, png) not other
file formats.

2. Via an html image tag your can call the script on your site.
syntax: <img src="randompic.php"> or <img src="randompic.php?tnsize=XXX"> (to resize).

Change $config["systempath"] to the path where the images are stored (not the URL and no slash on the end).
Change $config["webpath"] to the path where the images can be found through a webbrowser (include a ending slash).

The options for $config["type"] are "redirect" and "readfile". Redirect will use a "location" header wich will display the image (kind of redirect so). Readfile will read the file from the local hardrive and directly displayed (actually I can not recommend that).
////////////////////////////////////////////////////////////////////////////////////////*/
$config["systempath"] = "./images/logo";
$config["webpath"] = "http://www.ad6storieshigh.com/gallery/themes/oranje/images/logo/";
$config["type"] = "redirect";

if($config["systempath"] != "")
{
    $dircheck = $config["systempath"];
    if (is_dir($dircheck)) //check if it is a valid systempath
    {
$pathtoread = $config["systempath"];
}
else
{
// If not, it creates an error image and displays it
$img = imagecreate(150, 150);
$red = imagecolorallocate($img, 255, 0, 0);
$yellow = imagecolorallocate($img, 255,255, 0);
imagestring($img, 4, 20, 20, "System", $yellow);
imagestring($img, 4, 20, 40, "path", $yellow);
imagestring($img, 4, 20, 60, "error!", $yellow);
imagepng($img);
imagedestroy($img);
exit();
}
}
else
{
$pathtoread = "."; //Dir where this file is stored
}
if($config["webpath"] != "")
{
$url = $config["webpath"];
}
else
{
$explode = explode("/",$_SERVER["REQUEST_URI"]);
$url = "http://".$_SERVER["SERVER_NAME"]."/";
for($i=0;$i<(count($explode)-1);$i++)
{
$url.=$explode[$i]."/";
}
}

$imgdir = opendir($pathtoread);
while($file = readdir($imgdir))
{     
$images[count($images)] = $file; //search all files
}
closedir ($imgdir);

// Removes all non-image files
$tempvar = 0;
for ($i = 0; $images[$i]; $i++)
{
$ext = strtolower(substr($images[$i],-4));
if ($ext == ".jpg" || $ext == ".gif" || $ext == "jpeg" || $ext== ".png" )
{
$images1[$tempvar] = $images[$i];
$tempvar++;
}
}

//Get a random image
$randomnr = rand(0, count($images1)-1);
$img = $images1[$randomnr];

//To resize a image
if(isset($tnsize))
{
$tnsize = (integer) $tnsize;
if (($tnsize < 20) || ($tnsize > 300))
{
// If not, it creates an error image and displays it
$img = imagecreate(150, 150);
$red = imagecolorallocate($img, 255, 0, 0);
$yellow = imagecolorallocate($img, 255,255, 0);
imagestring($img, 4, 20, 20, "Afmetingen", $yellow);
imagestring($img, 4, 20, 40, "kloppen", $yellow);
imagestring($img, 4, 20, 60, "niet!", $yellow);
imagepng($img);
imagedestroy($img);
exit();
}

if ($ext == ".jpg" || $ext == "jpeg")
{
$bigimage = @imagecreatefromjpeg($img);
}
if ($ext == ".gif")
{
$bigimage = @imagecreatefromgif($img);
}
if ($ext == ".png" || $ext == "jpeg")
{
$bigimage = @imagecreatefrompng($img);
}

//Create an empty image of the given size
$tnimage = imagecreate($tnsize, $tnsize);
$darkblue = imagecolorallocate($tnimage, 0,0, 127);
imagecolortransparent($tnimage,$darkblue);

//Calculate the resizing image factor
$sz = getimagesize($img);
$x = $sz[0];
$y = $sz[1];
if ($x > $y)
{
$dx = 0;
$w = $tnsize;
$h = ($y / $x) * $tnsize;
$dy = ($tnsize - $h) / 2;
}
else
{
$dy = 0; 
$h = $tnsize;
$w = ($x / $y) * $tnsize;
$dx = ($tnsize - $w) / 2;
}

// Resizes the image
imagecopyresized($tnimage, $bigimage, $dx, $dy, 0, 0, $w, $h,$x, $y);

// Displays the image
imagepng($tnimage);

// Clears the variables
imagedestroy($tnimage);
imagedestroy($bigimage);
}
else
{
if($config["type"]=="redirect"){
header("Location: ".$url.$img);
}
else if($config["type"]=="readfile"){
readfile($pathtoread."/".$img);
}
else
{
echo "Error: unknown displaytype";
}
}
?>

Thank you in advanced for the help or pointing me in the right direction

I apologize if this turns out to not be related to the modpack, and thank you for making such a great mod.

Stramm

This definitely isn't related to the modpack nor to CPG in general. Also it's working for me. With 10 tries I've seen all your 7 images. From reading I've first thought you're using an older php version (there you needed to seed the random nr generator). But as said... works smooth for me.

Nibbler

I see the same problem as you. Firefox is caching the background image. Try as suggested in http://www.webmasterworld.com/forum88/13000.htm.

This is not related to Coppermine, so support (on this board) is limited.

aldog

ahhh, alright. Thank you stramm for replying so quickly. I'm glad it's working for you!

Nibbler, thank you very very much for figuring out what it is, and thanks for the link.