summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/img.pm
blob: 103f6b2b3510067f10a5eddf86f8e657ba96fe29 (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. 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. }
  99. else {
  100. ($dwidth, $dheight)=($w, $h);
  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. $dwidth = $im->Get("width") unless defined $dwidth;
  114. $dheight = $im->Get("height") unless defined $dheight;
  115. }
  116. }
  117. else {
  118. $imglink = $file;
  119. $dwidth = $im->Get("width");
  120. $dheight = $im->Get("height");
  121. }
  122. if (! defined($dwidth) || ! defined($dheight)) {
  123. error sprintf(gettext("failed to determine size of image %s"), $file)
  124. }
  125. my ($fileurl, $imgurl);
  126. if (! $params{preview}) {
  127. $fileurl=urlto($file, $params{destpage});
  128. $imgurl=urlto($imglink, $params{destpage});
  129. }
  130. else {
  131. $fileurl=urlto($file);
  132. $imgurl=urlto($imglink);
  133. }
  134. if (! exists $params{class}) {
  135. $params{class}="img";
  136. }
  137. my $attrs='';
  138. foreach my $attr (qw{alt title class id hspace vspace}) {
  139. if (exists $params{$attr}) {
  140. $attrs.=" $attr=\"$params{$attr}\"";
  141. }
  142. }
  143. my $imgtag='<img src="'.$imgurl.
  144. '" width="'.$dwidth.
  145. '" height="'.$dheight.'"'.
  146. $attrs.
  147. (exists $params{align} && ! exists $params{caption} ? ' align="'.$params{align}.'"' : '').
  148. ' />';
  149. my $link;
  150. if (! defined $params{link}) {
  151. $link=$fileurl;
  152. }
  153. elsif ($params{link} =~ /^\w+:\/\//) {
  154. $link=$params{link};
  155. }
  156. if (defined $link) {
  157. $imgtag='<a href="'.$link.'">'.$imgtag.'</a>';
  158. }
  159. else {
  160. my $b = bestlink($params{page}, $params{link});
  161. if (length $b) {
  162. add_depends($params{page}, $b, deptype("presence"));
  163. $imgtag=htmllink($params{page}, $params{destpage},
  164. $params{link}, linktext => $imgtag,
  165. noimageinline => 1,
  166. );
  167. }
  168. }
  169. if (exists $params{caption}) {
  170. return '<table class="img'.
  171. (exists $params{align} ? " align-$params{align}" : "").
  172. '">'.
  173. '<caption>'.$params{caption}.'</caption>'.
  174. '<tr><td>'.$imgtag.'</td></tr>'.
  175. '</table>';
  176. }
  177. else {
  178. return $imgtag;
  179. }
  180. }
  181. 1