]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/img.pm
link to the readme
[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         my $issvg = $base=~s/\.svg$/.png/i;
68
69         eval q{use Image::Magick};
70         error gettext("Image::Magick is not installed") if $@;
71         my $im = Image::Magick->new($issvg ? (magick => "png") : ());
72         my $imglink;
73         my $r = $im->Read($srcfile);
74         error sprintf(gettext("failed to read %s: %s"), $file, $r) if $r;
75         
76         my ($dwidth, $dheight);
77
78         if ($params{size} ne 'full') {
79                 my ($w, $h) = ($params{size} =~ /^(\d*)x(\d*)$/);
80                 error sprintf(gettext('wrong size format "%s" (should be WxH)'), $params{size})
81                         unless (defined $w && defined $h &&
82                                 (length $w || length $h));
83                 
84                 if ((length $w && $w > $im->Get("width")) ||
85                     (length $h && $h > $im->Get("height"))) {
86                         # resizing larger
87                         $imglink = $file;
88
89                         # don't generate larger image, just set display size
90                         if (length $w && length $h) {
91                                 ($dwidth, $dheight)=($w, $h);
92                         }
93                         # avoid division by zero on 0x0 image
94                         elsif ($im->Get("width") == 0 || $im->Get("height") == 0) {
95                                 ($dwidth, $dheight)=(0, 0);
96                         }
97                         # calculate unspecified size from the other one, preserving
98                         # aspect ratio
99                         elsif (length $w) {
100                                 $dwidth=$w;
101                                 $dheight=$w / $im->Get("width") * $im->Get("height");
102                         }
103                         elsif (length $h) {
104                                 $dheight=$h;
105                                 $dwidth=$h / $im->Get("height") * $im->Get("width");
106                         }
107                 }
108                 else {
109                         # resizing smaller
110                         my $outfile = "$config{destdir}/$dir/${w}x${h}-$base";
111                         $imglink = "$dir/${w}x${h}-$base";
112                 
113                         will_render($params{page}, $imglink);
114
115                         if (-e $outfile && (-M $srcfile >= -M $outfile)) {
116                                 $im = Image::Magick->new;
117                                 $r = $im->Read($outfile);
118                                 error sprintf(gettext("failed to read %s: %s"), $outfile, $r) if $r;
119                         }
120                         else {
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                         # always get the true size of the resized image
136                         $dwidth  = $im->Get("width"); 
137                         $dheight = $im->Get("height");
138                 }
139         }
140         else {
141                 $imglink = $file;
142                 $dwidth = $im->Get("width");
143                 $dheight = $im->Get("height");
144         }
145         
146         if (! defined($dwidth) || ! defined($dheight)) {
147                 error sprintf(gettext("failed to determine size of image %s"), $file)
148         }
149
150         my ($fileurl, $imgurl);
151         if (! $params{preview}) {
152                 $fileurl=urlto($file, $params{destpage});
153                 $imgurl=urlto($imglink, $params{destpage});
154         }
155         else {
156                 $fileurl=urlto($file);
157                 $imgurl=urlto($imglink);
158         }
159
160         if (! exists $params{class}) {
161                 $params{class}="img";
162         }
163
164         my $attrs='';
165         foreach my $attr (qw{alt title class id hspace vspace}) {
166                 if (exists $params{$attr}) {
167                         $attrs.=" $attr=\"$params{$attr}\"";
168                 }
169         }
170         
171         my $imgtag='<img src="'.$imgurl.
172                 '" width="'.$dwidth.
173                 '" height="'.$dheight.'"'.
174                 $attrs.
175                 (exists $params{align} && ! exists $params{caption} ? ' align="'.$params{align}.'"' : '').
176                 ' />';
177
178         my $link;
179         if (! defined $params{link}) {
180                 $link=$fileurl;
181         }
182         elsif ($params{link} =~ /^\w+:\/\//) {
183                 $link=$params{link};
184         }
185
186         if (defined $link) {
187                 $imgtag='<a href="'.$link.'">'.$imgtag.'</a>';
188         }
189         else {
190                 my $b = bestlink($params{page}, $params{link});
191         
192                 if (length $b) {
193                         add_depends($params{page}, $b, deptype("presence"));
194                         $imgtag=htmllink($params{page}, $params{destpage},
195                                 $params{link}, linktext => $imgtag,
196                                 noimageinline => 1,
197                         );
198                 }
199         }
200
201         if (exists $params{caption}) {
202                 return '<table class="img'.
203                         (exists $params{align} ? " align-$params{align}" : "").
204                         '">'.
205                         '<caption>'.$params{caption}.'</caption>'.
206                         '<tr><td>'.$imgtag.'</td></tr>'.
207                         '</table>';
208         }
209         else {
210                 return $imgtag;
211         }
212 }
213
214 1