2 # Ikiwiki enhanced image handling plugin
3 # Christian Mock cm@tahina.priv.at 20061002
4 package IkiWiki::Plugin::img;
13 hook(type => "getsetup", id => "img", call => \&getsetup);
14 hook(type => "preprocess", id => "img", call => \&preprocess, scan => 1);
27 my ($image) = $_[0] =~ /$config{wiki_file_regexp}/; # untaint
30 if (! defined $image) {
31 error("bad image filename");
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};
42 if (! exists $params{size} || ! length $params{size}) {
46 if ($image eq 'defaults') {
47 $imgdefaults{$params{page}} = \%params;
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) {
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);
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";
74 eval q{use Image::Magick};
75 error gettext("Image::Magick is not installed") if $@;
76 my $im = Image::Magick->new();
79 my $r = $im->Read("$srcfile\[$pagenumber]");
80 error sprintf(gettext("failed to read %s: %s"), $file, $r) if $r;
82 my ($dwidth, $dheight);
84 if ($params{size} eq 'full') {
85 $dwidth = $im->Get("width");
86 $dheight = $im->Get("height");
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));
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")
103 $dwidth=$h / $im->Get("height") * $im->Get("width");
104 } else { # (! length $h) or $w is what determines the resized size
106 $dheight=$w / $im->Get("width") * $im->Get("height");
110 if ($dwidth < $im->Get("width") || $ispdf) {
111 # resize down, or resize to pixels at all
113 my $outfile = "$config{destdir}/$dir/$params{size}-$base";
114 $imglink = "$dir/$params{size}-$base";
116 will_render($params{page}, $imglink);
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;
124 $r = $im->Resize(geometry => "${dwidth}x${dheight}");
125 error sprintf(gettext("failed to resize: %s"), $r) if $r;
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);
135 eval q{use MIME::Base64};
137 $imgdatalink = "data:image/".$im->Get("magick").";base64,".encode_base64($blob[0]);
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");
149 if (! defined($dwidth) || ! defined($dheight)) {
150 error sprintf(gettext("failed to determine size of image %s"), $file)
153 my ($fileurl, $imgurl);
154 my $urltobase = $params{preview} ? undef : $params{destpage};
155 $fileurl=urlto($file, $urltobase);
156 $imgurl=$imgdatalink ? $imgdatalink : urlto($imglink, $urltobase);
158 if (! exists $params{class}) {
159 $params{class}="img";
163 foreach my $attr (qw{alt title class id hspace vspace}) {
164 if (exists $params{$attr}) {
165 $attrs.=" $attr=\"$params{$attr}\"";
169 my $imgtag='<img src="'.$imgurl.
171 '" height="'.$dheight.'"'.
173 (exists $params{align} && ! exists $params{caption} ? ' align="'.$params{align}.'"' : '').
177 if (! defined $params{link}) {
180 elsif ($params{link} =~ /^\w+:\/\//) {
185 $imgtag='<a href="'.$link.'">'.$imgtag.'</a>';
188 my $b = bestlink($params{page}, $params{link});
191 add_depends($params{page}, $b, deptype("presence"));
192 $imgtag=htmllink($params{page}, $params{destpage},
193 $params{link}, linktext => $imgtag,
199 if (exists $params{caption}) {
200 return '<table class="img'.
201 (exists $params{align} ? " align-$params{align}" : "").
203 '<caption>'.$params{caption}.'</caption>'.
204 '<tr><td>'.$imgtag.'</td></tr>'.