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);
45 my $dir = $params{page};
46 my $base = IkiWiki::basename($file);
48 eval q{use Image::Magick};
49 error gettext("Image::Magick is not installed") if $@;
50 my $im = Image::Magick->new;
54 if ($params{size} ne 'full') {
55 my ($w, $h) = ($params{size} =~ /^(\d+)x(\d+)$/);
56 error sprintf(gettext('bad size "%s"'), $params{size})
57 unless (defined $w && defined $h);
59 my $outfile = "$config{destdir}/$dir/${w}x${h}-$base";
60 $imglink = "$dir/${w}x${h}-$base";
62 will_render($params{page}, $imglink);
64 if (-e $outfile && (-M srcfile($file) >= -M $outfile)) {
65 $r = $im->Read($outfile);
66 error sprintf(gettext("failed to read %s: %s"), $outfile, $r) if $r;
69 $r = $im->Read(srcfile($file));
70 error sprintf(gettext("failed to read %s: %s"), $file, $r) if $r;
72 $r = $im->Resize(geometry => "${w}x${h}");
73 error sprintf(gettext("failed to resize: %s"), $r) if $r;
75 # don't actually write file in preview mode
76 if (! $params{preview}) {
77 my @blob = $im->ImageToBlob();
78 writefile($imglink, $config{destdir}, $blob[0], 1);
86 $r = $im->Read(srcfile($file));
87 error sprintf(gettext("failed to read %s: %s"), $file, $r) if $r;
91 add_depends($imglink, $params{page});
93 my ($fileurl, $imgurl);
94 if (! $params{preview}) {
95 $fileurl=urlto($file, $params{destpage});
96 $imgurl=urlto($imglink, $params{destpage});
99 $fileurl="$config{url}/$file";
100 $imgurl="$config{url}/$imglink";
103 if (! defined($im->Get("width")) || ! defined($im->Get("height"))) {
104 error sprintf(gettext("failed to determine size of image %s"), $file)
107 my $imgtag='<img src="'.$imgurl.
108 '" alt="'.(exists $params{alt} ? $params{alt} : '').
109 '" width="'.$im->Get("width").
110 '" height="'.$im->Get("height").'"'.
111 (exists $params{title} ? ' title="'.$params{title}.'"' : '').
112 (exists $params{class} ? ' class="'.$params{class}.'"' : '').
113 (exists $params{id} ? ' id="'.$params{id}.'"' : '').
116 if (! defined $params{link} || lc($params{link}) eq 'yes') {
117 $imgtag='<a href="'.$fileurl.'">'.$imgtag.'</a>';
119 elsif ($params{link} =~ /^\w+:\/\//) {
120 $imgtag='<a href="'.$params{link}.'">'.$imgtag.'</a>';
122 elsif (length bestlink($params{page}, $params{link})) {
123 add_depends($params{page}, $params{link});
124 $imgtag=htmllink($params{page}, $params{destpage},
125 $params{link}, linktext => $imgtag,
129 if (exists $params{caption}) {
130 return '<table class="img">'.
131 '<caption>'.$params{caption}.'</caption>'.
132 '<tr><td>'.$imgtag.'</td></tr>'.