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);
26 my ($image) = $_[0] =~ /$config{wiki_file_regexp}/; # untaint
29 if (exists $imgdefaults{$params{page}}) {
30 foreach my $key (keys %{$imgdefaults{$params{page}}}) {
31 if (! exists $params{$key}) {
32 $params{$key}=$imgdefaults{$params{page}}->{$key};
37 if (! exists $params{size}) {
41 if ($image eq 'defaults') {
42 $imgdefaults{$params{page}} = \%params;
46 add_link($params{page}, $image);
48 # optimisation: detect scan mode, and avoid generating the image
49 if (! defined wantarray) {
53 my $file = bestlink($params{page}, $image);
54 my $srcfile = srcfile($file, 1);
55 if (! length $file || ! defined $srcfile) {
56 return htmllink($params{page}, $params{destpage}, $image);
59 my $dir = $params{page};
60 my $base = IkiWiki::basename($file);
62 eval q{use Image::Magick};
63 error gettext("Image::Magick is not installed") if $@;
64 my $im = Image::Magick->new;
68 my ($dwidth, $dheight);
70 if ($params{size} ne 'full') {
71 add_depends($params{page}, $image);
73 my ($w, $h) = ($params{size} =~ /^(\d*)x(\d*)$/);
74 error sprintf(gettext('wrong size format "%s" (should be WxH)'), $params{size})
75 unless (defined $w && defined $h &&
76 (length $w || length $h));
78 my $outfile = "$config{destdir}/$dir/${w}x${h}-$base";
79 $imglink = "$dir/${w}x${h}-$base";
81 will_render($params{page}, $imglink);
83 if (-e $outfile && (-M $srcfile >= -M $outfile)) {
84 $r = $im->Read($outfile);
85 error sprintf(gettext("failed to read %s: %s"), $outfile, $r) if $r;
88 $r = $im->Read($srcfile);
89 error sprintf(gettext("failed to read %s: %s"), $file, $r) if $r;
91 # don't resize any larger
92 my ($rw, $rh) = ($w, $h);
93 if ((length $rw && $rw > $im->Get("width")) ||
94 (length $rh && $rh > $im->Get("height"))) {
95 $rw=$im->Get("width");
96 $rh=$im->Get("height");
99 $r = $im->Resize(geometry => "${rw}x${rh}");
100 error sprintf(gettext("failed to resize: %s"), $r) if $r;
102 # don't actually write file in preview mode
103 if (! $params{preview}) {
104 my @blob = $im->ImageToBlob();
105 writefile($imglink, $config{destdir}, $blob[0], 1);
112 # since we don't really resize larger, set the display
113 # size, so the browser can scale the image up if necessary
114 if (length $w && length $h) {
115 ($dwidth, $dheight)=($w, $h);
117 # avoid division by zero on 0x0 image
118 elsif ($im->Get("width") == 0 || $im->Get("height") == 0) {
119 ($dwidth, $dheight)=(0, 0);
121 # calculate unspecified size from the other one, preserving
125 $dheight=$w / $im->Get("width") * $im->Get("height");
129 $dwidth=$h / $im->Get("height") * $im->Get("width");
134 $r = $im->Read($srcfile);
135 error sprintf(gettext("failed to read %s: %s"), $file, $r) if $r;
137 $dwidth = $im->Get("width");
138 $dheight = $im->Get("height");
141 my ($fileurl, $imgurl);
142 if (! $params{preview}) {
143 $fileurl=urlto($file, $params{destpage});
144 $imgurl=urlto($imglink, $params{destpage});
147 $fileurl="$config{url}/$file";
148 $imgurl="$config{url}/$imglink";
151 if (! defined($im->Get("width")) || ! defined($im->Get("height"))) {
152 error sprintf(gettext("failed to determine size of image %s"), $file)
155 my $imgtag='<img src="'.$imgurl.
157 '" height="'.$dheight.'"'.
158 (exists $params{alt} ? ' alt="'.$params{alt}.'"' : '').
159 (exists $params{title} ? ' title="'.$params{title}.'"' : '').
160 (exists $params{align} ? ' align="'.$params{align}.'"' : '').
161 (exists $params{class} ? ' class="'.$params{class}.'"' : '').
162 (exists $params{id} ? ' id="'.$params{id}.'"' : '').
165 if (! defined $params{link} || lc($params{link}) eq 'yes') {
166 $imgtag='<a href="'.$fileurl.'">'.$imgtag.'</a>';
168 elsif ($params{link} =~ /^\w+:\/\//) {
169 $imgtag='<a href="'.$params{link}.'">'.$imgtag.'</a>';
172 my $b = bestlink($params{page}, $params{link});
175 add_depends($params{page}, $b);
176 $imgtag=htmllink($params{page}, $params{destpage},
177 $params{link}, linktext => $imgtag,
182 if (exists $params{caption}) {
183 return '<table class="img">'.
184 '<caption>'.$params{caption}.'</caption>'.
185 '<tr><td>'.$imgtag.'</td></tr>'.