I want to make something that will load a random image from a directory, NOT an array (it will be updated VERY frequently and it will be tedious to update the array every day and have it be 100+ entries in a matter of weeks). However, with the same reason, I don't want to go around making thumbnails and loading those, so it needs to just go ahead and load the images in a resized thumbnail, with a link to the full version. Now I dont know much javascript, but I would think this to be an easy task.
Maybe you could declare a variable that reads directory "/images/screenshots/" and counts the files or something and rolls random(1-$number) and pulls that number image, and assigns it to $image. Then you could just have it output <a href="/images/screenshots/$image" target="_blank"><img src="/images/screenshots/$image" height="240px" width="300px"; /></a>??
If anyone thinks they can do something like this, please let me know, maybe give me a small starter script to help me along. Thanks.
I am not sure if this is what you need and it is not HTML, PHP.
| Code: |
<?php
$dir= $_SERVER['DOCUMENT_ROOT'] . "/imageFolder"; // path to folder
$openDir = opendir($dir);
while (false !== ($fileNames = readdir($openDir))) {
if($fileNames == "." || $fileNames == "..") {
continue;
} else {
$ff[] = $fileNames;
}
}
shuffle($ff); //rand all files in image folder
$img = @$_SERVER['DOC_ROOT'] . "/imageFolder/" . $ff[0];
echo "<img src='$img' width='200' height='300' border='0' >"; // images must have same dimension - print first one from randomized array
?> |
This will automatically show the image that you upload to the folder, and you can change the dimensions.. I found it very handy.
I hope this helps.
Thank you, this is exactly what I need. I modified it a little to include a link to the resized image. This saves a lot of work. I don't know why people resize images and save them in another folder to do an array of with javascript when they can do stuff like this.
No problem, I never made it, I forgot who did, but whoever did it saved me a lot of time too.
| Quote: |
| No problem, I never made it, I forgot who did, but whoever did it saved me a lot of time too. |
I!
| Quote: |
| Thank you, this is exactly what I need. I modified it a little to include a link to the resized image. This saves a lot of work. I don't know why people resize images and save them in another folder to do an array of with javascript when they can do stuff like this. |
This script is working good if you have optimized images. If you put 2 MB images then you will wait long time to see thumbs. In other case if you save thumbs you will get faster page load. Now, you have two solutions.
Sonam
Ya, my images are all resampled to reduce the file size down to 600kb or so, none over a meg. (no quality reduction by the eye either). Infranview works miracles on file sizes, i swear, I've sized things up over 200% without pixelizing it. And it can batch that! But since you appear to be the original author, I'd just like to say thanks and that it rocks lol.
Cool i was looking for this 