]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/img.pm
Merge branch 'filter-full'
[ikiwiki.git] / IkiWiki / Plugin / img.pm
1 #!/usr/bin/perl
2 # Ikiwiki enhanced image handling plugin
3 # Christian Mock cm@tahina.priv.at 20061002
4 package IkiWiki::Plugin::img;
5
6 use warnings;
7 use strict;
8 use IkiWiki 3.00;
9
10 my %imgdefaults;
11
12 sub import {
13         hook(type => "getsetup", id => "img", call => \&getsetup);
14         hook(type => "preprocess", id => "img", call => \&preprocess, scan => 1);
15 }
16
17 sub getsetup () {
18         return
19                 plugin => {
20                         safe => 1,
21                         rebuild => undef,
22                         section => "widget",
23                 },
24 }
25
26 sub preprocess (@) {
27         my ($image) = $_[0] =~ /$config{wiki_file_regexp}/; # untaint
28         my %params=@_;
29
30         if (! defined $image) {
31                 error("bad image filename");
32         }
33
34         if (exists $imgdefaults{$params{page}}) {
35                 foreach my $key (keys %{$imgdefaults{$params{page}}}) {
36                         if (! exists $params{$key}) {
37                                 $params{$key}=$imgdefaults{$params{page}}->{$key};
38                         }
39                 }
40         }
41
42         if (! exists $params{size} || ! length $params{size}) {
43                 $params{size}='full';
44         }
45
46         if ($image eq 'defaults') {
47                 $imgdefaults{$params{page}} = \%params;
48                 return '';
49         }
50
51         add_link($params{page}, $image);
52         add_depends($params{page}, $image);
53
54         # optimisation: detect scan mode, and avoid generating the image
55         if (! defined wantarray) {
56                 return;
57         }
58
59         my $file = bestlink($params{page}, $image);
60         my $srcfile = srcfile($file, 1);
61         if (! length $file || ! defined $srcfile) {
62                 return htmllink($params{page}, $params{destpage}, $image);
63         }
64
65         my $dir = $params{page};
66         my $base = IkiWiki::basename($file);
67
68         eval q{use Image::Magick};
69         error gettext("Image::Magick is not installed") if $@;
70         my $im = Image::Magick->new;
71         my $imglink;
72         my $r = $im->Read($srcfile);
73         error sprintf(gettext("failed to read %s: %s"), $file, $r) if $r;
74         
75         my ($dwidth, $dheight);
76
77         if ($params{size} ne 'full') {
78                 my ($w, $h) = ($params{size} =~ /^(\d*)x(\d*)$/);
79                 error sprintf(gettext('wrong size format "%s" (should be WxH)'), $params{size})
80                         unless (defined $w && defined $h &&
81                                 (length $w || length $h));
82                 
83                 if ((length $w && $w > $im->Get("width")) ||
84                     (length $h && $h > $im->Get("height"))) {
85                         # resizing larger
86                         $imglink = $file;
87
88                         # don't generate larger image, just set display size
89                         if (length $w && length $h) {
90                                 ($dwidth, $dheight)=($w, $h);
91                         }
92                         # avoid division by zero on 0x0 image
93                         elsif ($im->Get("width") == 0 || $im->Get("height") == 0) {
94                                 ($dwidth, $dheight)=(0, 0);
95                         }
96                         # calculate unspecified size from the other one, preserving
97                         # aspect ratio
98                         elsif (length $w) {
99                                 $dwidth=$w;
100                                 $dheight=$w / $im->Get("width") * $im->Get("height");
101                         }
102                         elsif (length $h) {
103                                 $dheight=$h;
104                                 $dwidth=$h / $im->Get("height") * $im->Get("width");
105                         }
106                 }
107                 else {
108                         # resizing smaller
109                         my $outfile = "$config{destdir}/$dir/${w}x${h}-$base";
110                         $imglink = "$dir/${w}x${h}-$base";
111                 
112                         will_render($params{page}, $imglink);
113
114                         if (-e $outfile && (-M $srcfile >= -M $outfile)) {
115                                 $im = Image::Magick->new;
116                                 $r = $im->Read($outfile);
117                                 error sprintf(gettext("failed to read %s: %s"), $outfile, $r) if $r;
118                         }
119                         else {
120                                 ($dwidth, $dheight)=($w, $h);
121                                 $r = $im->Resize(geometry => "${w}x${h}");
122                                 error sprintf(gettext("failed to resize: %s"), $r) if $r;
123
124                                 # don't actually write resized file in preview mode;
125                                 # rely on width and height settings
126                                 if (! $params{preview}) {
127                                         my @blob = $im->ImageToBlob();
128                                         writefile($imglink, $config{destdir}, $blob[0], 1);
129                                 }
130                                 else {
131                                         $imglink = $file;
132                                 }
133                         }
134                         
135                         $dwidth = $im->Get("width") unless defined $dwidth;
136                         $dheight = $im->Get("height") unless defined $dheight;
137                 }
138         }
139         else {
140                 $imglink = $file;
141                 $dwidth = $im->Get("width");
142                 $dheight = $im->Get("height");
143         }
144         
145         if (! defined($dwidth) || ! defined($dheight)) {
146                 error sprintf(gettext("failed to determine size of image %s"), $file)
147         }
148
149         my ($fileurl, $imgurl);
150         if (! $params{preview}) {
151                 $fileurl=urlto($file, $params{destpage});
152                 $imgurl=urlto($imglink, $params{destpage});
153         }
154         else {
155                 $fileurl="$config{url}/$file";
156                 $imgurl="$config{url}/$imglink";
157         }
158
159         if (exists $params{class}) {
160                 $params{class}.=" img";
161         }
162         else {
163                 $params{class}="img";
164         }
165
166         my $attrs='';
167         foreach my $attr (qw{alt title class id hspace vspace}) {
168                 if (exists $params{$attr}) {
169                         $attrs.=" $attr=\"$params{$attr}\"";
170                 }
171         }
172         
173         my $imgtag='<img src="'.$imgurl.
174                 '" width="'.$dwidth.
175                 '" height="'.$dheight.'"'.
176                 $attrs.
177                 (exists $params{align} && ! exists $params{caption} ? ' align="'.$params{align}.'"' : '').
178                 ' />';
179
180         my $link;
181         if (! defined $params{link}) {
182                 $link=$fileurl;
183         }
184         elsif ($params{link} =~ /^\w+:\/\//) {
185                 $link=$params{link};
186         }
187
188         if (defined $link) {
189                 $imgtag='<a href="'.$link.'">'.$imgtag.'</a>';
190         }
191         else {
192                 my $b = bestlink($params{page}, $params{link});
193         
194                 if (length $b) {
195                         add_depends($params{page}, $b, deptype("presence"));
196                         $imgtag=htmllink($params{page}, $params{destpage},
197                                 $params{link}, linktext => $imgtag,
198                                 noimageinline => 1,
199                         );
200                 }
201         }
202
203         if (exists $params{caption}) {
204                 return '<table class="img'.
205                         (exists $params{align} ? " align-$params{align}" : "").
206                         '">'.
207                         '<caption>'.$params{caption}.'</caption>'.
208                         '<tr><td>'.$imgtag.'</td></tr>'.
209                         '</table>';
210         }
211         else {
212                 return $imgtag;
213         }
214 }
215
216 1