...

View Full Version : Random image from url parameter?



htcilt
03-25-2010, 06:49 PM
I'm generating a random image using an array e.g.


$pics = array(
'image1',
'image2',
'image3'
);

$randompic = $pics[array_rand($pics)];

echo "<img src=\"images/".$randompic.".jpg\" />";

On the page that displays the image (lets call it index.php) I echo different content based on the parameter in the URL e.g.
index.php?cat=group1

I now need to do the same for the random image generation.
I can use $_GET in randomimage.php to select a random image based on the group e.g.



$category = $_GET['cat'];

$group1 = array(
'image1',
'image2',
'image3'
);

$group2 = array(
'image4',
'image5',
'image6'
);

if ($category=='group1'){
$randompic = $group1[array_rand($group1)];}
elseif ($category=='group2'){
$randompic = $group2[array_rand($group2)];}

echo "<img src=\"images/".$randompic."\" />";



However, I dont know how to do the echo on index.php rather than randomimage.php.

I tried

echo "<img src=\"randompic.php?cat=group1\" />";

but looking at the html, it shows the above as a literal string.

I'm guessing I need to set the header on randomimage.php to header("Content-type: image/JPEG"); to just display the image with no html?

DJCMBear
03-25-2010, 07:07 PM
have you tryed including the image so that it acts as the real image even tho its not.

example:


$pics = array(
'image1',
'image2',
'image3'
);
$randompic = $pics[array_rand($pics)];
include($randompic.'.jpg');


you will need to use the full path such as ../images/$randompic.jpg

htcilt
03-25-2010, 07:34 PM
I'll give that a go and post back shortly :)

met
03-25-2010, 08:52 PM
function. create a file, say "includes.php". include this page on every page.



<?php
function randomImage($category) {

$safe= array('group1', 'group2', 'group3', 'group4'); /*add each "gallery here */

if(in_array($category,$safe)) {
/*if requested image is in the array */
$group1 = array(
'image1',
'image2',
'image3'
);

$group2 = array(
'image4',
'image5',
'image6'
);

if ($category=='group1'){
$randompic = $group1[array_rand($group1)];
}
elseif ($category=='group2'){
$randompic = $group2[array_rand($group2)];
}

return "<img src=\"images/".$randompic."\" />";

} else {
return 'Invalid URL specified.'; // default content if the $category doesn't exist
}
}


/* use like this */
//url=index.php?category=group1
echo randomImage($_GET['category']);
//returns <img src="images/image4" /> from $group1



don't forget you need a file extension.

htcilt
03-25-2010, 09:16 PM
Thanks Met, that approach works perfectly :)

mlseim
03-26-2010, 04:20 PM
For anyone that wants to read any directory and display random images ....



<?php

// directory name where they are stored.
$dir_name="photos";

// put these types of images into an array.
$images = array_merge(
glob("$dir_name/*.jpg"),
glob("$dir_name/*.png"),
glob("$dir_name/*.gif")
);

// Randomize the array.
shuffle($images);

// Display the first 5 random images.
echo"
<img src='$images[0]' /><br />
<img src='$images[1]' /><br />
<img src='$images[2]' /><br />
<img src='$images[3]' /><br />
<img src='$images[4]' /><br />
";

// If you would rather display all of them.
//foreach($images as &$img){
//echo "<img src='$img' /><br />";
//}

htcilt
03-27-2010, 11:33 AM
Also a nice solution. Thank you :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum