RE: [PHP] Randomly rotate image on page load? File under: Image Manipulation
| From: | Jay Blanchard | Date: | Mon, 10 Jul 2006 12:32:11 +0000 |
| Subject: | RE: [PHP] Randomly rotate image on page load? File under: Image Manipulation | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-239249@lists.php.net to get a copy of this message | ||
[snip]
If you actually want to load a different image each time, start with
php.net/rand Perhaps the image names could be integer based, or you
could
oad the image paths into an array and use rand to select based on a
numeric
key. Or you could store the image paths in a mysql database and use
mysql's
RAND to select an image path for display.
The possibilities are endless, but seeing this is a php list I won't
mention
J...s....t :-)
[/snip]
Script That Shall Not Be Named!
Selecting a random image from a directory using only PHP is not
tremendously difficult;
/*random picture picker*/
define ("ALBUM", "the/place/where/pictures/are/kept");
$albumOpen = opendir(ALBUM);
while(FALSE !== ($pic = readdir($albumOpen))){
$picParts = explode(".", $pic);
if("jpg" === $picParts[1]){
$arrDisplayPic[] = $pic;
}
}
closedir($albumOpen);
$randImgIndex = rand(0, (count($arrDisplayPic)-1));
/* test for the image size and reformat if needed,
* in this case one 350 px wide
*/
$imgInfo = getimagesize(ALBUM.$arrDisplayPic[$randImgIndex]);
if("350" <= $imgInfo[0]){
echo "<img src=\"" . ALBUM.$arrDisplayPic[$randImgIndex] . "\"
width=\"350\" align=\"right\" hspace=\"10\"
vspace=\"10\">";
} else {
echo "<img src=\"" . ALBUM.$arrDisplayPic[$randImgIndex] . "\"
align=\"right\" hspace=\"10\" vspace=\"10\">";
}