Jump to content

Recommended Posts

Hi,

I am trying to customize a script that shows random images..

i am obviously doing something wrong because the script does not show any images

can someone help me?

 

 


<?php
//this is the file for all the function

function getRandomImage($dir,$type='random')
{
global $errors,$seed;

  if (is_dir($dir)) {

  $fd = opendir($dir);
  $images = array();

      while (($part = @readdir($fd)) == true) {

          if ( @eregi("(gif|jpg|png|jpeg)$",$part) ) {
              $images[] = $part;
          }
      }

    // adding this in case you want to return the image array
    if ($type == 'all') return $images;

    if ($seed !== true) {
      mt_srand ((double) microtime() * 1000000);
      $seed = true;
    }

      $key = mt_rand (0,sizeof($images)-1);

    return $dir . $images[$key];

  } else {
      $errors[] = $dir.' is not a directory';
      return false;
  }
}


$query = "SELECT * FROM images WHERE is_published=1";
$result = mysql_query ($query); // Run the query

while ($row = mysql_fetch_array($result, MYSQL_BOTH)) {
$image_id = $row['image_id'];

$image_filename = "uploaded_images/" . $image_id . ".jpg";


$image = getRandomImage($image_filename);
echo 'The value of image is: ' . $image . '<br \>';

echo "<img src='$image_filename' width='519' height='353' border='0' usemap='#Map' \>";
}
?>

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/198678-customizing-a-php-script/
Share on other sites

You are attempting to use the function for a use it wasn't designed for.

 

This function is design to pass a directory full of images to it, and it will select a random image from that directory.  You are trying to give it an image name, but I don't know how you expect it to return a random image from that.

 

Instead try:

<?php
//this is the file for all the function

function getRandomImage($dir,$type='random')
{
global $errors,$seed;

  if (is_dir($dir)) {

  $fd = opendir($dir);
  $images = array();

      while (($part = @readdir($fd)) == true) {

          if ( @eregi("(gif|jpg|png|jpeg)$",$part) ) {
              $images[] = $part;
          }
      }

    // adding this in case you want to return the image array
    if ($type == 'all') return $images;

    if ($seed !== true) {
      mt_srand ((double) microtime() * 1000000);
      $seed = true;
    }

      $key = mt_rand (0,sizeof($images)-1);

    return $dir . $images[$key];

  } else {
      $errors[] = $dir.' is not a directory';
      return false;
  }
}




$image = getRandomImage('uploaded_images/');


echo "<img src='$image' width='519' height='353' border='0' usemap='#Map' \>";

?>

When you define the function you have:

function getRandomImage($dir,$type='random')

it should be:

function getRandomImage($dir,$type)

Then when you call the function you use something like:

getRandomImage($imgdir,'random')

Next problem:

The type=random does not matter cause if it not "all" the function returns one random image.

 

 

HTH

Teamatomic

Un-tested, but you can try:

<?php

$query = "SELECT * FROM images WHERE is_published!=1";
$result = mysql_query ($query); // Run the query

while ($row = mysql_fetch_array($result, MYSQL_BOTH)) {
$images_not_published[] = $row['image_id'];
}


function getRandomImage($dir,$type='random')
{
global $errors,$seed,$images_not_published;

  if (is_dir($dir)) {

  $fd = opendir($dir);
  $images = array();

      while (($part = @readdir($fd)) == true) {

          if ( @eregi("(gif|jpg|png|jpeg)$",$part) ) {
              $images[] = $part;
          }
      }
$images = array_values(array_diff($images,$images_not_published));
    // adding this in case you want to return the image array
    if ($type == 'all') return $images;

    if ($seed !== true) {
      mt_srand ((double) microtime() * 1000000);
      $seed = true;
    }

      $key = mt_rand (0,sizeof($images)-1);

    return $dir . $images[$key];

  } else {
      $errors[] = $dir.' is not a directory';
      return false;
  }
}

$image = getRandomImage('uploaded_images/');


echo "<img src='$image' width='519' height='353' border='0' usemap='#Map' \>";
?>

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.