Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Thumbnail an Image, DO NOT SAVE, load in browser temporarily, discard when page closes

Status
Not open for further replies.
Jul 28, 2011
167
0
0
NG
Ok, I know this looks somewhat awkward. However, it is a thought that came to me and I've been thinking of its possibilities and implementation.

I have about 40 images stored in my images folder. I want three(3) random thumbnail of the images to be generated each time a page loads. I don't want to have to store thumbnails in any folder; I just want the thumbnails to be generated on the fly and displayed temporarily

Is this kinda thing possible?

Many thanks in advance.
 
it is possible.
but unadvisable as you are incurring processor cycles every impression that are entirely unnecessary.

1. store the thumbnails in a cache.
2. store the filenames of the thumbnails somewhere too; or grab them dynamically into an array
3. randomise the array
4. store a session
5. each impression show the images you want and record the key of the last image displayed.
6. rotate when you run out of images

Code:
function getrandthumbnails(){
 $tDir = 'path/to/thumbnails/';
 $images = glob( $tDir . '*.jpg');
 shuffle($images);
 $_SESSION['imageRotation'] = $images;
 $_SESSION['lastImage'] = -1;
}

if(session_id == '') session_start();
if(!isset($_SESSION['imageRotation'])) getrandthumbnails();

for($i=0; $i<3; $i++):
 if($_SESSION['lastImage'] >= count($_SESSION['imageRotation'])) $_SESSION['lastImage'] = -1;
 echo '<img src="'. $_SESSION['imageRotation'][$_SESSION['lastImage'] + 1 ] . '"/>';
 $_SESSION['lastImage']++;
endfor;

caution: not tested ...
 
@jpadie: Thanks.

However, my major challenge is really how to thumbnail on the fly (as soon as the page loads) without saving the thumbnail in any folder on my site...maybe as you have said..."cache it
 
cacheing a thumbnail is the precise opposite of 'without saving the thumbnail in any folder on my site'. Doing the latter is loony.

however, if you are hell-bent on that route then there is no difference between creating a thumbnail on the fly to doing so otherwise. Obviously you will need either to redirect requests to image files to your php handler script; or to replace the links with the script name.

so instead of
<img src="myImage.jpg" />
you would have
<img src="myPHPImageServer.php?imageID=someuniqueurlencodedidentifier" />

your receiving script identifies the image from the imageID query parameter, grabs it, resamples it and spews it out with the correct headers. this thread has some sample code that may be helpful. it adds watermarks and cacheing too so you will need to delete those parts. however there are many thumbnail creation scripts in the php.net online manual that will serve you just as well (but doubtless you have already read these before posting here).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top