summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/img.pm
blob: b85824345ed6e1634cf6f43b08ac3887a56117d4 (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. my $im = Image::Magick->new();
  110. my $r = $im->Ping(file => $in);
  111. if ($r) {
  112. $format = lc $r;
  113. }
  114. else {
  115. error sprintf(gettext("failed to determine format of %s"), $file);
  116. }
  117. }
  118. error sprintf(gettext("%s image processing disabled in img_allowed_formats configuration"), $format ? $format : "\"$extension\"") unless allowed($format ? $format : "everything");
  119. # Try harder to protect ImageMagick from itself
  120. if (defined $magic) {
  121. my $content;
  122. read($in, $content, length $magic) or error sprintf(gettext("failed to read %s: %s"), $file, $!);
  123. if ($magic ne $content) {
  124. error sprintf(gettext("\"%s\" does not seem to be a valid %s file"), $file, $format);
  125. }
  126. }
  127. my $ispdf = $base=~s/\.pdf$/.png/i;
  128. my $pagenumber = exists($params{pagenumber}) ? int($params{pagenumber}) : 0;
  129. if ($pagenumber != 0) {
  130. $base = "p$pagenumber-$base";
  131. }
  132. my $imglink;
  133. my $imgdatalink;
  134. my ($dwidth, $dheight);
  135. my ($w, $h);
  136. if ($params{size} ne 'full') {
  137. ($w, $h) = ($params{size} =~ /^(\d*)x(\d*)$/);
  138. }
  139. if ($format eq 'svg') {
  140. # svg images are not scaled using ImageMagick because the
  141. # pipeline is complex. Instead, the image size is simply
  142. # set to the provided values.
  143. #
  144. # Aspect ratio will be preserved automatically when
  145. # only a width or only a height is specified.
  146. # When both are specified, aspect ratio will not be
  147. # preserved.
  148. $imglink = $file;
  149. $dwidth = $w if length $w;
  150. $dheight = $h if length $h;
  151. }
  152. else {
  153. eval q{use Image::Magick};
  154. error gettext("Image::Magick is not installed") if $@;
  155. my $im = Image::Magick->new();
  156. my $r = $im->Read("$format:$srcfile\[$pagenumber]");
  157. error sprintf(gettext("failed to read %s: %s"), $file, $r) if $r;
  158. if ($config{deterministic}) {
  159. $im->Set('date:create' => 0);
  160. $im->Set('date:modify' => 0);
  161. $im->Set('option' => 'png:exclude-chunk=time');
  162. }
  163. if (! defined $im->Get("width") || ! defined $im->Get("height")) {
  164. error sprintf(gettext("failed to get dimensions of %s"), $file);
  165. }
  166. if (! length $w && ! length $h) {
  167. $dwidth = $im->Get("width");
  168. $dheight = $im->Get("height");
  169. } else {
  170. error sprintf(gettext('wrong size format "%s" (should be WxH)'), $params{size})
  171. unless (defined $w && defined $h &&
  172. (length $w || length $h));
  173. if ($im->Get("width") == 0 || $im->Get("height") == 0) {
  174. ($dwidth, $dheight)=(0, 0);
  175. } elsif (! length $w || (length $h && $im->Get("height")*$w > $h * $im->Get("width"))) {
  176. # using height because only height is given or ...
  177. # because original image is more portrait than $w/$h
  178. # ... slimness of $im > $h/w
  179. # ... $im->Get("height")/$im->Get("width") > $h/$w
  180. # ... $im->Get("height")*$w > $h * $im->Get("width")
  181. $dheight=$h;
  182. $dwidth=$h / $im->Get("height") * $im->Get("width");
  183. } else { # (! length $h) or $w is what determines the resized size
  184. $dwidth=$w;
  185. $dheight=$w / $im->Get("width") * $im->Get("height");
  186. }
  187. }
  188. if ($dwidth < $im->Get("width") || $ispdf) {
  189. # resize down, or resize to pixels at all
  190. my $outfile = "$config{destdir}/$dir/$params{size}-$base";
  191. $imglink = "$dir/$params{size}-$base";
  192. will_render($params{page}, $imglink);
  193. if (-e $outfile && (-M $srcfile >= -M $outfile)) {
  194. $im = Image::Magick->new;
  195. $r = $im->Read($outfile);
  196. error sprintf(gettext("failed to read %s: %s"), $outfile, $r) if $r;
  197. }
  198. else {
  199. $r = $im->Resize(geometry => "${dwidth}x${dheight}");
  200. error sprintf(gettext("failed to resize: %s"), $r) if $r;
  201. $im->set($ispdf ? (magick => 'png') : ());
  202. my @blob = $im->ImageToBlob();
  203. # don't actually write resized file in preview mode;
  204. # rely on width and height settings
  205. if (! $params{preview}) {
  206. writefile($imglink, $config{destdir}, $blob[0], 1);
  207. }
  208. else {
  209. eval q{use MIME::Base64};
  210. error($@) if $@;
  211. $imgdatalink = "data:image/".$im->Get("magick").";base64,".encode_base64($blob[0]);
  212. }
  213. }
  214. # always get the true size of the resized image (it could be
  215. # that imagemagick did its calculations differently)
  216. $dwidth = $im->Get("width");
  217. $dheight = $im->Get("height");
  218. } else {
  219. $imglink = $file;
  220. }
  221. if (! defined($dwidth) || ! defined($dheight)) {
  222. error sprintf(gettext("failed to determine size of image %s"), $file)
  223. }
  224. }
  225. my ($fileurl, $imgurl);
  226. my $urltobase = $params{preview} ? undef : $params{destpage};
  227. $fileurl=urlto($file, $urltobase);
  228. $imgurl=$imgdatalink ? $imgdatalink : urlto($imglink, $urltobase);
  229. if (! exists $params{class}) {
  230. $params{class}="img";
  231. }
  232. my $attrs='';
  233. foreach my $attr (qw{alt title class id hspace vspace}) {
  234. if (exists $params{$attr}) {
  235. $attrs.=" $attr=\"$params{$attr}\"";
  236. }
  237. }
  238. my $imgtag='<img src="'.$imgurl.'"';
  239. $imgtag.=' width="'.$dwidth.'"' if defined $dwidth;
  240. $imgtag.=' height="'.$dheight.'"' if defined $dheight;
  241. $imgtag.= $attrs.
  242. (exists $params{align} && ! exists $params{caption} ? ' align="'.$params{align}.'"' : '').
  243. ' />';
  244. my $link;
  245. if (! defined $params{link}) {
  246. $link=$fileurl;
  247. }
  248. elsif ($params{link} =~ /^\w+:\/\//) {
  249. $link=$params{link};
  250. }
  251. if (defined $link) {
  252. $imgtag='<a href="'.$link.'">'.$imgtag.'</a>';
  253. }
  254. else {
  255. my $b = bestlink($params{page}, $params{link});
  256. if (length $b) {
  257. add_depends($params{page}, $b, deptype("presence"));
  258. $imgtag=htmllink($params{page}, $params{destpage},
  259. $params{link}, linktext => $imgtag,
  260. noimageinline => 1,
  261. );
  262. }
  263. }
  264. if (exists $params{caption}) {
  265. return '<table class="img'.
  266. (exists $params{align} ? " align-$params{align}" : "").
  267. '">'.
  268. '<caption>'.$params{caption}.'</caption>'.
  269. '<tr><td>'.$imgtag.'</td></tr>'.
  270. '</table>';
  271. }
  272. else {
  273. return $imgtag;
  274. }
  275. }
  276. 1