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);
27 my ($image) = $_[0] =~ /$config{wiki_file_regexp}/; # untaint
30 if (! defined $image) {
31 error("bad image filename");
34 if (exists $imgdefaults{$params{page}}) {
35 foreach my $key (keys %{$imgdefaults{$params{page}}}) {
36 if (! exists $params{$key}) {
37 $params{$key}=$imgdefaults{$params{page}}->{$key};
42 if (! exists $params{size}) {
46 if ($image eq 'defaults') {
47 $imgdefaults{$params{page}} = \%params;
51 add_link($params{page}, $image);
52 add_depends($params{page}, $image);
54 # optimisation: detect scan mode, and avoid generating the image
55 if (! defined wantarray) {
59 my $file = bestlink($params{page}, $image);
60 my $srcfile = srcfile($file, 1);
61 if (! length $file || ! defined $srcfile) {
62 return htmllink($params{page}, $params{destpage}, $image);
65 my $dir = $params{page};
66 my $base = IkiWiki::basename($file);
68 eval q{use Image::Magick};
69 error gettext("Image::Magick is not installed") if $@;
70 my $im = Image::Magick->new;
72 my $r = $im->Read($srcfile);
73 error sprintf(gettext("failed to read %s: %s"), $file, $r) if $r;
75 my ($dwidth, $dheight);
77 if ($params{size} ne 'full') {
78 my ($w, $h) = ($params{size} =~ /^(\d*)x(\d*)$/);
79 error sprintf(gettext('wrong size format "%s" (should be WxH)'), $params{size})
80 unless (defined $w && defined $h &&
81 (length $w || length $h));
83 if ((length $w && $w > $im->Get("width")) ||
84 (length $h && $h > $im->Get("height"))) {
88 # don't generate larger image, just set display size
89 if (length $w && length $h) {
90 ($dwidth, $dheight)=($w, $h);
92 # avoid division by zero on 0x0 image
93 elsif ($im->Get("width") == 0 || $im->Get("height") == 0) {
94 ($dwidth, $dheight)=(0, 0);
96 # calculate unspecified size from the other one, preserving
100 $dheight=$w / $im->Get("width") * $im->Get("height");
104 $dwidth=$h / $im->Get("height") * $im->Get("width");
109 my $outfile = "$config{destdir}/$dir/${w}x${h}-$base";
110 $imglink = "$dir/${w}x${h}-$base";
112 will_render($params{page}, $imglink);
114 if (-e $outfile && (-M $srcfile >= -M $outfile)) {
115 $im = Image::Magick->new;
116 $r = $im->Read($outfile);
117 error sprintf(gettext("failed to read %s: %s"), $outfile, $r) if $r;
119 $dwidth = $im->Get("width");
120 $dheight = $im->Get("height");
123 ($dwidth, $dheight)=($w, $h);
124 $r = $im->Resize(geometry => "${w}x${h}");
125 error sprintf(gettext("failed to resize: %s"), $r) if $r;
127 # don't actually write file in preview mode
128 if (! $params{preview}) {
129 my @blob = $im->ImageToBlob();
130 writefile($imglink, $config{destdir}, $blob[0], 1);
140 $dwidth = $im->Get("width");
141 $dheight = $im->Get("height");
144 if (! defined($dwidth) || ! defined($dheight)) {
145 error sprintf(gettext("failed to determine size of image %s"), $file)
148 my ($fileurl, $imgurl);
149 if (! $params{preview}) {
150 $fileurl=urlto($file, $params{destpage});
151 $imgurl=urlto($imglink, $params{destpage});
154 $fileurl="$config{url}/$file";
155 $imgurl="$config{url}/$imglink";
158 my $imgtag='<img src="'.$imgurl.
160 '" height="'.$dheight.'"'.
161 (exists $params{alt} ? ' alt="'.$params{alt}.'"' : '').
162 (exists $params{title} ? ' title="'.$params{title}.'"' : '').
163 (exists $params{class} ? ' class="'.$params{class}.'"' : '').
164 (exists $params{align} && ! exists $params{caption} ? ' align="'.$params{align}.'"' : '').
165 (exists $params{id} ? ' id="'.$params{id}.'"' : '').
169 if (! defined $params{link}) {
172 elsif ($params{link} =~ /^\w+:\/\//) {
177 $imgtag='<a href="'.$link.'">'.$imgtag.'</a>';
180 my $b = bestlink($params{page}, $params{link});
183 add_depends($params{page}, $b, deptype("presence"));
184 $imgtag=htmllink($params{page}, $params{destpage},
185 $params{link}, linktext => $imgtag,
191 if (exists $params{caption}) {
192 return '<table class="img'.
193 (exists $params{align} ? " align-$params{align}" : "").
195 '<caption>'.$params{caption}.'</caption>'.
196 '<tr><td>'.$imgtag.'</td></tr>'.