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;
66 my $r = $im->Read($srcfile);
67 error sprintf(gettext("failed to read %s: %s"), $file, $r) if $r;
69 my ($dwidth, $dheight);
71 if ($params{size} ne 'full') {
72 add_depends($params{page}, $image);
74 my ($w, $h) = ($params{size} =~ /^(\d*)x(\d*)$/);
75 error sprintf(gettext('wrong size format "%s" (should be WxH)'), $params{size})
76 unless (defined $w && defined $h &&
77 (length $w || length $h));
79 if ((length $w && $w > $im->Get("width")) ||
80 (length $h && $h > $im->Get("height"))) {
84 # don't generate larger image, just set display size
85 if (length $w && length $h) {
86 ($dwidth, $dheight)=($w, $h);
88 # avoid division by zero on 0x0 image
89 elsif ($im->Get("width") == 0 || $im->Get("height") == 0) {
90 ($dwidth, $dheight)=(0, 0);
92 # calculate unspecified size from the other one, preserving
96 $dheight=$w / $im->Get("width") * $im->Get("height");
100 $dwidth=$h / $im->Get("height") * $im->Get("width");
105 my $outfile = "$config{destdir}/$dir/${w}x${h}-$base";
106 $imglink = "$dir/${w}x${h}-$base";
108 will_render($params{page}, $imglink);
110 if (-e $outfile && (-M $srcfile >= -M $outfile)) {
111 $im = Image::Magick->new;
112 $r = $im->Read($outfile);
113 error sprintf(gettext("failed to read %s: %s"), $outfile, $r) if $r;
115 $dwidth = $im->Get("width");
116 $dheight = $im->Get("height");
119 ($dwidth, $dheight)=($w, $h);
120 $r = $im->Resize(geometry => "${w}x${h}");
121 error sprintf(gettext("failed to resize: %s"), $r) if $r;
123 # don't actually write file in preview mode
124 if (! $params{preview}) {
125 my @blob = $im->ImageToBlob();
126 writefile($imglink, $config{destdir}, $blob[0], 1);
136 $dwidth = $im->Get("width");
137 $dheight = $im->Get("height");
140 if (! defined($dwidth) || ! defined($dheight)) {
141 error sprintf(gettext("failed to determine size of image %s"), $file)
144 my ($fileurl, $imgurl);
145 if (! $params{preview}) {
146 $fileurl=urlto($file, $params{destpage});
147 $imgurl=urlto($imglink, $params{destpage});
150 $fileurl="$config{url}/$file";
151 $imgurl="$config{url}/$imglink";
154 my $imgtag='<img src="'.$imgurl.
156 '" height="'.$dheight.'"'.
157 (exists $params{alt} ? ' alt="'.$params{alt}.'"' : '').
158 (exists $params{title} ? ' title="'.$params{title}.'"' : '').
159 (exists $params{align} ? ' align="'.$params{align}.'"' : '').
160 (exists $params{class} ? ' class="'.$params{class}.'"' : '').
161 (exists $params{id} ? ' id="'.$params{id}.'"' : '').
164 if (! defined $params{link} || lc($params{link}) eq 'yes') {
165 $imgtag='<a href="'.$fileurl.'">'.$imgtag.'</a>';
167 elsif ($params{link} =~ /^\w+:\/\//) {
168 $imgtag='<a href="'.$params{link}.'">'.$imgtag.'</a>';
171 my $b = bestlink($params{page}, $params{link});
174 add_depends($params{page}, $b);
175 $imgtag=htmllink($params{page}, $params{destpage},
176 $params{link}, linktext => $imgtag,
181 if (exists $params{caption}) {
182 return '<table class="img">'.
183 '<caption>'.$params{caption}.'</caption>'.
184 '<tr><td>'.$imgtag.'</td></tr>'.