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 (! defined $image) {
30 error("bad image filename");
33 if (exists $imgdefaults{$params{page}}) {
34 foreach my $key (keys %{$imgdefaults{$params{page}}}) {
35 if (! exists $params{$key}) {
36 $params{$key}=$imgdefaults{$params{page}}->{$key};
41 if (! exists $params{size}) {
45 if ($image eq 'defaults') {
46 $imgdefaults{$params{page}} = \%params;
50 add_link($params{page}, $image);
51 add_depends($params{page}, $image);
53 # optimisation: detect scan mode, and avoid generating the image
54 if (! defined wantarray) {
58 my $file = bestlink($params{page}, $image);
59 my $srcfile = srcfile($file, 1);
60 if (! length $file || ! defined $srcfile) {
61 return htmllink($params{page}, $params{destpage}, $image);
64 my $dir = $params{page};
65 my $base = IkiWiki::basename($file);
67 eval q{use Image::Magick};
68 error gettext("Image::Magick is not installed") if $@;
69 my $im = Image::Magick->new;
71 my $r = $im->Read($srcfile);
72 error sprintf(gettext("failed to read %s: %s"), $file, $r) if $r;
74 my ($dwidth, $dheight);
76 if ($params{size} ne 'full') {
77 my ($w, $h) = ($params{size} =~ /^(\d*)x(\d*)$/);
78 error sprintf(gettext('wrong size format "%s" (should be WxH)'), $params{size})
79 unless (defined $w && defined $h &&
80 (length $w || length $h));
82 if ((length $w && $w > $im->Get("width")) ||
83 (length $h && $h > $im->Get("height"))) {
87 # don't generate larger image, just set display size
88 if (length $w && length $h) {
89 ($dwidth, $dheight)=($w, $h);
91 # avoid division by zero on 0x0 image
92 elsif ($im->Get("width") == 0 || $im->Get("height") == 0) {
93 ($dwidth, $dheight)=(0, 0);
95 # calculate unspecified size from the other one, preserving
99 $dheight=$w / $im->Get("width") * $im->Get("height");
103 $dwidth=$h / $im->Get("height") * $im->Get("width");
108 my $outfile = "$config{destdir}/$dir/${w}x${h}-$base";
109 $imglink = "$dir/${w}x${h}-$base";
111 will_render($params{page}, $imglink);
113 if (-e $outfile && (-M $srcfile >= -M $outfile)) {
114 $im = Image::Magick->new;
115 $r = $im->Read($outfile);
116 error sprintf(gettext("failed to read %s: %s"), $outfile, $r) if $r;
118 $dwidth = $im->Get("width");
119 $dheight = $im->Get("height");
122 ($dwidth, $dheight)=($w, $h);
123 $r = $im->Resize(geometry => "${w}x${h}");
124 error sprintf(gettext("failed to resize: %s"), $r) if $r;
126 # don't actually write file in preview mode
127 if (! $params{preview}) {
128 my @blob = $im->ImageToBlob();
129 writefile($imglink, $config{destdir}, $blob[0], 1);
139 $dwidth = $im->Get("width");
140 $dheight = $im->Get("height");
143 if (! defined($dwidth) || ! defined($dheight)) {
144 error sprintf(gettext("failed to determine size of image %s"), $file)
147 my ($fileurl, $imgurl);
148 if (! $params{preview}) {
149 $fileurl=urlto($file, $params{destpage});
150 $imgurl=urlto($imglink, $params{destpage});
153 $fileurl="$config{url}/$file";
154 $imgurl="$config{url}/$imglink";
157 my $imgtag='<img src="'.$imgurl.
159 '" height="'.$dheight.'"'.
160 (exists $params{alt} ? ' alt="'.$params{alt}.'"' : '').
161 (exists $params{title} ? ' title="'.$params{title}.'"' : '').
162 (exists $params{class} ? ' class="'.$params{class}.'"' : '').
163 (exists $params{align} && ! exists $params{caption} ? ' align="'.$params{align}.'"' : '').
164 (exists $params{id} ? ' id="'.$params{id}.'"' : '').
168 if (! defined $params{link}) {
171 elsif ($params{link} =~ /^\w+:\/\//) {
176 $imgtag='<a href="'.$link.'">'.$imgtag.'</a>';
179 my $b = bestlink($params{page}, $params{link});
182 add_depends($params{page}, $b, deptype("presence"));
183 $imgtag=htmllink($params{page}, $params{destpage},
184 $params{link}, linktext => $imgtag,
190 if (exists $params{caption}) {
191 return '<table class="img'.
192 (exists $params{align} ? " align-$params{align}" : "").
194 '<caption>'.$params{caption}.'</caption>'.
195 '<tr><td>'.$imgtag.'</td></tr>'.