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);
17 sub getsetup () { #{{{
25 sub preprocess (@) { #{{{
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 push @{$links{$params{page}}}, $image;
47 # optimisation: detect scan mode, and avoid generating the image
48 if (! defined wantarray) {
52 my $file = bestlink($params{page}, $image);
53 my $srcfile = srcfile($file, 1);
54 if (! length $file || ! defined $srcfile) {
55 return htmllink($params{page}, $params{destpage}, $image);
58 my $dir = $params{page};
59 my $base = IkiWiki::basename($file);
61 eval q{use Image::Magick};
62 error gettext("Image::Magick is not installed") if $@;
63 my $im = Image::Magick->new;
67 if ($params{size} ne 'full') {
68 my ($w, $h) = ($params{size} =~ /^(\d+)x(\d+)$/);
69 error sprintf(gettext('bad size "%s"'), $params{size})
70 unless (defined $w && defined $h);
72 my $outfile = "$config{destdir}/$dir/${w}x${h}-$base";
73 $imglink = "$dir/${w}x${h}-$base";
75 will_render($params{page}, $imglink);
77 if (-e $outfile && (-M $srcfile >= -M $outfile)) {
78 $r = $im->Read($outfile);
79 error sprintf(gettext("failed to read %s: %s"), $outfile, $r) if $r;
82 $r = $im->Read($srcfile);
83 error sprintf(gettext("failed to read %s: %s"), $file, $r) if $r;
85 $r = $im->Resize(geometry => "${w}x${h}");
86 error sprintf(gettext("failed to resize: %s"), $r) if $r;
88 # don't actually write file in preview mode
89 if (! $params{preview}) {
90 my @blob = $im->ImageToBlob();
91 writefile($imglink, $config{destdir}, $blob[0], 1);
99 $r = $im->Read($srcfile);
100 error sprintf(gettext("failed to read %s: %s"), $file, $r) if $r;
104 add_depends($imglink, $params{page});
106 my ($fileurl, $imgurl);
107 if (! $params{preview}) {
108 $fileurl=urlto($file, $params{destpage});
109 $imgurl=urlto($imglink, $params{destpage});
112 $fileurl="$config{url}/$file";
113 $imgurl="$config{url}/$imglink";
116 if (! defined($im->Get("width")) || ! defined($im->Get("height"))) {
117 error sprintf(gettext("failed to determine size of image %s"), $file)
120 my $imgtag='<img src="'.$imgurl.
121 '" alt="'.(exists $params{alt} ? $params{alt} : '').
122 '" width="'.$im->Get("width").
123 '" height="'.$im->Get("height").'"'.
124 (exists $params{title} ? ' title="'.$params{title}.'"' : '').
125 (exists $params{class} ? ' class="'.$params{class}.'"' : '').
126 (exists $params{id} ? ' id="'.$params{id}.'"' : '').
129 if (! defined $params{link} || lc($params{link}) eq 'yes') {
130 $imgtag='<a href="'.$fileurl.'">'.$imgtag.'</a>';
132 elsif ($params{link} =~ /^\w+:\/\//) {
133 $imgtag='<a href="'.$params{link}.'">'.$imgtag.'</a>';
135 elsif (length bestlink($params{page}, $params{link})) {
136 add_depends($params{page}, $params{link});
137 $imgtag=htmllink($params{page}, $params{destpage},
138 $params{link}, linktext => $imgtag,
142 if (exists $params{caption}) {
143 return '<table class="img">'.
144 '<caption>'.$params{caption}.'</caption>'.
145 '<tr><td>'.$imgtag.'</td></tr>'.