zhanna Posted July 29, 2008 Share Posted July 29, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/117196-click-on-image/ Share on other sites More sharing options...
.josh Posted July 29, 2008 Share Posted July 29, 2008 are you wanting the full sized image to be on a page all by itself? Just wrap a url tag around it with it as the target. Are you wanting some kind of popup thing? You're gonna have to use javascript for that. Quote Link to comment https://forums.phpfreaks.com/topic/117196-click-on-image/#findComment-602832 Share on other sites More sharing options...
craygo Posted July 29, 2008 Share Posted July 29, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/117196-click-on-image/#findComment-602838 Share on other sites More sharing options...
zhanna Posted July 29, 2008 Author Share Posted July 29, 2008 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 ? Quote Link to comment https://forums.phpfreaks.com/topic/117196-click-on-image/#findComment-602863 Share on other sites More sharing options...
craygo Posted July 29, 2008 Share Posted July 29, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/117196-click-on-image/#findComment-602885 Share on other sites More sharing options...
zhanna Posted July 29, 2008 Author Share Posted July 29, 2008 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 Really, i never thought of array Thank you Once again, works Great ! Quote Link to comment https://forums.phpfreaks.com/topic/117196-click-on-image/#findComment-602896 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.