2 # Ikiwiki enhanced image handling plugin
3 # Christian Mock cm@tahina.priv.at 20061002
4 package IkiWiki::Plugin::img;
14 hook(type => "preprocess", id => "img", call => \&preprocess);
17 sub preprocess (@) { #{{{
18 my ($image) = $_[0] =~ /$config{wiki_file_regexp}/; # untaint
21 if (! exists $imgdefaults{$params{page}}) {
22 $imgdefaults{$params{page}} = {};
24 my $size = $params{size} || $imgdefaults{$params{page}}->{size} || 'full';
25 my $alt = $params{alt} || $imgdefaults{$params{page}}->{alt} || '';
27 if ($image eq 'defaults') {
28 $imgdefaults{$params{page}} = {
35 add_depends($params{page}, $image);
36 my $file = bestlink($params{page}, $image) || return "[[img $image not found]]";
38 my $dir = IkiWiki::dirname($file);
39 my $base = IkiWiki::basename($file);
40 my $im = Image::Magick->new;
44 if ($size ne 'full') {
45 my ($w, $h) = ($size =~ /^(\d+)x(\d+)$/);
46 return "[[img bad size \"$size\"]]" unless (defined $w && defined $h);
48 my $outfile = "$config{destdir}/$dir/${w}x${h}-$base";
49 $imglink = "$dir/${w}x${h}-$base";
50 will_render($params{page}, $imglink);
52 if (-e $outfile && (-M srcfile($file) >= -M $outfile)) {
53 $r = $im->Read($outfile);
54 return "[[img failed to read $outfile: $r]]" if $r;
57 $r = $im->Read(srcfile($file));
58 return "[[img failed to read $file: $r]]" if $r;
60 $r = $im->Resize(geometry => "${w}x${h}");
61 return "[[img failed to resize: $r]]" if $r;
63 my @blob = $im->ImageToBlob();
64 writefile($imglink, $config{destdir}, $blob[0], 1);
68 $r = $im->Read(srcfile($file));
69 return "[[img failed to read $file: $r]]" if $r;
73 add_depends($imglink, $params{page});
76 IkiWiki::abs2rel($file, IkiWiki::dirname($params{destpage})).
78 IkiWiki::abs2rel($imglink, IkiWiki::dirname($params{destpage})).
79 '" alt="'.$alt.'" width="'.$im->Get("width").
80 '" height="'.$im->Get("height").'" /></a>';