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