2 # Ikiwiki enhanced image handling plugin
3 # Christian Mock cm@tahina.priv.at 20061002
4 package IkiWiki::Plugin::img;
13 hook(type => "getsetup", id => "img", call => \&getsetup);
14 hook(type => "preprocess", id => "img", call => \&preprocess, scan => 1);
26 my ($image) = $_[0] =~ /$config{wiki_file_regexp}/; # untaint
29 if (exists $imgdefaults{$params{page}}) {
30 foreach my $key (keys %{$imgdefaults{$params{page}}}) {
31 if (! exists $params{$key}) {
32 $params{$key}=$imgdefaults{$params{page}}->{$key};
37 if (! exists $params{size}) {
41 if ($image eq 'defaults') {
42 $imgdefaults{$params{page}} = \%params;
46 add_link($params{page}, $image);
48 # optimisation: detect scan mode, and avoid generating the image
49 if (! defined wantarray) {
53 my $file = bestlink($params{page}, $image);
54 my $srcfile = srcfile($file, 1);
55 if (! length $file || ! defined $srcfile) {
56 return htmllink($params{page}, $params{destpage}, $image);
59 my $dir = $params{page};
60 my $base = IkiWiki::basename($file);
62 eval q{use Image::Magick};
63 error gettext("Image::Magick is not installed") if $@;
64 my $im = Image::Magick->new;
68 if ($params{size} ne 'full') {
69 add_depends($params{page}, $image);
71 my ($w, $h) = ($params{size} =~ /^(\d*)x(\d*)$/);
72 error sprintf(gettext('wrong size format "%s" (should be WxH)'), $params{size})
73 unless (defined $w && defined $h &&
74 (length $w || length $h));
76 my $outfile = "$config{destdir}/$dir/${w}x${h}-$base";
77 $imglink = "$dir/${w}x${h}-$base";
79 will_render($params{page}, $imglink);
81 if (-e $outfile && (-M $srcfile >= -M $outfile)) {
82 $r = $im->Read($outfile);
83 error sprintf(gettext("failed to read %s: %s"), $outfile, $r) if $r;
86 $r = $im->Read($srcfile);
87 error sprintf(gettext("failed to read %s: %s"), $file, $r) if $r;
89 $r = $im->Resize(geometry => "${w}x${h}");
90 error sprintf(gettext("failed to resize: %s"), $r) if $r;
92 # don't actually write file in preview mode
93 if (! $params{preview}) {
94 my @blob = $im->ImageToBlob();
95 writefile($imglink, $config{destdir}, $blob[0], 1);
103 $r = $im->Read($srcfile);
104 error sprintf(gettext("failed to read %s: %s"), $file, $r) if $r;
108 my ($fileurl, $imgurl);
109 if (! $params{preview}) {
110 $fileurl=urlto($file, $params{destpage});
111 $imgurl=urlto($imglink, $params{destpage});
114 $fileurl="$config{url}/$file";
115 $imgurl="$config{url}/$imglink";
118 if (! defined($im->Get("width")) || ! defined($im->Get("height"))) {
119 error sprintf(gettext("failed to determine size of image %s"), $file)
122 my $imgtag='<img src="'.$imgurl.
123 '" width="'.$im->Get("width").
124 '" height="'.$im->Get("height").'"'.
125 (exists $params{alt} ? ' alt="'.$params{alt}.'"' : '').
126 (exists $params{title} ? ' title="'.$params{title}.'"' : '').
127 (exists $params{align} ? ' align="'.$params{align}.'"' : '').
128 (exists $params{class} ? ' class="'.$params{class}.'"' : '').
129 (exists $params{id} ? ' id="'.$params{id}.'"' : '').
132 if (! defined $params{link} || lc($params{link}) eq 'yes') {
133 $imgtag='<a href="'.$fileurl.'">'.$imgtag.'</a>';
135 elsif ($params{link} =~ /^\w+:\/\//) {
136 $imgtag='<a href="'.$params{link}.'">'.$imgtag.'</a>';
139 my $b = bestlink($params{page}, $params{link});
142 add_depends($params{page}, $b);
143 $imgtag=htmllink($params{page}, $params{destpage},
144 $params{link}, linktext => $imgtag,
149 if (exists $params{caption}) {
150 return '<table class="img">'.
151 '<caption>'.$params{caption}.'</caption>'.
152 '<tr><td>'.$imgtag.'</td></tr>'.