summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/img.pm
blob: f06121578266bedc92027a0624cd05a21cdf6c0f (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}) {
  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. $dwidth = $im->Get("width");
  99. $dheight = $im->Get("height");
  100. }
  101. else {
  102. ($dwidth, $dheight)=($w, $h);
  103. $r = $im->Resize(geometry => "${w}x${h}");
  104. error sprintf(gettext("failed to resize: %s"), $r) if $r;
  105. # don't actually write file in preview mode
  106. if (! $params{preview}) {
  107. my @blob = $im->ImageToBlob();
  108. writefile($imglink, $config{destdir}, $blob[0], 1);
  109. }
  110. else {
  111. $imglink = $file;
  112. }
  113. }
  114. }
  115. }
  116. else {
  117. $imglink = $file;
  118. $dwidth = $im->Get("width");
  119. $dheight = $im->Get("height");
  120. }
  121. if (! defined($dwidth) || ! defined($dheight)) {
  122. error sprintf(gettext("failed to determine size of image %s"), $file)
  123. }
  124. my ($fileurl, $imgurl);
  125. if (! $params{preview}) {
  126. $fileurl=urlto($file, $params{destpage});
  127. $imgurl=urlto($imglink, $params{destpage});
  128. }
  129. else {
  130. $fileurl="$config{url}/$file";
  131. $imgurl="$config{url}/$imglink";
  132. }
  133. my $imgtag='<img src="'.$imgurl.
  134. '" width="'.$dwidth.
  135. '" height="'.$dheight.'"'.
  136. (exists $params{alt} ? ' alt="'.$params{alt}.'"' : '').
  137. (exists $params{title} ? ' title="'.$params{title}.'"' : '').
  138. (exists $params{class} ? ' class="'.$params{class}.'"' : '').
  139. (exists $params{align} && ! exists $params{caption} ? ' align="'.$params{align}.'"' : '').
  140. (exists $params{id} ? ' id="'.$params{id}.'"' : '').
  141. ' />';
  142. my $link;
  143. if (! defined $params{link}) {
  144. $link=$fileurl;
  145. }
  146. elsif ($params{link} =~ /^\w+:\/\//) {
  147. $link=$params{link};
  148. }
  149. if (defined $link) {
  150. $imgtag='<a href="'.$link.'">'.$imgtag.'</a>';
  151. }
  152. else {
  153. my $b = bestlink($params{page}, $params{link});
  154. if (length $b) {
  155. add_depends($params{page}, $b, deptype("presence"));
  156. $imgtag=htmllink($params{page}, $params{destpage},
  157. $params{link}, linktext => $imgtag,
  158. noimageinline => 1,
  159. );
  160. }
  161. }
  162. if (exists $params{caption}) {
  163. return '<table class="img'.
  164. (exists $params{align} ? " align-$params{align}" : "").
  165. '">'.
  166. '<caption>'.$params{caption}.'</caption>'.
  167. '<tr><td>'.$imgtag.'</td></tr>'.
  168. '</table>';
  169. }
  170. else {
  171. return $imgtag;
  172. }
  173. }
  174. 1