summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/img.pm
blob: 5c580c03cec01a5c4529fefd9f207702e0306d58 (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. push @{$links{$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('bad size "%s"'), $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. '" alt="'.(exists $params{alt} ? $params{alt} : '').
  101. '" width="'.$im->Get("width").
  102. '" height="'.$im->Get("height").'"'.
  103. (exists $params{title} ? ' title="'.$params{title}.'"' : '').
  104. (exists $params{class} ? ' class="'.$params{class}.'"' : '').
  105. (exists $params{id} ? ' id="'.$params{id}.'"' : '').
  106. ' />';
  107. if (! defined $params{link} || lc($params{link}) eq 'yes') {
  108. $imgtag='<a href="'.$fileurl.'">'.$imgtag.'</a>';
  109. }
  110. elsif ($params{link} =~ /^\w+:\/\//) {
  111. $imgtag='<a href="'.$params{link}.'">'.$imgtag.'</a>';
  112. }
  113. elsif (length bestlink($params{page}, $params{link})) {
  114. add_depends($params{page}, $params{link});
  115. $imgtag=htmllink($params{page}, $params{destpage},
  116. $params{link}, linktext => $imgtag,
  117. noimageinline => 1);
  118. }
  119. if (exists $params{caption}) {
  120. return '<table class="img">'.
  121. '<caption>'.$params{caption}.'</caption>'.
  122. '<tr><td>'.$imgtag.'</td></tr>'.
  123. '</table>';
  124. }
  125. else {
  126. return $imgtag;
  127. }
  128. }
  129. 1