summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/img.pm
blob: 44d67bd8375057e77d5853ee93cdd7943108ed14 (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. my ($w, $h) = ($params{size} =~ /^(\d*)x(\d*)$/);
  56. error sprintf(gettext('wrong size format "%s" (should be WxH)'), $params{size})
  57. unless (defined $w && defined $h &&
  58. (length $w || length $h));
  59. my $outfile = "$config{destdir}/$dir/${w}x${h}-$base";
  60. $imglink = "$dir/${w}x${h}-$base";
  61. will_render($params{page}, $imglink);
  62. if (-e $outfile && (-M $srcfile >= -M $outfile)) {
  63. $r = $im->Read($outfile);
  64. error sprintf(gettext("failed to read %s: %s"), $outfile, $r) if $r;
  65. }
  66. else {
  67. $r = $im->Read($srcfile);
  68. error sprintf(gettext("failed to read %s: %s"), $file, $r) if $r;
  69. $r = $im->Resize(geometry => "${w}x${h}");
  70. error sprintf(gettext("failed to resize: %s"), $r) if $r;
  71. # don't actually write file in preview mode
  72. if (! $params{preview}) {
  73. my @blob = $im->ImageToBlob();
  74. writefile($imglink, $config{destdir}, $blob[0], 1);
  75. }
  76. else {
  77. $imglink = $file;
  78. }
  79. }
  80. }
  81. else {
  82. $r = $im->Read($srcfile);
  83. error sprintf(gettext("failed to read %s: %s"), $file, $r) if $r;
  84. $imglink = $file;
  85. }
  86. add_depends($imglink, $params{page});
  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