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();
78 my $r = $im->Read("$srcfile\[$pagenumber]");
79 error sprintf(gettext("failed to read %s: %s"), $file, $r) if $r;
81 my ($dwidth, $dheight);
83 if ($params{size} eq 'full') {
84 $dwidth = $im->Get("width");
85 $dheight = $im->Get("height");
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")
102 $dwidth=$h / $im->Get("height") * $im->Get("width");
103 } else { # (! length $h) or $w is what determines the resized size
105 $dheight=$w / $im->Get("width") * $im->Get("height");
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;
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);
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");
146 if (! defined($dwidth) || ! defined($dheight)) {
147 error sprintf(gettext("failed to determine size of image %s"), $file)
150 my ($fileurl, $imgurl);
151 if (! $params{preview}) {
152 $fileurl=urlto($file, $params{destpage});
153 $imgurl=urlto($imglink, $params{destpage});
156 $fileurl=urlto($file);
157 $imgurl=urlto($imglink);
160 if (! exists $params{class}) {
161 $params{class}="img";
165 foreach my $attr (qw{alt title class id hspace vspace}) {
166 if (exists $params{$attr}) {
167 $attrs.=" $attr=\"$params{$attr}\"";
171 my $imgtag='<img src="'.$imgurl.
173 '" height="'.$dheight.'"'.
175 (exists $params{align} && ! exists $params{caption} ? ' align="'.$params{align}.'"' : '').
179 if (! defined $params{link}) {
182 elsif ($params{link} =~ /^\w+:\/\//) {
187 $imgtag='<a href="'.$link.'">'.$imgtag.'</a>';
190 my $b = bestlink($params{page}, $params{link});
193 add_depends($params{page}, $b, deptype("presence"));
194 $imgtag=htmllink($params{page}, $params{destpage},
195 $params{link}, linktext => $imgtag,
201 if (exists $params{caption}) {
202 return '<table class="img'.
203 (exists $params{align} ? " align-$params{align}" : "").
205 '<caption>'.$params{caption}.'</caption>'.
206 '<tr><td>'.$imgtag.'</td></tr>'.