]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/img.pm
img plugin: split resizing calculations and actual resizing
[git.ikiwiki.info.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;
6 use warnings;
7 use strict;
8 use IkiWiki 3.00;
10 my %imgdefaults;
12 sub import {
13         hook(type => "getsetup", id => "img", call => \&getsetup);
14         hook(type => "preprocess", id => "img", call => \&preprocess, scan => 1);
15 }
17 sub getsetup () {
18         return
19                 plugin => {
20                         safe => 1,
21                         rebuild => undef,
22                         section => "widget",
23                 },
24 }
26 sub preprocess (@) {
27         my ($image) = $_[0] =~ /$config{wiki_file_regexp}/; # untaint
28         my %params=@_;
30         if (! defined $image) {
31                 error("bad image filename");
32         }
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         }
42         if (! exists $params{size} || ! length $params{size}) {
43                 $params{size}='full';
44         }
46         if ($image eq 'defaults') {
47                 $imgdefaults{$params{page}} = \%params;
48                 return '';
49         }
51         add_link($params{page}, $image);
52         add_depends($params{page}, $image);
54         # optimisation: detect scan mode, and avoid generating the image
55         if (! defined wantarray) {
56                 return;
57         }
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         }
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         }
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 $r = $im->Read("$srcfile\[$pagenumber]");
79         error sprintf(gettext("failed to read %s: %s"), $file, $r) if $r;
80         
81         my ($dwidth, $dheight);
83         if ($params{size} eq 'full') {
84                 $dwidth = $im->Get("width");
85                 $dheight = $im->Get("height");
86         } else {
87                 my ($w, $h) = ($params{size} =~ /^(\d*)x(\d*)$/);
88                 error sprintf(gettext('wrong size format "%s" (should be WxH)'), $params{size})
89                         unless (defined $w && defined $h &&
90                                 (length $w || length $h));
92                 if ($im->Get("width") == 0 || $im->Get("height") == 0) {
93                         ($dwidth, $dheight)=(0, 0);
94                 } elsif (! length $w || (length $h && $im->Get("height")*$w > $h * $im->Get("width"))) {
95                         # using height because only height is given or ...
96                         # because original image is more portrait than $w/$h
97                         # ... slimness of $im > $h/w
98                         # ... $im->Get("height")/$im->Get("width") > $h/$w
99                         # ... $im->Get("height")*$w > $h * $im->Get("width")
101                         $dheight=$h;
102                         $dwidth=$h / $im->Get("height") * $im->Get("width");
103                 } else { # (! length $h) or $w is what determines the resized size
104                         $dwidth=$w;
105                         $dheight=$w / $im->Get("width") * $im->Get("height");
106                 }
107         }
109         if ($dwidth < $im->Get("width") || $ispdf) {
110                 # resize down, or resize to pixels at all
112                 my $outfile = "$config{destdir}/$dir/$params{size}-$base";
113                 $imglink = "$dir/$params{size}-$base";
115                 will_render($params{page}, $imglink);
117                 if (-e $outfile && (-M $srcfile >= -M $outfile)) {
118                         $im = Image::Magick->new;
119                         $r = $im->Read($outfile);
120                         error sprintf(gettext("failed to read %s: %s"), $outfile, $r) if $r;
121                 }
122                 else {
123                         $r = $im->Resize(geometry => "${dwidth}x${dheight}");
124                         error sprintf(gettext("failed to resize: %s"), $r) if $r;
126                         # don't actually write resized file in preview mode;
127                         # rely on width and height settings
128                         if (! $params{preview}) {
129                                 $im->set(($issvg || $ispdf) ? (magick => 'png') : ());
130                                 my @blob = $im->ImageToBlob();
131                                 writefile($imglink, $config{destdir}, $blob[0], 1);
132                         }
133                         else {
134                                 $imglink = $file;
135                         }
136                 }
138                 # always get the true size of the resized image (it could be
139                 # that imagemagick did its calculations differently)
140                 $dwidth  = $im->Get("width");
141                 $dheight = $im->Get("height");
142         } else {
143                 $imglink = $file;
144         }
145         
146         if (! defined($dwidth) || ! defined($dheight)) {
147                 error sprintf(gettext("failed to determine size of image %s"), $file)
148         }
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         }
160         if (! exists $params{class}) {
161                 $params{class}="img";
162         }
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                 ' />';
178         my $link;
179         if (! defined $params{link}) {
180                 $link=$fileurl;
181         }
182         elsif ($params{link} =~ /^\w+:\/\//) {
183                 $link=$params{link};
184         }
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         }
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         }