summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/img.pm
blob: 740c44f96045e19e5ed4f79dcca8db4369feeae6 (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. img_allowed_formats => {
  21. type => "string",
  22. default => [qw(jpeg png gif svg)],
  23. description => "Image formats to process (jpeg, png, gif, svg, pdf or 'everything' to accept all)",
  24. # ImageMagick has had arbitrary code execution flaws,
  25. # and the whole delegates mechanism is scary from
  26. # that perspective
  27. safe => 0,
  28. rebuild => 0,
  29. },
  30. }
  31. sub allowed {
  32. my $format = shift;
  33. my $allowed = $config{img_allowed_formats};
  34. $allowed = ['jpeg', 'png', 'gif', 'svg'] unless defined $allowed && @$allowed;
  35. foreach my $a (@$allowed) {
  36. return 1 if lc($a) eq $format || lc($a) eq 'everything';
  37. }
  38. return 0;
  39. }
  40. sub preprocess (@) {
  41. my ($image) = $_[0] =~ /$config{wiki_file_regexp}/; # untaint
  42. my %params=@_;
  43. if (! defined $image) {
  44. error("bad image filename");
  45. }
  46. if (exists $imgdefaults{$params{page}}) {
  47. foreach my $key (keys %{$imgdefaults{$params{page}}}) {
  48. if (! exists $params{$key}) {
  49. $params{$key}=$imgdefaults{$params{page}}->{$key};
  50. }
  51. }
  52. }
  53. if (! exists $params{size} || ! length $params{size}) {
  54. $params{size}='full';
  55. }
  56. if ($image eq 'defaults') {
  57. $imgdefaults{$params{page}} = \%params;
  58. return '';
  59. }
  60. add_link($params{page}, $image);
  61. add_depends($params{page}, $image);
  62. # optimisation: detect scan mode, and avoid generating the image
  63. if (! defined wantarray) {
  64. return;
  65. }
  66. my $file = bestlink($params{page}, $image);
  67. my $srcfile = srcfile($file, 1);
  68. if (! length $file || ! defined $srcfile) {
  69. return htmllink($params{page}, $params{destpage}, $image);
  70. }
  71. my $dir = $params{page};
  72. my $base = IkiWiki::basename($file);
  73. my $extension;
  74. my $format;
  75. if ($base =~ m/\.([a-z0-9]+)$/is) {
  76. $extension = $1;
  77. }
  78. else {
  79. error gettext("Unable to detect image type from extension");
  80. }
  81. # Never interpret well-known file extensions as any other format,
  82. # in case the wiki configuration unwisely allows attaching
  83. # arbitrary files named *.jpg, etc.
  84. my $magic;
  85. my $offset = 0;
  86. open(my $in, '<', $srcfile) or error sprintf(gettext("failed to read %s: %s"), $file, $!);
  87. binmode($in);
  88. if ($extension =~ m/^(jpeg|jpg)$/is) {
  89. $format = 'jpeg';
  90. $magic = "\377\330\377";
  91. }
  92. elsif ($extension =~ m/^(png)$/is) {
  93. $format = 'png';
  94. $magic = "\211PNG\r\n\032\n";
  95. }
  96. elsif ($extension =~ m/^(gif)$/is) {
  97. $format = 'gif';
  98. $magic = "GIF8";
  99. }
  100. elsif ($extension =~ m/^(svg)$/is) {
  101. $format = 'svg';
  102. }
  103. elsif ($extension =~ m/^(pdf)$/is) {
  104. $format = 'pdf';
  105. $magic = "%PDF-";
  106. }
  107. else {
  108. # allow ImageMagick to auto-detect (potentially dangerous)
  109. $format = '';
  110. }
  111. error sprintf(gettext("%s image processing disabled in img_allowed_formats configuration"), $format ? $format : "\"$extension\"") unless allowed($format ? $format : "everything");
  112. # Try harder to protect ImageMagick from itself
  113. if (defined $magic) {
  114. my $content;
  115. read($in, $content, length $magic) or error sprintf(gettext("failed to read %s: %s"), $file, $!);
  116. if ($magic ne $content) {
  117. error sprintf(gettext("\"%s\" does not seem to be a valid %s file"), $file, $format);
  118. }
  119. }
  120. my $ispdf = $base=~s/\.pdf$/.png/i;
  121. my $pagenumber = exists($params{pagenumber}) ? int($params{pagenumber}) : 0;
  122. if ($pagenumber != 0) {
  123. $base = "p$pagenumber-$base";
  124. }
  125. my $imglink;
  126. my $imgdatalink;
  127. my ($dwidth, $dheight);
  128. my ($w, $h);
  129. if ($params{size} ne 'full') {
  130. ($w, $h) = ($params{size} =~ /^(\d*)x(\d*)$/);
  131. }
  132. if ($format eq 'svg') {
  133. # svg images are not scaled using ImageMagick because the
  134. # pipeline is complex. Instead, the image size is simply
  135. # set to the provided values.
  136. #
  137. # Aspect ratio will be preserved automatically when
  138. # only a width or only a height is specified.
  139. # When both are specified, aspect ratio will not be
  140. # preserved.
  141. $imglink = $file;
  142. $dwidth = $w if length $w;
  143. $dheight = $h if length $h;
  144. }
  145. else {
  146. eval q{use Image::Magick};
  147. error gettext("Image::Magick is not installed") if $@;
  148. my $im = Image::Magick->new();
  149. my $r = $im->Read("$format:$srcfile\[$pagenumber]");
  150. error sprintf(gettext("failed to read %s: %s"), $file, $r) if $r;
  151. if (! defined $im->Get("width") || ! defined $im->Get("height")) {
  152. error sprintf(gettext("failed to get dimensions of %s"), $file);
  153. }
  154. if (! length $w && ! length $h) {
  155. $dwidth = $im->Get("width");
  156. $dheight = $im->Get("height");
  157. } else {
  158. error sprintf(gettext('wrong size format "%s" (should be WxH)'), $params{size})
  159. unless (defined $w && defined $h &&
  160. (length $w || length $h));
  161. if ($im->Get("width") == 0 || $im->Get("height") == 0) {
  162. ($dwidth, $dheight)=(0, 0);
  163. } elsif (! length $w || (length $h && $im->Get("height")*$w > $h * $im->Get("width"))) {
  164. # using height because only height is given or ...
  165. # because original image is more portrait than $w/$h
  166. # ... slimness of $im > $h/w
  167. # ... $im->Get("height")/$im->Get("width") > $h/$w
  168. # ... $im->Get("height")*$w > $h * $im->Get("width")
  169. $dheight=$h;
  170. $dwidth=$h / $im->Get("height") * $im->Get("width");
  171. } else { # (! length $h) or $w is what determines the resized size
  172. $dwidth=$w;
  173. $dheight=$w / $im->Get("width") * $im->Get("height");
  174. }
  175. }
  176. if ($dwidth < $im->Get("width") || $ispdf) {
  177. # resize down, or resize to pixels at all
  178. my $outfile = "$config{destdir}/$dir/$params{size}-$base";
  179. $imglink = "$dir/$params{size}-$base";
  180. will_render($params{page}, $imglink);
  181. if (-e $outfile && (-M $srcfile >= -M $outfile)) {
  182. $im = Image::Magick->new;
  183. $r = $im->Read($outfile);
  184. error sprintf(gettext("failed to read %s: %s"), $outfile, $r) if $r;
  185. }
  186. else {
  187. $r = $im->Resize(geometry => "${dwidth}x${dheight}");
  188. error sprintf(gettext("failed to resize: %s"), $r) if $r;
  189. $im->set($ispdf ? (magick => 'png') : ());
  190. my @blob = $im->ImageToBlob();
  191. # don't actually write resized file in preview mode;
  192. # rely on width and height settings
  193. if (! $params{preview}) {
  194. writefile($imglink, $config{destdir}, $blob[0], 1);
  195. }
  196. else {
  197. eval q{use MIME::Base64};
  198. error($@) if $@;
  199. $imgdatalink = "data:image/".$im->Get("magick").";base64,".encode_base64($blob[0]);
  200. }
  201. }
  202. # always get the true size of the resized image (it could be
  203. # that imagemagick did its calculations differently)
  204. $dwidth = $im->Get("width");
  205. $dheight = $im->Get("height");
  206. } else {
  207. $imglink = $file;
  208. }
  209. if (! defined($dwidth) || ! defined($dheight)) {
  210. error sprintf(gettext("failed to determine size of image %s"), $file)
  211. }
  212. }
  213. my ($fileurl, $imgurl);
  214. my $urltobase = $params{preview} ? undef : $params{destpage};
  215. $fileurl=urlto($file, $urltobase);
  216. $imgurl=$imgdatalink ? $imgdatalink : urlto($imglink, $urltobase);
  217. if (! exists $params{class}) {
  218. $params{class}="img";
  219. }
  220. my $attrs='';
  221. foreach my $attr (qw{alt title class id hspace vspace}) {
  222. if (exists $params{$attr}) {
  223. $attrs.=" $attr=\"$params{$attr}\"";
  224. }
  225. }
  226. my $imgtag='<img src="'.$imgurl.'"';
  227. $imgtag.=' width="'.$dwidth.'"' if defined $dwidth;
  228. $imgtag.=' height="'.$dheight.'"' if defined $dheight;
  229. $imgtag.= $attrs.
  230. (exists $params{align} && ! exists $params{caption} ? ' align="'.$params{align}.'"' : '').
  231. ' />';
  232. my $link;
  233. if (! defined $params{link}) {
  234. $link=$fileurl;
  235. }
  236. elsif ($params{link} =~ /^\w+:\/\//) {
  237. $link=$params{link};
  238. }
  239. if (defined $link) {
  240. $imgtag='<a href="'.$link.'">'.$imgtag.'</a>';
  241. }
  242. else {
  243. my $b = bestlink($params{page}, $params{link});
  244. if (length $b) {
  245. add_depends($params{page}, $b, deptype("presence"));
  246. $imgtag=htmllink($params{page}, $params{destpage},
  247. $params{link}, linktext => $imgtag,
  248. noimageinline => 1,
  249. );
  250. }
  251. }
  252. if (exists $params{caption}) {
  253. return '<table class="img'.
  254. (exists $params{align} ? " align-$params{align}" : "").
  255. '">'.
  256. '<caption>'.$params{caption}.'</caption>'.
  257. '<tr><td>'.$imgtag.'</td></tr>'.
  258. '</table>';
  259. }
  260. else {
  261. return $imgtag;
  262. }
  263. }
  264. 1