]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/img.pm
Merge branch 'ready/document-success-reason'
[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         my $ispdf = $base=~s/\.pdf$/.png/i;
69         my $pagenumber = exists($params{pagenumber}) ? int($params{pagenumber}) : 0;
70         if ($pagenumber != 0) {
71                 $base = "p$pagenumber-$base";
72         }
73
74         eval q{use Image::Magick};
75         error gettext("Image::Magick is not installed") if $@;
76         my $im = Image::Magick->new();
77         my $imglink;
78         my $imgdatalink;
79         my $r = $im->Read("$srcfile\[$pagenumber]");
80         error sprintf(gettext("failed to read %s: %s"), $file, $r) if $r;
81         
82         my ($dwidth, $dheight);
83
84         if ($params{size} eq 'full') {
85                 $dwidth = $im->Get("width");
86                 $dheight = $im->Get("height");
87         } else {
88                 my ($w, $h) = ($params{size} =~ /^(\d*)x(\d*)$/);
89                 error sprintf(gettext('wrong size format "%s" (should be WxH)'), $params{size})
90                         unless (defined $w && defined $h &&
91                                 (length $w || length $h));
92
93                 if ($im->Get("width") == 0 || $im->Get("height") == 0) {
94                         ($dwidth, $dheight)=(0, 0);
95                 } elsif (! length $w || (length $h && $im->Get("height")*$w > $h * $im->Get("width"))) {
96                         # using height because only height is given or ...
97                         # because original image is more portrait than $w/$h
98                         # ... slimness of $im > $h/w
99                         # ... $im->Get("height")/$im->Get("width") > $h/$w
100                         # ... $im->Get("height")*$w > $h * $im->Get("width")
101
102                         $dheight=$h;
103                         $dwidth=$h / $im->Get("height") * $im->Get("width");
104                 } else { # (! length $h) or $w is what determines the resized size
105                         $dwidth=$w;
106                         $dheight=$w / $im->Get("width") * $im->Get("height");
107                 }
108         }
109
110         if ($dwidth < $im->Get("width") || $ispdf) {
111                 # resize down, or resize to pixels at all
112
113                 my $outfile = "$config{destdir}/$dir/$params{size}-$base";
114                 $imglink = "$dir/$params{size}-$base";
115
116                 will_render($params{page}, $imglink);
117
118                 if (-e $outfile && (-M $srcfile >= -M $outfile)) {
119                         $im = Image::Magick->new;
120                         $r = $im->Read($outfile);
121                         error sprintf(gettext("failed to read %s: %s"), $outfile, $r) if $r;
122                 }
123                 else {
124                         $r = $im->Resize(geometry => "${dwidth}x${dheight}");
125                         error sprintf(gettext("failed to resize: %s"), $r) if $r;
126
127                         $im->set(($issvg || $ispdf) ? (magick => 'png') : ());
128                         my @blob = $im->ImageToBlob();
129                         # don't actually write resized file in preview mode;
130                         # rely on width and height settings
131                         if (! $params{preview}) {
132                                 writefile($imglink, $config{destdir}, $blob[0], 1);
133                         }
134                         else {
135                                 eval q{use MIME::Base64};
136                                 error($@) if $@;
137                                 $imgdatalink = "data:image/".$im->Get("magick").";base64,".encode_base64($blob[0]);
138                         }
139                 }
140
141                 # always get the true size of the resized image (it could be
142                 # that imagemagick did its calculations differently)
143                 $dwidth  = $im->Get("width");
144                 $dheight = $im->Get("height");
145         } else {
146                 $imglink = $file;
147         }
148         
149         if (! defined($dwidth) || ! defined($dheight)) {
150                 error sprintf(gettext("failed to determine size of image %s"), $file)
151         }
152
153         my ($fileurl, $imgurl);
154         my $urltobase = $params{preview} ? undef : $params{destpage};
155         $fileurl=urlto($file, $urltobase);
156         $imgurl=$imgdatalink ? $imgdatalink : urlto($imglink, $urltobase);
157
158         if (! exists $params{class}) {
159                 $params{class}="img";
160         }
161
162         my $attrs='';
163         foreach my $attr (qw{alt title class id hspace vspace}) {
164                 if (exists $params{$attr}) {
165                         $attrs.=" $attr=\"$params{$attr}\"";
166                 }
167         }
168         
169         my $imgtag='<img src="'.$imgurl.
170                 '" width="'.$dwidth.
171                 '" height="'.$dheight.'"'.
172                 $attrs.
173                 (exists $params{align} && ! exists $params{caption} ? ' align="'.$params{align}.'"' : '').
174                 ' />';
175
176         my $link;
177         if (! defined $params{link}) {
178                 $link=$fileurl;
179         }
180         elsif ($params{link} =~ /^\w+:\/\//) {
181                 $link=$params{link};
182         }
183
184         if (defined $link) {
185                 $imgtag='<a href="'.$link.'">'.$imgtag.'</a>';
186         }
187         else {
188                 my $b = bestlink($params{page}, $params{link});
189         
190                 if (length $b) {
191                         add_depends($params{page}, $b, deptype("presence"));
192                         $imgtag=htmllink($params{page}, $params{destpage},
193                                 $params{link}, linktext => $imgtag,
194                                 noimageinline => 1,
195                         );
196                 }
197         }
198
199         if (exists $params{caption}) {
200                 return '<table class="img'.
201                         (exists $params{align} ? " align-$params{align}" : "").
202                         '">'.
203                         '<caption>'.$params{caption}.'</caption>'.
204                         '<tr><td>'.$imgtag.'</td></tr>'.
205                         '</table>';
206         }
207         else {
208                 return $imgtag;
209         }
210 }
211
212 1