summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/img.pm
blob: 9ae85c4e682839d7a2179785e1c4e504d6ecc866 (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. },
  19. }
  20. sub preprocess (@) {
  21. my ($image) = $_[0] =~ /$config{wiki_file_regexp}/; # untaint
  22. my %params=@_;
  23. if (exists $imgdefaults{$params{page}}) {
  24. foreach my $key (keys %{$imgdefaults{$params{page}}}) {
  25. if (! exists $params{$key}) {
  26. $params{$key}=$imgdefaults{$params{page}}->{$key};
  27. }
  28. }
  29. }
  30. if (! exists $params{size}) {
  31. $params{size}='full';
  32. }
  33. if ($image eq 'defaults') {
  34. $imgdefaults{$params{page}} = \%params;
  35. return '';
  36. }
  37. add_link($params{page}, $image);
  38. # optimisation: detect scan mode, and avoid generating the image
  39. if (! defined wantarray) {
  40. return;
  41. }
  42. my $file = bestlink($params{page}, $image);
  43. my $srcfile = srcfile($file, 1);
  44. if (! length $file || ! defined $srcfile) {
  45. return htmllink($params{page}, $params{destpage}, $image);
  46. }
  47. my $dir = $params{page};
  48. my $base = IkiWiki::basename($file);
  49. eval q{use Image::Magick};
  50. error gettext("Image::Magick is not installed") if $@;
  51. my $im = Image::Magick->new;
  52. my $imglink;
  53. my $r;
  54. my ($dwidth, $dheight);
  55. if ($params{size} ne 'full') {
  56. add_depends($params{page}, $image);
  57. my ($w, $h) = ($params{size} =~ /^(\d*)x(\d*)$/);
  58. error sprintf(gettext('wrong size format "%s" (should be WxH)'), $params{size})
  59. unless (defined $w && defined $h &&
  60. (length $w || length $h));
  61. my $outfile = "$config{destdir}/$dir/${w}x${h}-$base";
  62. $imglink = "$dir/${w}x${h}-$base";
  63. will_render($params{page}, $imglink);
  64. if (-e $outfile && (-M $srcfile >= -M $outfile)) {
  65. $r = $im->Read($outfile);
  66. error sprintf(gettext("failed to read %s: %s"), $outfile, $r) if $r;
  67. }
  68. else {
  69. $r = $im->Read($srcfile);
  70. error sprintf(gettext("failed to read %s: %s"), $file, $r) if $r;
  71. # don't resize any larger
  72. my ($rw, $rh) = ($w, $h);
  73. if ((length $rw && $rw > $im->Get("width")) ||
  74. (length $rh && $rh > $im->Get("height"))) {
  75. $rw=$im->Get("width");
  76. $rh=$im->Get("height");
  77. }
  78. $r = $im->Resize(geometry => "${rw}x${rh}");
  79. error sprintf(gettext("failed to resize: %s"), $r) if $r;
  80. # don't actually write file in preview mode
  81. if (! $params{preview}) {
  82. my @blob = $im->ImageToBlob();
  83. writefile($imglink, $config{destdir}, $blob[0], 1);
  84. }
  85. else {
  86. $imglink = $file;
  87. }
  88. }
  89. # since we don't really resize larger, set the display
  90. # size, so the browser can scale the image up if necessary
  91. if (length $w && length $h) {
  92. ($dwidth, $dheight)=($w, $h);
  93. }
  94. # avoid division by zero on 0x0 image
  95. elsif ($im->Get("width") == 0 || $im->Get("height") == 0) {
  96. ($dwidth, $dheight)=(0, 0);
  97. }
  98. # calculate unspecified size from the other one, preserving
  99. # aspect ratio
  100. elsif (length $w) {
  101. $dwidth=$w;
  102. $dheight=$w / $im->Get("width") * $im->Get("height");
  103. }
  104. elsif (length $h) {
  105. $dheight=$h;
  106. $dwidth=$h / $im->Get("height") * $im->Get("width");
  107. }
  108. }
  109. else {
  110. $r = $im->Read($srcfile);
  111. error sprintf(gettext("failed to read %s: %s"), $file, $r) if $r;
  112. $imglink = $file;
  113. $dwidth = $im->Get("width");
  114. $dheight = $im->Get("height");
  115. }
  116. my ($fileurl, $imgurl);
  117. if (! $params{preview}) {
  118. $fileurl=urlto($file, $params{destpage});
  119. $imgurl=urlto($imglink, $params{destpage});
  120. }
  121. else {
  122. $fileurl="$config{url}/$file";
  123. $imgurl="$config{url}/$imglink";
  124. }
  125. if (! defined($im->Get("width")) || ! defined($im->Get("height"))) {
  126. error sprintf(gettext("failed to determine size of image %s"), $file)
  127. }
  128. my $imgtag='<img src="'.$imgurl.
  129. '" width="'.$dwidth.
  130. '" height="'.$dheight.'"'.
  131. (exists $params{alt} ? ' alt="'.$params{alt}.'"' : '').
  132. (exists $params{title} ? ' title="'.$params{title}.'"' : '').
  133. (exists $params{align} ? ' align="'.$params{align}.'"' : '').
  134. (exists $params{class} ? ' class="'.$params{class}.'"' : '').
  135. (exists $params{id} ? ' id="'.$params{id}.'"' : '').
  136. ' />';
  137. if (! defined $params{link} || lc($params{link}) eq 'yes') {
  138. $imgtag='<a href="'.$fileurl.'">'.$imgtag.'</a>';
  139. }
  140. elsif ($params{link} =~ /^\w+:\/\//) {
  141. $imgtag='<a href="'.$params{link}.'">'.$imgtag.'</a>';
  142. }
  143. else {
  144. my $b = bestlink($params{page}, $params{link});
  145. if (length $b) {
  146. add_depends($params{page}, $b);
  147. $imgtag=htmllink($params{page}, $params{destpage},
  148. $params{link}, linktext => $imgtag,
  149. noimageinline => 1);
  150. }
  151. }
  152. if (exists $params{caption}) {
  153. return '<table class="img">'.
  154. '<caption>'.$params{caption}.'</caption>'.
  155. '<tr><td>'.$imgtag.'</td></tr>'.
  156. '</table>';
  157. }
  158. else {
  159. return $imgtag;
  160. }
  161. }
  162. 1