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 if (! defined $im->Get("width") || ! defined $im->Get("height")) {
83 error sprintf(gettext("failed to get dimensions of %s"), $file);
86 my ($dwidth, $dheight);
88 if ($params{size} eq 'full') {
89 $dwidth = $im->Get("width");
90 $dheight = $im->Get("height");
92 my ($w, $h) = ($params{size} =~ /^(\d*)x(\d*)$/);
93 error sprintf(gettext('wrong size format "%s" (should be WxH)'), $params{size})
94 unless (defined $w && defined $h &&
95 (length $w || length $h));
97 if ($im->Get("width") == 0 || $im->Get("height") == 0) {
98 ($dwidth, $dheight)=(0, 0);
99 } elsif (! length $w || (length $h && $im->Get("height")*$w > $h * $im->Get("width"))) {
100 # using height because only height is given or ...
101 # because original image is more portrait than $w/$h
102 # ... slimness of $im > $h/w
103 # ... $im->Get("height")/$im->Get("width") > $h/$w
104 # ... $im->Get("height")*$w > $h * $im->Get("width")
107 $dwidth=$h / $im->Get("height") * $im->Get("width");
108 } else { # (! length $h) or $w is what determines the resized size
110 $dheight=$w / $im->Get("width") * $im->Get("height");
114 if ($dwidth < $im->Get("width") || $ispdf) {
115 # resize down, or resize to pixels at all
117 my $outfile = "$config{destdir}/$dir/$params{size}-$base";
118 $imglink = "$dir/$params{size}-$base";
120 will_render($params{page}, $imglink);
122 if (-e $outfile && (-M $srcfile >= -M $outfile)) {
123 $im = Image::Magick->new;
124 $r = $im->Read($outfile);
125 error sprintf(gettext("failed to read %s: %s"), $outfile, $r) if $r;
128 $r = $im->Resize(geometry => "${dwidth}x${dheight}");
129 error sprintf(gettext("failed to resize: %s"), $r) if $r;
131 $im->set(($issvg || $ispdf) ? (magick => 'png') : ());
132 my @blob = $im->ImageToBlob();
133 # don't actually write resized file in preview mode;
134 # rely on width and height settings
135 if (! $params{preview}) {
136 writefile($imglink, $config{destdir}, $blob[0], 1);
139 eval q{use MIME::Base64};
141 $imgdatalink = "data:image/".$im->Get("magick").";base64,".encode_base64($blob[0]);
145 # always get the true size of the resized image (it could be
146 # that imagemagick did its calculations differently)
147 $dwidth = $im->Get("width");
148 $dheight = $im->Get("height");
153 if (! defined($dwidth) || ! defined($dheight)) {
154 error sprintf(gettext("failed to determine size of image %s"), $file)
157 my ($fileurl, $imgurl);
158 my $urltobase = $params{preview} ? undef : $params{destpage};
159 $fileurl=urlto($file, $urltobase);
160 $imgurl=$imgdatalink ? $imgdatalink : urlto($imglink, $urltobase);
162 if (! exists $params{class}) {
163 $params{class}="img";
167 foreach my $attr (qw{alt title class id hspace vspace}) {
168 if (exists $params{$attr}) {
169 $attrs.=" $attr=\"$params{$attr}\"";
173 my $imgtag='<img src="'.$imgurl.
175 '" height="'.$dheight.'"'.
177 (exists $params{align} && ! exists $params{caption} ? ' align="'.$params{align}.'"' : '').
181 if (! defined $params{link}) {
184 elsif ($params{link} =~ /^\w+:\/\//) {
189 $imgtag='<a href="'.$link.'">'.$imgtag.'</a>';
192 my $b = bestlink($params{page}, $params{link});
195 add_depends($params{page}, $b, deptype("presence"));
196 $imgtag=htmllink($params{page}, $params{destpage},
197 $params{link}, linktext => $imgtag,
203 if (exists $params{caption}) {
204 return '<table class="img'.
205 (exists $params{align} ? " align-$params{align}" : "").
207 '<caption>'.$params{caption}.'</caption>'.
208 '<tr><td>'.$imgtag.'</td></tr>'.