FAGZAL's blog

Channels : ALL    Perl 5  (2)   PET  (2)   Misc.  (1)   Perl-2-PHP  (1)  
Creating a TGP-like gallery using PET in 6 lines
2007, November 17., 0 h

A very typical web programming task is to take a directory which contains a bunch of images, and create a thumbnail gallery out of it. The Album module does this very professionally, and an actual industry - TGP sites - is built around this idea.

In theory, it is relatively easy to create an album. All you have to do is to list the directory, get the images, resize them and then display the thumbnails on a (HTML) page with links to the original images. So let's make this easey in practice, too!

Using a few PET built-ins, I am going to show you how to create an album without writing any Perl code - using only 6 lines of code. We will have a page list.pet which lists the thumbnails, and another one called show.pet, which shows the big (original) image.

list.pet
<io:getDir=images dir="html/images" exti="jpg|jpeg" />
[% FOREACH image = images %]
 <a href="/show.pet?file=images/${image.file}"><image:thumbnail 
   maxwidth=
"100" maxheight="100"  file=image.path border="0" /></a>
[% END %]


show.pet
<img src="/${Query.file}" />


That's it! Of course this has no extra HTML, so if you want to make it look nice, you have to put in some CSS & DIVs & TABLEs and whatnot, but this is all the codig you need.

Now let's go into the details!



First, in order to "run" the above code, you have to load PET::Util::Image, PET::Util::IO and set filter PET::Util::UtilXmlMap in global.conf.

UtilXmlMap will transfer the above code (of list.pet) to:

[% images = Util.io.getDir('dir' , 'html/images', 'exti', 'jpg|jpeg') %]
[% FOREACH image = images %]
 <a href="/show.pet?file=${image.file}">[% 
   Util.image.thumbnail('file',image.path,'maxwidth',100,
   'maxheight',100,'border','0') %]</a>
[% END %]


The first line calls PET::Util::IO->getDir('dir' => 'html/images', 'exti' => 'jpg|jpeg'), which returns an arrayref of hashrefs, each hash containing a file from directory "html/images". (The directory is relative to BASEPATH if it does not start with a slash.) The 'exti' parameter greps files which have jpg or jpeg extension.

The we iterate through this directory list using FOREACH, and call a thumbnail generator on them. PET::Util::Image->thumbnail(...) creates the thumbnail image (if it did not exists already), and returns an <img ... /> tag. (The 'border' parameter is unknown by the 'thumbnail' method, so it is simply passed through.) Note that image.path is a TT variable, so it cannot (must not!) be quoted.

We also link to show.pet, which displays the original image by simply accessing the 'file' query parameter.

Simple, isn't it?
 

Comments

Add a comment

If your want to add a comment to this entry, log in or register here if you have not already done so!


See GALLERY»

1. PET tricks
   2007, November 17., 0h
2. Creating a TGP-like gallery using PET in 6 lines
   2007, November 17., 0h

Archive
rss newsfeed icon
Advertisement

Title: Bekas Szoros2
Visited: 399
Uploaded at: 2003-07-01 18:56:30
Title: Kolozsvar - Mathias Rex
Visited: 263
Uploaded at: 2003-07-01 18:37:47
Title: Marosvasarhely Bolyai
Visited: 144
Uploaded at: 2003-07-01 18:33:11
Title: Bran Castle
Visited: 384
Uploaded at: 2003-07-01 19:12:21
Title: Bekas Szoros
Visited: 414
Uploaded at: 2003-07-01 18:55:29
Title: Egyesko
Visited: 291
Uploaded at: 2003-07-01 18:39:46
Title: Transylvania1
Visited: 131
Uploaded at: 2003-07-01 18:29:14
Title: Bekas Szoros Kereszt
Visited: 422
Uploaded at: 2003-07-01 18:57:18
Title: Marosvasarhely Kulturpalota
Visited: 93
Uploaded at: 2003-07-01 18:32:12
Title: Transylvania2
Visited: 144
Uploaded at: 2003-07-01 18:30:41
Title: Kolozsvar Farkas utcai templom
Visited: 250
Uploaded at: 2003-07-01 18:35:32
Title: Transylvania3
Visited: 362
Uploaded at: 2003-07-01 18:54:02