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 foreach my $key (keys %{$imgdefaults{$params{page}}}) {
22 if (! exists $params{$key}) {
23 $params{$key}=$imgdefaults{$params{page}}->{$key};
28 if (! exists $params{size}) {
32 if ($image eq 'defaults') {
33 $imgdefaults{$params{page}} = \%params;
37 push @{$links{$params{page}}}, $image;
38 # optimisation: detect scan mode, and avoid generating the image
39 if (! defined wantarray) {
43 my $file = bestlink($params{page}, $image);
44 my $srcfile = srcfile($file, 1);
45 if (! length $file || ! defined $srcfile) {
46 return htmllink($params{page}, $params{destpage}, $image);
49 my $dir = $params{page};
50 my $base = IkiWiki::basename($file);
52 eval q{use Image::Magick};
53 error gettext("Image::Magick is not installed") if $@;
54 my $im = Image::Magick->new;
58 if ($params{size} ne 'full') {
59 my ($w, $h) = ($params{size} =~ /^(\d+)x(\d+)$/);
60 error sprintf(gettext('bad size "%s"'), $params{size})
61 unless (defined $w && defined $h);
63 my $outfile = "$config{destdir}/$dir/${w}x${h}-$base";
64 $imglink = "$dir/${w}x${h}-$base";
66 will_render($params{page}, $imglink);
68 if (-e $outfile && (-M $srcfile >= -M $outfile)) {
69 $r = $im->Read($outfile);
70 error sprintf(gettext("failed to read %s: %s"), $outfile, $r) if $r;
73 $r = $im->Read($srcfile);
74 error sprintf(gettext("failed to read %s: %s"), $file, $r) if $r;
76 $r = $im->Resize(geometry => "${w}x${h}");
77 error sprintf(gettext("failed to resize: %s"), $r) if $r;
79 # don't actually write file in preview mode
80 if (! $params{preview}) {
81 my @blob = $im->ImageToBlob();
82 writefile($imglink, $config{destdir}, $blob[0], 1);
90 $r = $im->Read($srcfile);
91 error sprintf(gettext("failed to read %s: %s"), $file, $r) if $r;
95 add_depends($imglink, $params{page});
97 my ($fileurl, $imgurl);
98 if (! $params{preview}) {
99 $fileurl=urlto($file, $params{destpage});
100 $imgurl=urlto($imglink, $params{destpage});
103 $fileurl="$config{url}/$file";
104 $imgurl="$config{url}/$imglink";
107 if (! defined($im->Get("width")) || ! defined($im->Get("height"))) {
108 error sprintf(gettext("failed to determine size of image %s"), $file)
111 my $imgtag='<img src="'.$imgurl.
112 '" alt="'.(exists $params{alt} ? $params{alt} : '').
113 '" width="'.$im->Get("width").
114 '" height="'.$im->Get("height").'"'.
115 (exists $params{title} ? ' title="'.$params{title}.'"' : '').
116 (exists $params{class} ? ' class="'.$params{class}.'"' : '').
117 (exists $params{id} ? ' id="'.$params{id}.'"' : '').
120 if (! defined $params{link} || lc($params{link}) eq 'yes') {
121 $imgtag='<a href="'.$fileurl.'">'.$imgtag.'</a>';
123 elsif ($params{link} =~ /^\w+:\/\//) {
124 $imgtag='<a href="'.$params{link}.'">'.$imgtag.'</a>';
126 elsif (length bestlink($params{page}, $params{link})) {
127 add_depends($params{page}, $params{link});
128 $imgtag=htmllink($params{page}, $params{destpage},
129 $params{link}, linktext => $imgtag,
133 if (exists $params{caption}) {
134 return '<table class="img">'.
135 '<caption>'.$params{caption}.'</caption>'.
136 '<tr><td>'.$imgtag.'</td></tr>'.