summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/img.pm
blob: b92e24cc06fb93d8d88adcf1517903e46e49dc25 (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. my $issvg = $base=~s/\.svg$/.png/i;
  55. eval q{use Image::Magick};
  56. error gettext("Image::Magick is not installed") if $@;
  57. my $im = Image::Magick->new($issvg ? (magick => "png") : ());
  58. my $imglink;
  59. my $r = $im->Read($srcfile);
  60. error sprintf(gettext("failed to read %s: %s"), $file, $r) if $r;
  61. my ($dwidth, $dheight);
  62. if ($params{size} ne 'full') {
  63. my ($w, $h) = ($params{size} =~ /^(\d*)x(\d*)$/);
  64. error sprintf(gettext('wrong size format "%s" (should be WxH)'), $params{size})
  65. unless (defined $w && defined $h &&
  66. (length $w || length $h));
  67. if ((length $w && $w > $im->Get("width")) ||
  68. (length $h && $h > $im->Get("height"))) {
  69. # resizing larger
  70. $imglink = $file;
  71. # don't generate larger image, just set display size
  72. if (length $w && length $h) {
  73. ($dwidth, $dheight)=($w, $h);
  74. }
  75. # avoid division by zero on 0x0 image
  76. elsif ($im->Get("width") == 0 || $im->Get("height") == 0) {
  77. ($dwidth, $dheight)=(0, 0);
  78. }
  79. # calculate unspecified size from the other one, preserving
  80. # aspect ratio
  81. elsif (length $w) {
  82. $dwidth=$w;
  83. $dheight=$w / $im->Get("width") * $im->Get("height");
  84. }
  85. elsif (length $h) {
  86. $dheight=$h;
  87. $dwidth=$h / $im->Get("height") * $im->Get("width");
  88. }
  89. }
  90. else {
  91. # resizing smaller
  92. my $outfile = "$config{destdir}/$dir/${w}x${h}-$base";
  93. $imglink = "$dir/${w}x${h}-$base";
  94. will_render($params{page}, $imglink);
  95. if (-e $outfile && (-M $srcfile >= -M $outfile)) {
  96. $im = Image::Magick->new;
  97. $r = $im->Read($outfile);
  98. error sprintf(gettext("failed to read %s: %s"), $outfile, $r) if $r;
  99. }
  100. else {
  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. # always get the true size of the resized image
  114. $dwidth = $im->Get("width");
  115. $dheight = $im->Get("height");
  116. }
  117. }
  118. else {
  119. $imglink = $file;
  120. $dwidth = $im->Get("width");
  121. $dheight = $im->Get("height");
  122. }
  123. if (! defined($dwidth) || ! defined($dheight)) {
  124. error sprintf(gettext("failed to determine size of image %s"), $file)
  125. }
  126. my ($fileurl, $imgurl);
  127. if (! $params{preview}) {
  128. $fileurl=urlto($file, $params{destpage});
  129. $imgurl=urlto($imglink, $params{destpage});
  130. }
  131. else {
  132. $fileurl=urlto($file);
  133. $imgurl=urlto($imglink);
  134. }
  135. if (! exists $params{class}) {
  136. $params{class}="img";
  137. }
  138. my $attrs='';
  139. foreach my $attr (qw{alt title class id hspace vspace}) {
  140. if (exists $params{$attr}) {
  141. $attrs.=" $attr=\"$params{$attr}\"";
  142. }
  143. }
  144. my $imgtag='<img src="'.$imgurl.
  145. '" width="'.$dwidth.
  146. '" height="'.$dheight.'"'.
  147. $attrs.
  148. (exists $params{align} && ! exists $params{caption} ? ' align="'.$params{align}.'"' : '').
  149. ' />';
  150. my $link;
  151. if (! defined $params{link}) {
  152. $link=$fileurl;
  153. }
  154. elsif ($params{link} =~ /^\w+:\/\//) {
  155. $link=$params{link};
  156. }
  157. if (defined $link) {
  158. $imgtag='<a href="'.$link.'">'.$imgtag.'</a>';
  159. }
  160. else {
  161. my $b = bestlink($params{page}, $params{link});
  162. if (length $b) {
  163. add_depends($params{page}, $b, deptype("presence"));
  164. $imgtag=htmllink($params{page}, $params{destpage},
  165. $params{link}, linktext => $imgtag,
  166. noimageinline => 1,
  167. );
  168. }
  169. }
  170. if (exists $params{caption}) {
  171. return '<table class="img'.
  172. (exists $params{align} ? " align-$params{align}" : "").
  173. '">'.
  174. '<caption>'.$params{caption}.'</caption>'.
  175. '<tr><td>'.$imgtag.'</td></tr>'.
  176. '</table>';
  177. }
  178. else {
  179. return $imgtag;
  180. }
  181. }
  182. 1