summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/img.pm
blob: eb1b6812482dab4065f40dbe6b8a4ebc96726a49 (plain)
  1. #!/usr/bin/perl
  2. # Ikiwiki enhanced image handling plugin
  3. # Christian Mock cm@tahina.priv.at 20061002
  4. package IkiWiki::Plugin::img;
  5. use warnings;
  6. use strict;
  7. use IkiWiki 3.00;
  8. my %imgdefaults;
  9. sub import {
  10. hook(type => "getsetup", id => "img", call => \&getsetup);
  11. hook(type => "preprocess", id => "img", call => \&preprocess, scan => 1);
  12. }
  13. sub getsetup () {
  14. return
  15. plugin => {
  16. safe => 1,
  17. rebuild => undef,
  18. section => "widget",
  19. },
  20. }
  21. sub preprocess (@) {
  22. my ($image) = $_[0] =~ /$config{wiki_file_regexp}/; # untaint
  23. my %params=@_;
  24. if (! defined $image) {
  25. error("bad image filename");
  26. }
  27. if (exists $imgdefaults{$params{page}}) {
  28. foreach my $key (keys %{$imgdefaults{$params{page}}}) {
  29. if (! exists $params{$key}) {
  30. $params{$key}=$imgdefaults{$params{page}}->{$key};
  31. }
  32. }
  33. }
  34. if (! exists $params{size} || ! length $params{size}) {
  35. $params{size}='full';
  36. }
  37. if ($image eq 'defaults') {
  38. $imgdefaults{$params{page}} = \%params;
  39. return '';
  40. }
  41. add_link($params{page}, $image);
  42. add_depends($params{page}, $image);
  43. # optimisation: detect scan mode, and avoid generating the image
  44. if (! defined wantarray) {
  45. return;
  46. }
  47. my $file = bestlink($params{page}, $image);
  48. my $srcfile = srcfile($file, 1);
  49. if (! length $file || ! defined $srcfile) {
  50. return htmllink($params{page}, $params{destpage}, $image);
  51. }
  52. my $dir = $params{page};
  53. my $base = IkiWiki::basename($file);
  54. eval q{use Image::Magick};
  55. error gettext("Image::Magick is not installed") if $@;
  56. my $im = Image::Magick->new;
  57. my $imglink;
  58. my $r = $im->Read($srcfile);
  59. error sprintf(gettext("failed to read %s: %s"), $file, $r) if $r;
  60. my ($dwidth, $dheight);
  61. if ($params{size} ne 'full') {
  62. my ($w, $h) = ($params{size} =~ /^(\d*)x(\d*)$/);
  63. error sprintf(gettext('wrong size format "%s" (should be WxH)'), $params{size})
  64. unless (defined $w && defined $h &&
  65. (length $w || length $h));
  66. if ((length $w && $w > $im->Get("width")) ||
  67. (length $h && $h > $im->Get("height"))) {
  68. # resizing larger
  69. $imglink = $file;
  70. # don't generate larger image, just set display size
  71. if (length $w && length $h) {
  72. ($dwidth, $dheight)=($w, $h);
  73. }
  74. # avoid division by zero on 0x0 image
  75. elsif ($im->Get("width") == 0 || $im->Get("height") == 0) {
  76. ($dwidth, $dheight)=(0, 0);
  77. }
  78. # calculate unspecified size from the other one, preserving
  79. # aspect ratio
  80. elsif (length $w) {
  81. $dwidth=$w;
  82. $dheight=$w / $im->Get("width") * $im->Get("height");
  83. }
  84. elsif (length $h) {
  85. $dheight=$h;
  86. $dwidth=$h / $im->Get("height") * $im->Get("width");
  87. }
  88. }
  89. else {
  90. # resizing smaller
  91. my $outfile = "$config{destdir}/$dir/${w}x${h}-$base";
  92. $imglink = "$dir/${w}x${h}-$base";
  93. will_render($params{page}, $imglink);
  94. if (-e $outfile && (-M $srcfile >= -M $outfile)) {
  95. $im = Image::Magick->new;
  96. $r = $im->Read($outfile);
  97. error sprintf(gettext("failed to read %s: %s"), $outfile, $r) if $r;
  98. }
  99. else {
  100. ($dwidth, $dheight)=($w, $h);
  101. $r = $im->Resize(geometry => "${w}x${h}");
  102. error sprintf(gettext("failed to resize: %s"), $r) if $r;
  103. # don't actually write resized file in preview mode;
  104. # rely on width and height settings
  105. if (! $params{preview}) {
  106. my @blob = $im->ImageToBlob();
  107. writefile($imglink, $config{destdir}, $blob[0], 1);
  108. }
  109. else {
  110. $imglink = $file;
  111. }
  112. }
  113. $dwidth = $im->Get("width") unless defined $dwidth;
  114. $dheight = $im->Get("height") unless defined $dheight;
  115. }
  116. }
  117. else {
  118. $imglink = $file;
  119. $dwidth = $im->Get("width");
  120. $dheight = $im->Get("height");
  121. }
  122. if (! defined($dwidth) || ! defined($dheight)) {
  123. error sprintf(gettext("failed to determine size of image %s"), $file)
  124. }
  125. my ($fileurl, $imgurl);
  126. if (! $params{preview}) {
  127. $fileurl=urlto($file, $params{destpage});
  128. $imgurl=urlto($imglink, $params{destpage});
  129. }
  130. else {
  131. $fileurl="$config{url}/$file";
  132. $imgurl="$config{url}/$imglink";
  133. }
  134. my $attrs='';
  135. foreach my $attr (qw{alt title class id hspace vspace}) {
  136. if (exists $params{$attr}) {
  137. $attrs.=" $attr=\"$params{$attr}\"";
  138. }
  139. }
  140. my $imgtag='<img src="'.$imgurl.
  141. '" width="'.$dwidth.
  142. '" height="'.$dheight.'"'.
  143. $attrs.
  144. (exists $params{align} && ! exists $params{caption} ? ' align="'.$params{align}.'"' : '').
  145. ' />';
  146. my $link;
  147. if (! defined $params{link}) {
  148. $link=$fileurl;
  149. }
  150. elsif ($params{link} =~ /^\w+:\/\//) {
  151. $link=$params{link};
  152. }
  153. if (defined $link) {
  154. $imgtag='<a href="'.$link.'">'.$imgtag.'</a>';
  155. }
  156. else {
  157. my $b = bestlink($params{page}, $params{link});
  158. if (length $b) {
  159. add_depends($params{page}, $b, deptype("presence"));
  160. $imgtag=htmllink($params{page}, $params{destpage},
  161. $params{link}, linktext => $imgtag,
  162. noimageinline => 1,
  163. );
  164. }
  165. }
  166. if (exists $params{caption}) {
  167. return '<table class="img'.
  168. (exists $params{align} ? " align-$params{align}" : "").
  169. '">'.
  170. '<caption>'.$params{caption}.'</caption>'.
  171. '<tr><td>'.$imgtag.'</td></tr>'.
  172. '</table>';
  173. }
  174. else {
  175. return $imgtag;
  176. }
  177. }
  178. 1