LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 01-03-2013, 12:45 AM   #1
ziphem
Member
 
Registered: Feb 2004
Location: US / EU
Distribution: Fedora 20
Posts: 129

Rep: Reputation: 18
Resizing dynamically listed files on the fly, PHP


I can use PHP to dynamically show all images in a directory. These files, which are added every 1-10 minutes (security camera) are really big, and I need to view thumbnails when viewing directory contents, otherwise it'll take forever to load and will not be manageable. So, what I do, is I have a cron script run every 15 minutes to create thumbnails those larger images, I use the PHP script to list all the thumbnails, and create a hyperlink for each thuumbnail to the relevant larger image.

I'd prefer to skip the whole cron step, and resize every image on the fly (I'm the only one viewing the photos, once or twice a day, so not much server load).

I do have a script to resize any single image on the fly.

However, I can't seem to figure out how to merge this single image resize script with the dynamic file listing script (thus creating a dynamic resize of all images in the directory on the fly).

Since I'm trying to find a way to dynamically resize, I've included just my general directory listing script (not thumbnail to larger image link file), and the single image resize script. I greatly appreciate any and all help or tips anyone can give me on how to combine these two. Thanks a ton!!!

The two scripts are:

<?php

$dirdriveway_originals = "./";
$fddriveway_originals = scandir("$dirdriveway_originals");

echo "<br>";

sort($fddriveway_originals);

foreach ($fddriveway_originals as $driveway_file) {
if (substr($driveway_file, -4) == ".jpg") {
echo "<img src=\"$dirdriveway_originals$driveway_file\" border=\"0\" alt=\"$driveway_file\"> ";
}
}

?>

-----------------------------------------------

<?php
$photo="sunflower.jpg";

$cmd = "convert $photo -thumbnail 200x200 ".
" -unsharp 0.2x0.6+1.0 -quality 100 JPG:-";

header("Content-type: image/jpeg");
passthru($cmd, $retval);
?>
 
Old 01-03-2013, 03:59 AM   #2
eSelix
Senior Member
 
Registered: Oct 2009
Location: Wroclaw, Poland
Distribution: Arch, Kubuntu
Posts: 1,221

Rep: Reputation: 307Reputation: 307Reputation: 307Reputation: 307
The best way in my opinion is to use a mod_rewrite, something like
Code:
# .htaccess file in thumbnails directory
# -f check if thumbnail was already created, this requires unique filenames, you can ommit RewriteCond line if you don't want to cache thumbnails
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)\.jpg$ script_generating_thumbnail.php?photo=$1
Of course in .html file you pass thumbnail path to img tag.
If many files are in directory, then generating thumbnails may last too long, and client browser will disconnect and stop downloading images.

You can also use a php file as img src, for example
Code:
<img src="script_generating_thumbnail.php?photo=$dirdriveway_originals$driveway_file">
Browser will display this as image if proper header will be sent.

Last edited by eSelix; 01-03-2013 at 04:07 AM.
 
1 members found this post helpful.
Old 01-03-2013, 10:29 AM   #3
ziphem
Member
 
Registered: Feb 2004
Location: US / EU
Distribution: Fedora 20
Posts: 129

Original Poster
Rep: Reputation: 18
Thanks very much for the response. Why is mod rewrite better than plain php scripting? Also, what if I prefer not to use tmp files but also don't have (because I'd be using a new on-the-fly resize method) any thumbnails? There could be approximately 15 to 300 in each directory any given day.

How would I actually merge those two php scripts above - is that even possible? Thanks a ton!
 
Old 01-03-2013, 10:54 AM   #4
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,148

Rep: Reputation: 236Reputation: 236Reputation: 236
Hi

