2 # Ikiwiki enhanced image handling plugin
3 # Christian Mock cm@tahina.priv.at 20061002
4 package IkiWiki::Plugin::img;
11 my $convert = 'convert';
16 hook(type => "preprocess", id => "img", call => \&preprocess);
19 sub preprocess (@) { #{{{
20 my ($image) = $_[0] =~ /$config{wiki_file_regexp}/; # untaint
23 if (! exists $imgdefaults{$params{page}}) {
24 $imgdefaults{$params{page}} = {};
26 my $size = $params{size} || $imgdefaults{$params{page}}->{size} || 'full';
27 my $alt = $params{alt} || $imgdefaults{$params{page}}->{alt} || '';
29 if ($image eq 'defaults') {
30 $imgdefaults{$params{page}} = {
37 add_depends($params{page}, $image);
38 my $file = bestlink($params{page}, $image) || return "[[img $image not found]]";
40 my $dir = IkiWiki::dirname($file);
41 my $base = IkiWiki::basename($file);
42 my $im = Image::Magick->new;
46 if ($size ne 'full') {
47 my ($w, $h) = ($size =~ /^(\d+)x(\d+)$/);
48 return "[[img bad size \"$size\"]]" unless (defined $w && defined $h);
50 my $outfile = "$config{destdir}/$dir/${w}x${h}-$base";
51 $imglink = "$dir/${w}x${h}-$base";
52 will_render($params{page}, $imglink);
54 if (-e $outfile && (-M srcfile($file) >= -M $outfile)) {
55 $r = $im->Read($outfile);
56 return "[[img failed to read $outfile: $r]]" if $r;
59 $r = $im->Read(srcfile($file));
60 return "[[img failed to read $file: $r]]" if $r;
62 $r = $im->Resize(geometry => "${w}x${h}");
63 return "[[img failed to resize: $r]]" if $r;
65 my @blob = $im->ImageToBlob();
66 writefile($imglink, $config{destdir}, $blob[0], 1);
70 $r = $im->Read(srcfile($file));
71 return "[[img failed to read $file: $r]]" if $r;
75 add_depends($imglink, $params{page});
78 IkiWiki::abs2rel($file, IkiWiki::dirname($params{destpage})).
80 IkiWiki::abs2rel($imglink, IkiWiki::dirname($params{destpage})).
81 '" alt="'.$alt.'" width="'.$im->Get("width").
82 '" height="'.$im->Get("height").'" /></a>';