2 # Ikiwiki enhanced image handling plugin
3 # Christian Mock cm@tahina.priv.at 20061002
4 package IkiWiki::Plugin::img;
13 hook(type => "preprocess", id => "img", call => \&preprocess, scan => 1);
16 sub preprocess (@) { #{{{
17 my ($image) = $_[0] =~ /$config{wiki_file_regexp}/; # untaint
20 if (! exists $imgdefaults{$params{page}}) {
21 $imgdefaults{$params{page}} = {};
23 my $size = $params{size} || $imgdefaults{$params{page}}->{size} || 'full';
24 my $alt = $params{alt} || $imgdefaults{$params{page}}->{alt} || '';
26 if ($image eq 'defaults') {
27 $imgdefaults{$params{page}} = {
34 push @{$links{$params{page}}}, $image;
35 # optimisation: detect scan mode, and avoid generating the image
36 if (! defined wantarray) {
40 my $file = bestlink($params{page}, $image);
42 my $dir = $params{page};
43 my $base = IkiWiki::basename($file);
45 eval q{use Image::Magick};
47 my $im = Image::Magick->new;
51 if ($size ne 'full') {
52 my ($w, $h) = ($size =~ /^(\d+)x(\d+)$/);
53 return "[[img ".sprintf(gettext('bad size "%s"'), $size)."]]"
54 unless (defined $w && defined $h);
56 my $outfile = "$config{destdir}/$dir/${w}x${h}-$base";
57 $imglink = "$dir/${w}x${h}-$base";
59 will_render($params{page}, $imglink);
61 if (-e $outfile && (-M srcfile($file) >= -M $outfile)) {
62 $r = $im->Read($outfile);
63 return "[[img ".sprintf(gettext("failed to read %s: %s"), $outfile, $r)."]]" if $r;
66 $r = $im->Read(srcfile($file));
67 return "[[img ".sprintf(gettext("failed to read %s: %s"), $file, $r)."]]" if $r;
69 $r = $im->Resize(geometry => "${w}x${h}");
70 return "[[img ".sprintf(gettext("failed to resize: %s"), $r)."]]" if $r;
72 # don't actually write file in preview mode
73 if (! $params{preview}) {
74 my @blob = $im->ImageToBlob();
75 writefile($imglink, $config{destdir}, $blob[0], 1);
83 $r = $im->Read(srcfile($file));
84 return "[[img ".sprintf(gettext("failed to read %s: %s"), $file, $r)."]]" if $r;
88 add_depends($imglink, $params{page});
90 my ($fileurl, $imgurl);
91 if (! $params{preview}) {
92 $fileurl=urlto($file, $params{destpage});
93 $imgurl=urlto($imglink, $params{destpage});
96 $fileurl="$config{url}/$file";
97 $imgurl="$config{url}/$imglink";
100 if (! defined($im->Get("width")) || ! defined($im->Get("height"))) {
101 return "[[img ".sprintf(gettext("failed to determine size of image %s"), $file)."]]";
104 my $imgtag='<img src="'.$imgurl.
105 '" alt="'.$alt.'" width="'.$im->Get("width").
106 '" height="'.$im->Get("height").'"'.
107 (exists $params{class} ? ' class="'.$params{class}.'"' : '').
108 (exists $params{id} ? ' id="'.$params{id}.'"' : '').
111 if (! defined $params{link} || lc($params{link}) eq 'yes') {
112 return '<a href="'.$fileurl.'">'.$imgtag.'</a>';
114 elsif ($params{link} =~ /^\w+:\/\//) {
115 return '<a href="'.$params{link}.'">'.$imgtag.'</a>';
117 elsif (length bestlink($params{page}, $params{link})) {
118 add_depends($params{page}, $params{link});
119 return htmllink($params{page}, $params{destpage},
120 $params{link}, linktext => $imgtag,