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);
17 sub getsetup () { #{{{
25 sub preprocess (@) { #{{{
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 push @{$links{$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('bad size "%s"'), $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 '" alt="'.(exists $params{alt} ? $params{alt} : '').
123 '" width="'.$im->Get("width").
124 '" height="'.$im->Get("height").'"'.
125 (exists $params{title} ? ' title="'.$params{title}.'"' : '').
126 (exists $params{class} ? ' class="'.$params{class}.'"' : '').
127 (exists $params{id} ? ' id="'.$params{id}.'"' : '').
130 if (! defined $params{link} || lc($params{link}) eq 'yes') {
131 $imgtag='<a href="'.$fileurl.'">'.$imgtag.'</a>';
133 elsif ($params{link} =~ /^\w+:\/\//) {
134 $imgtag='<a href="'.$params{link}.'">'.$imgtag.'</a>';
136 elsif (length bestlink($params{page}, $params{link})) {
137 add_depends($params{page}, $params{link});
138 $imgtag=htmllink($params{page}, $params{destpage},
139 $params{link}, linktext => $imgtag,
143 if (exists $params{caption}) {
144 return '<table class="img">'.
145 '<caption>'.$params{caption}.'</caption>'.
146 '<tr><td>'.$imgtag.'</td></tr>'.