]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - gallery/gallery.pm
a new branch for the gallery plugin
[git.ikiwiki.info.git] / gallery / gallery.pm
1 ##!/usr/bin/perl
2 # Creating a Gallery of bunch of images. 
3 # Arpit Jain arpitjain11@ozoneball.com
4 package IkiWiki::Plugin::gallery;
6 use warnings;
7 use strict;
8 use IkiWiki 2.00;
10 sub import { #{{{
11         hook(type => "preprocess", id => "gallery", call => \&preprocess);
12         hook(type => "pagetemplate", id => "gallery", call => \&pagetemplate);
13 } #}}}
15 sub pagetemplate(@){ #{{{
16         my %params=@_ ; 
17         my $template = $params{template};
18         my $page = $params{page}; 
19         my $scripts;
21         if($template->query(name => "gallery")){
22                 my $pagetmpl=template("gallery.tmpl", blind_cache => 1); 
23                 $pagetmpl->param(prototype1 => "prototype.js");
24                 $pagetmpl->param(scriptaculous => "scriptaculous.js?load=effects");
25                 $pagetmpl->param(lightbox => "lightbox.js");
26                 $pagetmpl->param(lightboxcss => "css/lightbox.css");
27                 $pagetmpl->param(baseurl => "$config{url}/");
28                 $scripts=$pagetmpl->output ; 
29         }
30         $template->param(gallery => $scripts);
31 } #}}}
33 sub preprocess (@) { #{{{
34         my %params=@_;
35         
36         my $alt = $params{alt} || '';
37         my $title = $params{title} || '';
38         my $imagedir = $params{imagedir} || '';
39         my $thumbnailsize= $params{thumbnailsize} || '200x200';
40         my $cols= $params{cols} || '3';
41         my $name = $params{name} || '1';
42         
43         my $dir = bestdir($params{page}, $imagedir) || return "[[gallery ".sprintf(gettext("Directory %s not found"), $imagedir)."]]"; 
45         my ($w, $h) = ($thumbnailsize =~ /^(\d+)x(\d+)$/);
46         return "[[gallery ".sprintf(gettext('bad thumbnail size "%s"'), $thumbnailsize)."]]" unless (defined $w && defined $h);
47         
48         opendir PICSDIR, srcfile($dir);
49         my @image_files = grep /\.(jpg|png|gif)$/, readdir PICSDIR;
50         closedir PICSDIR;
52         eval q{use Image::Magick};
53         error($@) if $@;
54         
55         my $numfiles = scalar(@image_files);    
56         my $numcols=0;  
58         my $tosend='<table align="center">';
59         if(length $title){
60         $tosend.="<tr><td align=\"center\" colspan=\"$cols\"><h2>$title</h2></td></tr>";
61         }
63         my ($imagefile,$im,$r);
65         foreach $imagefile (@image_files){              
66                 $im = Image::Magick->new;
67                 my $imagelink = "$dir/$imagefile"; #Source Image File
68                 my $thumblink = "$dir/${w}x${h}-$imagefile";  #Destination Thumbnail File
69                 my $thumboutlink= "$config{destdir}/$thumblink" ; #Destination Image File
70                 
71                 will_render($params{page}, $thumblink); 
72                 
73                 if (-e $thumboutlink && (-M srcfile($imagelink) >= -M $thumboutlink)) {##Do Not Create Thumbnail if already exists.
74                         $r = $im->Read($thumboutlink);
75                         return "[[gallery ".sprintf(gettext("failed to read %s: %s"), $thumboutlink, $r)."]]" if $r;
76                 } else {
77                         $r = $im->Read(srcfile($imagelink)); #Read Image File. 
78                         return "[[gallery".sprintf(gettext("Failed to read %s: %s"), $imagelink, $r)."]]" if $r;
79                         
80                         $r = $im->Resize(geometry => "${w}x${h}"); #Create Thumbnail
81                         return "[[gallery ".sprintf(gettext("Failed to resize: %s"), $r)."]]" if $r;
82         
83                         # Don't actually write file in preview mode
84                         if (! $params{preview}) {
85                                 my @blob = $im->ImageToBlob();
86                                 $thumblink=Ikiwiki::possibly_foolish_untaint($thumblink);
87                                 writefile($thumblink, $config{destdir}, $blob[0], 1);
88                         }else {
89                                         $thumblink = $imagelink;
90                         }
91                 }
92                 
93                 add_depends($params{page},$thumblink);  
94                 add_depends($params{page},$imagelink); 
95         
96                 my ($imageurl, $thumburl);
97                 if (! $params{preview}) {
98                         $imageurl=urlto($imagelink, $params{destpage});
99                         $thumburl=urlto($thumblink, $params{destpage});
100                 } else {
101                         $imageurl="$config{url}/$imagelink";
102                         $thumburl="$config{url}/$thumblink";
103                 }
104                 undef $im ; 
105                 if(!$numcols){
106                 $tosend.="<tr>";
107                 }
108                 if($name==1){
109                 $tosend.="<td align=\"center\" class=\"images\"><table><tr>";
110                 }
111                 $tosend.= "<td align=\"center\" class=\"images\"><a href=\"$imageurl\" title=\"$imagefile\" rel=\"lightbox[mypics]\"><img src=\"" .$thumburl."\"/></a></td>";
112                 if($name==1){
113                 $tosend.="</tr><tr><td align=\"center\">$imagefile</td></tr></table></td>";
114                 }
115                 $numcols++; 
116                 if($numcols==$cols) {
117                 $numcols=0; 
118                 $tosend .= "</tr>"; 
119                 }
120         }
121         $tosend.="</table>";
122         
123         return $tosend; 
124 } #}}}
126 sub bestdir ($$) { #{{{
127         my $page=shift;
128         my $link=shift;
129         my $cwd=$page;
131         if ($link=~s/^\/+//) {
132                 $cwd="";
134         }
135         do {
136                 my $l=$cwd;
137                 $l.="/" if length $l;
138                 $l.=$link;
139                 if (-d "$config{srcdir}/$l") {
140                         return $l;
141                 }
142         } while $cwd=~s!/?[^/]+$!!;
143         
144         if (length $config{userdir}) {
145                 my $l = "$config{userdir}/".lc($link);
147                 if (-d $l) {
148                         return $l;
149                 }
150         }
151         return "";
152 } #}}}