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 add_depends($params{page}, $image);
49 # optimisation: detect scan mode, and avoid generating the image
50 if (! defined wantarray) {
54 my $file = bestlink($params{page}, $image);
55 my $srcfile = srcfile($file, 1);
56 if (! length $file || ! defined $srcfile) {
57 return htmllink($params{page}, $params{destpage}, $image);
60 my $dir = $params{page};
61 my $base = IkiWiki::basename($file);
63 eval q{use Image::Magick};
64 error gettext("Image::Magick is not installed") if $@;
65 my $im = Image::Magick->new;
67 my $r = $im->Read($srcfile);
68 error sprintf(gettext("failed to read %s: %s"), $file, $r) if $r;
70 my ($dwidth, $dheight);
72 if ($params{size} ne 'full') {
73 my ($w, $h) = ($params{size} =~ /^(\d*)x(\d*)$/);
74 error sprintf(gettext('wrong size format "%s" (should be WxH)'), $params{size})
75 unless (defined $w && defined $h &&
76 (length $w || length $h));
78 if ((length $w && $w > $im->Get("width")) ||
79 (length $h && $h > $im->Get("height"))) {
83 # don't generate larger image, just set display size
84 if (length $w && length $h) {
85 ($dwidth, $dheight)=($w, $h);
87 # avoid division by zero on 0x0 image
88 elsif ($im->Get("width") == 0 || $im->Get("height") == 0) {
89 ($dwidth, $dheight)=(0, 0);
91 # calculate unspecified size from the other one, preserving
95 $dheight=$w / $im->Get("width") * $im->Get("height");
99 $dwidth=$h / $im->Get("height") * $im->Get("width");
104 my $outfile = "$config{destdir}/$dir/${w}x${h}-$base";
105 $imglink = "$dir/${w}x${h}-$base";
107 will_render($params{page}, $imglink);
109 if (-e $outfile && (-M $srcfile >= -M $outfile)) {
110 $im = Image::Magick->new;
111 $r = $im->Read($outfile);
112 error sprintf(gettext("failed to read %s: %s"), $outfile, $r) if $r;
114 $dwidth = $im->Get("width");
115 $dheight = $im->Get("height");
118 ($dwidth, $dheight)=($w, $h);
119 $r = $im->Resize(geometry => "${w}x${h}");
120 error sprintf(gettext("failed to resize: %s"), $r) if $r;
122 # don't actually write file in preview mode
123 if (! $params{preview}) {
124 my @blob = $im->ImageToBlob();
125 writefile($imglink, $config{destdir}, $blob[0], 1);
135 $dwidth = $im->Get("width");
136 $dheight = $im->Get("height");
139 if (! defined($dwidth) || ! defined($dheight)) {
140 error sprintf(gettext("failed to determine size of image %s"), $file)
143 my ($fileurl, $imgurl);
144 if (! $params{preview}) {
145 $fileurl=urlto($file, $params{destpage});
146 $imgurl=urlto($imglink, $params{destpage});
149 $fileurl="$config{url}/$file";
150 $imgurl="$config{url}/$imglink";
153 my $imgtag='<img src="'.$imgurl.
155 '" height="'.$dheight.'"'.
156 (exists $params{alt} ? ' alt="'.$params{alt}.'"' : '').
157 (exists $params{title} ? ' title="'.$params{title}.'"' : '').
158 (exists $params{class} ? ' class="'.$params{class}.'"' : '').
159 (exists $params{id} ? ' id="'.$params{id}.'"' : '').
162 my $alignclass=exists $params{align} ? "align-$params{align}" : "";
165 if (! defined $params{link}) {
168 elsif ($params{link} =~ /^\w+:\/\//) {
173 $imgtag='<a href="'.$link.'"'.
174 (!exists $params{caption} && $alignclass ? ' class="'.$alignclass.'"' : '').
178 my $b = bestlink($params{page}, $params{link});
181 add_depends($params{page}, $b, deptype("presence"));
182 $imgtag=htmllink($params{page}, $params{destpage},
183 $params{link}, linktext => $imgtag,
185 (!exists $params{caption} && $alignclass) ?
186 (class => $alignclass) : (),
191 if (exists $params{caption}) {
192 return '<table class="img'.
193 ($alignclass ? ' '.$alignclass : '').
195 '<caption>'.$params{caption}.'</caption>'.
196 '<tr><td>'.$imgtag.'</td></tr>'.