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