Jump to content

Click on image.


zhanna

Recommended Posts

Hello,

 

I am facing a problem and i can't solve it.

What my code do?

It automatically loads an image from a folder randomly on any page i wish, i specially use it for banners.

 

In pages i placed, size is witdh 200 and height 200, but real size is not 200/200. All i want is the image loaded should be clickable to view full size of it.

 

My Code:imgs.php

<?

error_reporting(0);

if ($handle = opendir('files')) {

 

  $count=0;

  /* Dit is de juiste manier om door een directory te wandelen. */

  while (false !== ($file = readdir($handle))) {

if(getimagesize("files/".$file)) {

$count++;

      $dirlist[$count]="files/".$file;

    }

  }

 

  closedir($handle);

  $filenr = rand(1,$count);

  print($dirlist[$filenr]);

 

}

?>

 

Then i use this small script to show in my pages.

 

<img src="<? include("imgs.php"); ?>" cellpadding="0" width="200" height="200">

 

this will load a random image from imgs.php with size of 200/200, All i want is, i want the images to be clicable, if i click on them, i see full size of the images.

 

please  Can anyone of you help me ?

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/117196-click-on-image/
Share on other sites

You would be better off making it a function then assigning the random picture name to a variable so it will be clickable.

 

<?php
error_reporting(0);
function get_pic(){
  $dir = "files";
  if ($handle = opendir($dir)) {
  $count=0;
  /* Dit is de juiste manier om door een directory te wandelen. */
    while (false !== ($file = readdir($handle))) {
      if(getimagesize("$dir/".$file)) {
      $count++;
      $dirlist[$count]="$dir/".$file;
      }
    }
  closedir($handle);
  $filenr = rand(1,$count);
  return ($dirlist[$filenr]);
  }
}

$pic = get_pic();

echo "<a href=\"$pic\" /><img src=\"$pic\" cellpadding=\"0\" cellspacing=\"0\" width=\"200\" height=\"200\" /></a>\n";
?>

 

Ray

Link to comment
https://forums.phpfreaks.com/topic/117196-click-on-image/#findComment-602838
Share on other sites

You would be better off making it a function then assigning the random picture name to a variable so it will be clickable.

 

<?php
error_reporting(0);
function get_pic(){
  $dir = "files";
  if ($handle = opendir($dir)) {
  $count=0;
  /* Dit is de juiste manier om door een directory te wandelen. */
    while (false !== ($file = readdir($handle))) {
      if(getimagesize("$dir/".$file)) {
      $count++;
      $dirlist[$count]="$dir/".$file;
      }
    }
  closedir($handle);
  $filenr = rand(1,$count);
  return ($dirlist[$filenr]);
  }
}

$pic = get_pic();

echo "<a href=\"$pic\" /><img src=\"$pic\" cellpadding=\"0\" cellspacing=\"0\" width=\"200\" height=\"200\" /></a>\n";
?>

 

Ray

 

Thank You Ray.

 

but one more problem, i did use the way you mentioned.

 

I made a table, it has 4 row and 4 column.

 

In each row, i added this code.

<? echo "<a href=\"$pic\" /><img src=\"$pic\" cellpadding=\"0\" cellspacing=\"0\" width=\"150\" height=\"150\" /></a>\n"; ?>

 

As i refresh, i am getting same images everywhere. i have 8 image in one page, all images are same. I want them random.

 

Is it possible to show random images with one function ?

Link to comment
https://forums.phpfreaks.com/topic/117196-click-on-image/#findComment-602863
Share on other sites

you would need to either call the function 8 times or change the function to give you 8 images in an array. Personally I would store the 8 images in an array then call them from the array.

 

<?php
error_reporting(0);
function get_pic(){
  $dir = "files";
  if ($handle = opendir($dir)) {
  $count=0;
  /* Dit is de juiste manier om door een directory te wandelen. */
    while (false !== ($file = readdir($handle))) {
      if(getimagesize("$dir/".$file)) {
      $count++;
      $dirlist[$count]="$dir/".$file;
      }
    }
  closedir($handle);
  for($i=0; $i<8; $i++){
  $filenr = rand(1,$count);
  $images[] = $dirlist[$filenr];
  }
  return $images;
  }
}

