summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/img.pm
blob: 68b001671653000b4907ababf1d63782d834381c (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. # optimisation: detect scan mode, and avoid generating the image
  39. if (! defined wantarray) {
  40. return;
  41. }
  42. my $file = bestlink($params{page}, $image);
  43. my $srcfile = srcfile($file, 1);
  44. if (! length $file || ! defined $srcfile) {
  45. return htmllink($params{page}, $params{destpage}, $image);
  46. }
  47. my $dir = $params{page};
  48. my $base = IkiWiki::basename($file);
  49. eval q{use Image::Magick};
  50. error gettext("Image::Magick is not installed") if $@;
  51. my $im = Image::Magick->new;
  52. my $imglink;
  53. my $r;
  54. if ($params{size} ne 'full') {
  55. add_depends($params{page}, $image);
  56. my ($w, $h) = ($params{size} =~ /^(\d*)x(\d*)$/);
  57. error sprintf(gettext('wrong size format "%s" (should be WxH)'), $params{size})
  58. unless (defined $w && defined $h &&
  59. (length $w || length $h));
  60. my $outfile = "$config{destdir}/$dir/${w}x${h}-$base";
  61. $imglink = "$dir/${w}x${h}-$base";
  62. will_render($params{page}, $imglink);
  63. if (-e $outfile && (-M $srcfile >= -M $outfile)) {
  64. $r = $im->Read($outfile);
  65. error sprintf(gettext("failed to read %s: %s"), $outfile, $r) if $r;
  66. }
  67. else {
  68. $r = $im->Read($srcfile);
  69. error sprintf(gettext("failed to read %s: %s"), $file, $r) if $r;
  70. $r = $im->Resize(geometry => "${w}x${h}");
  71. error sprintf(gettext("failed to resize: %s"), $r) if $r;
  72. # don't actually write file in preview mode
  73. if (! $params{preview}) {
  74. my @blob = $im->ImageToBlob();
  75. writefile($imglink, $config{destdir}, $blob[0], 1);
  76. }
  77. else {
  78. $imglink = $file;
  79. }
  80. }
  81. }
  82. else {
  83. $r = $im->Read($srcfile);
  84. error sprintf(gettext("failed to read %s: %s"), $file, $r) if $r;
  85. $imglink = $file;
  86. }
  87. my ($fileurl, $imgurl);
  88. if (! $params{preview}) {
  89. $fileurl=urlto($file, $params{destpage});
  90. $imgurl=urlto($imglink, $params{destpage});
  91. }
  92. else {
  93. $fileurl="$config{url}/$file";
  94. $imgurl="$config{url}/$imglink";
  95. }
  96. if (! defined($im->Get("width")) || ! defined($im->Get("height"))) {
  97. error sprintf(gettext("failed to determine size of image %s"), $file)
  98. }
  99. my $imgtag='<img src="'.$imgurl.
  100. '" width="'.$im->Get("width").
  101. '" height="'.$im->Get("height").'"'.
  102. (exists $params{alt} ? ' alt="'.$params{alt}.'"' : '').
  103. (exists $params{title} ? ' title="'.$params{title}.'"' : '').
  104. (exists $params{align} ? ' align="'.$params{align}.'"' : '').
  105. (exists $params{class} ? ' class="'.$params{class}.'"' : '').
  106. (exists $params{id} ? ' id="'.$params{id}.'"' : '').
  107. ' />';
  108. if (! defined $params{link} || lc($params{link}) eq 'yes') {
  109. $imgtag='<a href="'.$fileurl.'">'.$imgtag.'</a>';
  110. }
  111. elsif ($params{link} =~ /^\w+:\/\//) {
  112. $imgtag='<a href="'.$params{link}.'">'.$imgtag.'</a>';
  113. }
  114. elsif (length bestlink($params{page}, $params{link})) {
  115. add_depends($params{page}, $params{link});
  116. $imgtag=htmllink($params{page}, $params{destpage},
  117. $params{link}, linktext => $imgtag,
  118. noimageinline => 1);
  119. }
  120. if (exists $params{caption}) {
  121. return '<table class="img">'.
  122. '<caption>'.$params{caption}.'</caption>'.
  123. '<tr><td>'.$imgtag.'</td></tr>'.
  124. '</table>';
  125. }
  126. else {
  127. return $imgtag;
  128. }
  129. }
  130. 1