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);
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('wrong size format "%s" (should be WxH)'), $params{size})
70 unless (defined $w && defined $h &&
71 (length $w || length $h));
73 my $outfile = "$config{destdir}/$dir/${w}x${h}-$base";
74 $imglink = "$dir/${w}x${h}-$base";
76 will_render($params{page}, $imglink);
78 if (-e $outfile && (-M $srcfile >= -M $outfile)) {
79 $r = $im->Read($outfile);
80 error sprintf(gettext("failed to read %s: %s"), $outfile, $r) if $r;
83 $r = $im->Read($srcfile);
84 error sprintf(gettext("failed to read %s: %s"), $file, $r) if $r;
86 $r = $im->Resize(geometry => "${w}x${h}");
87 error sprintf(gettext("failed to resize: %s"), $r) if $r;
89 # don't actually write file in preview mode
90 if (! $params{preview}) {
91 my @blob = $im->ImageToBlob();
92 writefile($imglink, $config{destdir}, $blob[0], 1);
100 $r = $im->Read($srcfile);
101 error sprintf(gettext("failed to read %s: %s"), $file, $r) if $r;
105 add_depends($imglink, $params{page});
107 my ($fileurl, $imgurl);
108 if (! $params{preview}) {
109 $fileurl=urlto($file, $params{destpage});
110 $imgurl=urlto($imglink, $params{destpage});
113 $fileurl="$config{url}/$file";
114 $imgurl="$config{url}/$imglink";
117 if (! defined($im->Get("width")) || ! defined($im->Get("height"))) {
118 error sprintf(gettext("failed to determine size of image %s"), $file)
121 my $imgtag='<img src="'.$imgurl.
122 '" width="'.$im->Get("width").
123 '" height="'.$im->Get("height").'"'.
124 (exists $params{alt} ? ' alt="'.$params{alt}.'"' : '').
125 (exists $params{title} ? ' title="'.$params{title}.'"' : '').
126 (exists $params{align} ? ' align="'.$params{align}.'"' : '').
127 (exists $params{class} ? ' class="'.$params{class}.'"' : '').
128 (exists $params{id} ? ' id="'.$params{id}.'"' : '').
131 if (! defined $params{link} || lc($params{link}) eq 'yes') {
132 $imgtag='<a href="'.$fileurl.'">'.$imgtag.'</a>';
134 elsif ($params{link} =~ /^\w+:\/\//) {
135 $imgtag='<a href="'.$params{link}.'">'.$imgtag.'</a>';
137 elsif (length bestlink($params{page}, $params{link})) {
138 add_depends($params{page}, $params{link});
139 $imgtag=htmllink($params{page}, $params{destpage},
140 $params{link}, linktext => $imgtag,
144 if (exists $params{caption}) {
145 return '<table class="img">'.
146 '<caption>'.$params{caption}.'</caption>'.
147 '<tr><td>'.$imgtag.'</td></tr>'.