$pics = get_pic();
// now your 8 images would be this
echo "<a href=\"".$pics[0]."\" /><img src=\"".$pics[0]."\" cellpadding=\"0\" cellspacing=\"0\" width=\"200\" height=\"200\" /></a>\n";
echo "<a href=\"".$pics[1]."\" /><img src=\"".$pics[1]."\" cellpadding=\"0\" cellspacing=\"0\" width=\"200\" height=\"200\" /></a>\n";
echo "<a href=\"".$pics[2]."\" /><img src=\"".$pics[2]."\" cellpadding=\"0\" cellspacing=\"0\" width=\"200\" height=\"200\" /></a>\n";
echo "<a href=\"".$pics[3]."\" /><img src=\"".$pics[3]."\" cellpadding=\"0\" cellspacing=\"0\" width=\"200\" height=\"200\" /></a>\n";
echo "<a href=\"".$pics[4]."\" /><img src=\"".$pics[4]."\" cellpadding=\"0\" cellspacing=\"0\" width=\"200\" height=\"200\" /></a>\n";
echo "<a href=\"".$pics[5]."\" /><img src=\"".$pics[5]."\" cellpadding=\"0\" cellspacing=\"0\" width=\"200\" height=\"200\" /></a>\n";
echo "<a href=\"".$pics[6]."\" /><img src=\"".$pics[6]."\" cellpadding=\"0\" cellspacing=\"0\" width=\"200\" height=\"200\" /></a>\n";
echo "<a href=\"".$pics[7]."\" /><img src=\"".$pics[7]."\" cellpadding=\"0\" cellspacing=\"0\" width=\"200\" height=\"200\" /></a>\n";
?>

 

Ray

Link to comment
https://forums.phpfreaks.com/topic/117196-click-on-image/#findComment-602885
Share on other sites

you would need to either call the function 8 times or change the function to give you 8 images in an array. Personally I would store the 8 images in an array then call them from the array.

 

<?php
error_reporting(0);
function get_pic(){
  $dir = "files";
  if ($handle = opendir($dir)) {
  $count=0;
  /* Dit is de juiste manier om door een directory te wandelen. */
    while (false !== ($file = readdir($handle))) {
      if(getimagesize("$dir/".$file)) {
      $count++;
      $dirlist[$count]="$dir/".$file;
      }
    }
  closedir($handle);
  for($i=0; $i<8; $i++){
  $filenr = rand(1,$count);
  $images[] = $dirlist[$filenr];
  }
  return $images;
  }
}

$pics = get_pic();
// now your 8 images would be this
echo "<a href=\"".$pics[0]."\" /><img src=\"".$pics[0]."\" cellpadding=\"0\" cellspacing=\"0\" width=\"200\" height=\"200\" /></a>\n";
echo "<a href=\"".$pics[1]."\" /><img src=\"".$pics[1]."\" cellpadding=\"0\" cellspacing=\"0\" width=\"200\" height=\"200\" /></a>\n";
echo "<a href=\"".$pics[2]."\" /><img src=\"".$pics[2]."\" cellpadding=\"0\" cellspacing=\"0\" width=\"200\" height=\"200\" /></a>\n";
echo "<a href=\"".$pics[3]."\" /><img src=\"".$pics[3]."\" cellpadding=\"0\" cellspacing=\"0\" width=\"200\" height=\"200\" /></a>\n";
echo "<a href=\"".$pics[4]."\" /><img src=\"".$pics[4]."\" cellpadding=\"0\" cellspacing=\"0\" width=\"200\" height=\"200\" /></a>\n";
echo "<a href=\"".$pics[5]."\" /><img src=\"".$pics[5]."\" cellpadding=\"0\" cellspacing=\"0\" width=\"200\" height=\"200\" /></a>\n";
echo "<a href=\"".$pics[6]."\" /><img src=\"".$pics[6]."\" cellpadding=\"0\" cellspacing=\"0\" width=\"200\" height=\"200\" /></a>\n";
echo "<a href=\"".$pics[7]."\" /><img src=\"".$pics[7]."\" cellpadding=\"0\" cellspacing=\"0\" width=\"200\" height=\"200\" /></a>\n";
?>

 

Ray

 

Thanks Ray.

You are the Best  :D

 

Really, i never thought of array  >:(

 

Thank you Once again, works Great !

Link to comment
https://forums.phpfreaks.com/topic/117196-click-on-image/#findComment-602896
Share on other sites

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.