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} || '';
25 my $title = $params{title} || $imgdefaults{$params{page}}->{title} || '';
27 if ($image eq 'defaults') {
28 $imgdefaults{$params{page}} = {
36 push @{$links{$params{page}}}, $image;
37 # optimisation: detect scan mode, and avoid generating the image
38 if (! defined wantarray) {
42 my $file = bestlink($params{page}, $image);
44 my $dir = $params{page};
45 my $base = IkiWiki::basename($file);
47 eval q{use Image::Magick};
49 my $im = Image::Magick->new;
53 if ($size ne 'full') {
54 my ($w, $h) = ($size =~ /^(\d+)x(\d+)$/);
55 return "[[img ".sprintf(gettext('bad size "%s"'), $size)."]]"
56 unless (defined $w && defined $h);
58 my $outfile = "$config{destdir}/$dir/${w}x${h}-$base";
59 $imglink = "$dir/${w}x${h}-$base";
61 will_render($params{page}, $imglink);
63 if (-e $outfile && (-M srcfile($file) >= -M $outfile)) {
64 $r = $im->Read($outfile);
65 return "[[img ".sprintf(gettext("failed to read %s: %s"), $outfile, $r)."]]" if $r;
68 $r = $im->Read(srcfile($file));
69 return "[[img ".sprintf(gettext("failed to read %s: %s"), $file, $r)."]]" if $r;
71 $r = $im->Resize(geometry => "${w}x${h}");
72 return "[[img ".sprintf(gettext("failed to resize: %s"), $r)."]]" if $r;
74 # don't actually write file in preview mode
75 if (! $params{preview}) {
76 my @blob = $im->ImageToBlob();
77 writefile($imglink, $config{destdir}, $blob[0], 1);
85 $r = $im->Read(srcfile($file));
86 return "[[img ".sprintf(gettext("failed to read %s: %s"), $file, $r)."]]" if $r;
90 add_depends($imglink, $params{page});
92 my ($fileurl, $imgurl);
93 if (! $params{preview}) {
94 $fileurl=urlto($file, $params{destpage});
95 $imgurl=urlto($imglink, $params{destpage});
98 $fileurl="$config{url}/$file";
99 $imgurl="$config{url}/$imglink";
102 if (! defined($im->Get("width")) || ! defined($im->Get("height"))) {
103 return "[[img ".sprintf(gettext("failed to determine size of image %s"), $file)."]]";
106 my $imgtag='<img src="'.$imgurl.
107 '" alt="'.$alt.'" width="'.$im->Get("width").
108 '" height="'.$im->Get("height").'"'.
109 (defined $title ? ' title="'.$title.'"' : '').
110 (exists $params{class} ? ' class="'.$params{class}.'"' : '').
111 (exists $params{id} ? ' id="'.$params{id}.'"' : '').
114 if (! defined $params{link} || lc($params{link}) eq 'yes') {
115 return '<a href="'.$fileurl.'">'.$imgtag.'</a>';
117 elsif ($params{link} =~ /^\w+:\/\//) {
118 return '<a href="'.$params{link}.'">'.$imgtag.'</a>';
120 elsif (length bestlink($params{page}, $params{link})) {
121 add_depends($params{page}, $params{link});
122 return htmllink($params{page}, $params{destpage},
123 $params{link}, linktext => $imgtag,