Jump to content

Random image generator help request


tonto68

Recommended Posts

Hello, I'm a total rookie at this. I have a script which generates a random image from a file ( of images uploaded by my forum users) . It works fine except one user uploads a .png image with all of his posts and its' showing up too frequently. I thought I had the script pulling just jpg and jpeg images but for some reason this .png shows up. I'm trying to modify the code to block or at least limit this .png image from showing up. The file is also 541 bytes(?) if that helps. The other images (pics) are much larger sizes so maybe a line of code specifying a minimum file size would work.  My code is below. Please let me know how I should modify this (please give step by step instructions because this is Greek to me)...thanks!  My code is below:

<?php
Header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
Header("Expires: Thu, 19 Nov 1981 08:52:00 GMT");
Header("Pragma: no-cache");
Header("Content-Type: image/jpg");
Header("Content-Type: image/jpeg");


$dir = "forums/uploads/images"; // This is the folder where the images are

srand((double)microtime()*1000000);
$i = 0;
$dirHandle = opendir($dir); // Open the images folder
while(($im = readdir($dirHandle)))
{
if($im != ".." && $im != ".") // Don't read in the 2 folders ".." and "."
{
$image[$i] = $im; // Select an image
$i++;
}
}
closedir($dirHandle); // Close the folder
$n = rand(0,(count($image)-1));

if(!readfile($dir."/".$image[$n])) // Read the image
readfile($dir."error/error.gif"); // If the script can't find the directory, display this image
?>
Link to comment
https://forums.phpfreaks.com/topic/34154-random-image-generator-help-request/
Share on other sites

[code]<?php

header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
header("Expires: Thu, 19 Nov 1981 08:52:00 GMT");
header("Pragma: no-cache");
header("Content-Type: image/jpg");
header("Content-Type: image/jpeg");


$dir = "forums/uploads/images"; // This is the folder where the images are

srand((double)microtime()*1000000);
$i = 0;
$dirHandle = opendir($dir); // Open the images folder
while(($im = readdir($dirHandle)))
{
if($im != ".." && $im != ".") // Don't read in the 2 folders ".." and "."
{
$ext = strtolower(strrchr($filename,"."));
if($ext == ".jpg" || $ext == ".jpeg" || $ext == ".jpe")
{
$image[$i] = $im; // Select an image
$i++;
}
}
}
closedir($dirHandle); // Close the folder
$n = rand(0,(count($image)-1));

if(!readfile($dir."/".$image[$n])) // Read the image
readfile($dir."error/error.gif"); // If the script can't find the directory, display this image

?>[/code]

I added a part that checks if the extension is jpg, jpe or jpeg.

Orio.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.