in reply to Memory issue while experimenting with Tk::Thumbnail
So I would say you have a basic design problem. From my quick look, I would say you need to decide how many thunbs you want to display as a maximum. Then change your thumbnail generation method, and maybe "page them".{ my (@imagelist); foreach ( 0 .. $#filelist ) { print $filelist[$_], "\n"; my $temp = $mw->Photo( -file => $filelist[$_] ); push( @imagelist, $temp ); } eval { my $thumb = $mw->Thumbnail( -images => [@imagelist], -ilabels => 1 )->pack; }; warn($@) if ($@); }
With Photo objects, you need to create a set of them once, and REUSE them. You can reuse a photo object with
So you could create a hash of %thumbs of say 100 keys, and that is what you display on each page....a 10 x 10 array of thumbs. And have a pager mechanism, to display as many pages as you need. But you don't fill in those subsequent pages, what you do is reuse the %thumbs and just reconfigure them with the next batch of thumbs.$thumbs{0}->blank; $thumbs{0}->read($info{'admin'}{'thumbnail'});
Also when you create the thumbs, just make 1 "big-photo object" and reuse it in a loop, to make the thumb-objects. Then push the smaller "thumb-objects" into the @imagelist, instead of the huge full-photo-objects.
Here is a sample bit of code which shows how to make thumbnails in a loop, with just a single imager object. Now, I'm actually creating the thumbnails in the thumbs dir, but you could just push them into a hash. However....you will still reach memory limits if you try to push too many thumbs into a hash. So you must carefully think about juggling and reusing everything, so as to keep your mem use as low as possible. There are many ways to do this, so you have to find the modules which you like working with, and figure out how to use them in unison, without wasting objects.
sub make_thumbs{ my @pics = <pics/*.jpg pics/*.gif pic/*.png>; my $imager = Imager->new(); foreach my $pic (@pics){ my ($basename,$path,$suffix) = fileparse($pic,@exts); $info{$basename}{'key'} = $basename; $info{$basename}{'name'} = $basename; $info{$basename}{'pic'} = "pics/$basename.jpg"; #convert to jpg $info{$basename}{'thumbnail'} = "thumbs/$basename.jpg"; $imager->open(file=>$pic) or die $imager->errstr(); # Create smaller version my $thumb = $imager->scale(xpixels=>100); print "Storing thumbnail as: thumbs/$basename.jpg\n"; $thumb->write(file=>"thumbs/$basename.jpg", jpegquality=>30) or die $thumb->errstr; #Create resized picture my $resizer = $imager->scale(ypixels=>400); print "Storing resize as: pics/$basename.jpg\n"; $resizer->write(file=>"pics/$basename.jpg", jpegquality=>90) or die $resizer->errstr; } undef $imager; print "\n#################endofthumbcreation########################## +#####\n"; }
|
|---|