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;
69 eval q{use Image::Magick};
70 error gettext("Image::Magick is not installed") if $@;
71 my $im = Image::Magick->new($issvg ? (magick => "png") : ());
73 my $r = $im->Read($srcfile);
74 error sprintf(gettext("failed to read %s: %s"), $file, $r) if $r;
76 my ($dwidth, $dheight);
78 if ($params{size} ne 'full') {
79 my ($w, $h) = ($params{size} =~ /^(\d*)x(\d*)$/);
80 error sprintf(gettext('wrong size format "%s" (should be WxH)'), $params{size})
81 unless (defined $w && defined $h &&
82 (length $w || length $h));
84 if ((length $w && $w > $im->Get("width")) ||
85 (length $h && $h > $im->Get("height"))) {
89 # don't generate larger image, just set display size
90 if (length $w && length $h) {
91 ($dwidth, $dheight)=($w, $h);
93 # avoid division by zero on 0x0 image
94 elsif ($im->Get("width") == 0 || $im->Get("height") == 0) {
95 ($dwidth, $dheight)=(0, 0);
97 # calculate unspecified size from the other one, preserving
101 $dheight=$w / $im->Get("width") * $im->Get("height");
105 $dwidth=$h / $im->Get("height") * $im->Get("width");
110 my $outfile = "$config{destdir}/$dir/${w}x${h}-$base";
111 $imglink = "$dir/${w}x${h}-$base";
113 will_render($params{page}, $imglink);
115 if (-e $outfile && (-M $srcfile >= -M $outfile)) {
116 $im = Image::Magick->new;
117 $r = $im->Read($outfile);
118 error sprintf(gettext("failed to read %s: %s"), $outfile, $r) if $r;
121 ($dwidth, $dheight)=($w, $h);
122 $r = $im->Resize(geometry => "${w}x${h}");
123 error sprintf(gettext("failed to resize: %s"), $r) if $r;
125 # don't actually write resized file in preview mode;
126 # rely on width and height settings
127 if (! $params{preview}) {
128 my @blob = $im->ImageToBlob();
129 writefile($imglink, $config{destdir}, $blob[0], 1);
135 # always get the true size of the resized image
136 $dwidth = $im->Get("width");
137 $dheight = $im->Get("height");
142 $dwidth = $im->Get("width");
143 $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>'.