I think he meant using two scripts, one for the list and one for for generating a thumbnail. If you want it in one PHP file only, you will have to send the pictures base64 encoded using data protocol instead.
PHP Code:
foreach (glob("./*.jpg") as $photo) {
  
$cmd "convert ".escapeshellarg($photo)." -thumbnail 200x200 "
       
" -unsharp 0.2x0.6+1.0 -quality 100 JPG:-";
  
$data shell_exec($cmd);
  
$data base64_encode($data);
  echo 
"<img src=\"data:image/jpeg;base64,".$data."\"/>";
  echo 
"<br/>";

 
1 members found this post helpful.
Old 01-03-2013, 03:36 PM   #5
eSelix
Senior Member
 
Registered: Oct 2009
Location: Wroclaw, Poland
Distribution: Arch, Kubuntu
Posts: 1,221

Rep: Reputation: 307Reputation: 307Reputation: 307Reputation: 307
Quote:
Originally Posted by ziphem View Post
Why is mod rewrite better than plain php scripting?
Only simplify with caching. If you would prefer not to cache thumbnails than there are no benefits.
 
Old 01-03-2013, 10:45 PM   #6
ziphem
Member
 
Registered: Feb 2004
Location: US / EU
Distribution: Fedora 20
Posts: 129

Original Poster
Rep: Reputation: 18
Thanks a ton for the help, I really appreciate it!! I got the script to work perfectly. I linked the to the original image $photo as echo "<a href=\"$photo\"><img src=\"data:image/jpeg;base64,".$data."\"/></a> ";

Just one question which had been mentioned, and that is timeouts. I noticed that this takes a while to load each image. Will this ever time out, or will it continue until it finishes loading all the images? I usually have about 10-50 images, but sometimes it's several hundred.

Thanks!!
 
Old 01-04-2013, 03:52 AM   #7
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,148

Rep: Reputation: 236Reputation: 236Reputation: 236
I doubt you'll get timeouts. PHP has a limit of 30 seconds by default, but time spent running external commands doesn't count. The convert command is the thing taking almost all of the processing time.

If you do worry about it, you can put this in the beginning of the script. Then there will be no time limit.

PHP Code:
set_time_limit(0); 
 
1 members found this post helpful.
Old 01-04-2013, 04:01 AM   #8
eSelix
Senior Member
 
Registered: Oct 2009
Location: Wroclaw, Poland
Distribution: Arch, Kubuntu
Posts: 1,221

Rep: Reputation: 307Reputation: 307Reputation: 307Reputation: 307
There is also client browser timeouts. I sometimes see this behavior, that browser stop downloading images after waiting too long. I don't known specific time, but this is about 60 sec.
 
1 members found this post helpful.
Old 01-04-2013, 04:07 AM   #9
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,148

Rep: Reputation: 236Reputation: 236Reputation: 236
True. But that only happens if the browser receives no data. In this situation it means it will timeout only if one convert command takes more than 60 seconds. That's not very likely.
 
1 members found this post helpful.
Old 01-04-2013, 06:53 PM   #10
ziphem
Member
 
Registered: Feb 2004
Location: US / EU
Distribution: Fedora 20
Posts: 129

Original Poster
Rep: Reputation: 18
Ok that's great to know that I shouldn't have a timeout issue.

Everyone has been so incredibly helpful (really, not just nice, but super helpful), I appreciate it very, very much.

I have one opinion question before I mark this thread as solved. What are your thoughts about resizing and displaying anywhere from 25 to 250 files in a folder with no cache, with anywhere from one to 3 reloads (i.e., visits) a day, vs using cache? Once the day passes, the folder where the images are stored is essentially archived and almost never reviewed. Does it really provide me much benefit to do the cache method or do you think the on-the-fly method is sufficient? Does caching provide any performance gains, and if so, are they more than minimal - particularly considering how infrequently these images will be viewed?

Certainly on the fly or cache files is better than what I've done up to this point, which is to cron create the originals and thumbs folder, and every 15 minutes, resize all originals as thumbs (as copies), and through the webpage display the thumbs with a link to the originals. I mean that either the cache or on the fly conversion is better from an organizational standpoint; under the method I've used up to this point, each day has 2 subdirectories of 2 directories; /garage/thumbs and /garage/originals, and /driveway/thumbs and /driveway/originals; if I want to delete files, I have to flush out from each directory. I also have a convoluted directory-creating cron file and the persistent 15 minute resizing.

Thanks again everyone for your input and help on this thread!!

Last edited by ziphem; 01-04-2013 at 06:55 PM.
 
Old 01-05-2013, 02:25 PM   #11
eSelix
Senior Member
 
Registered: Oct 2009
Location: Wroclaw, Poland
Distribution: Arch, Kubuntu
Posts: 1,221

Rep: Reputation: 307Reputation: 307Reputation: 307Reputation: 307
Personaly I prefer caching for example in ramdisk and delete old thumbs after a day, but I think that your on the fly without cache method will be sufficient in conditions you presented.
 
Old 01-05-2013, 06:18 PM   #12
ziphem
Member
 
Registered: Feb 2004
Location: US / EU
Distribution: Fedora 20
Posts: 129

Original Poster
Rep: Reputation: 18
Ok thanks for your affirmation on that. Again, I appreciate everyone's help!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to delete files keeping the files listed in a text file -urgent jeesun Linux - General 4 10-21-2011 11:28 AM
PHP - Dynamically listing pictures - but only a certain # per page? ziphem Programming 1 06-24-2007 05:52 AM
LXer: Generate Flash movies on the fly with PHP LXer Syndicated Linux News 0 12-23-2006 01:54 AM
"How to Generate PHP files Dynamically" manikantha Programming 5 09-26-2004 12:00 PM
php dynamically create transparent png j-ray Programming 2 08-29-2004 08:41 AM


All times are GMT -5. The time now is 09:02 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration