summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/img.pm
blob: 66cc08530e685e8d10f6ae90646d7496a00401d2 (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 2.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. my $outfile = "$config{destdir}/$dir/${w}x${h}-$base";
  59. $imglink = "$dir/${w}x${h}-$base";
  60. will_render($params{page}, $imglink);
  61. if (-e $outfile && (-M $srcfile >= -M $outfile)) {
  62. $r = $im->Read($outfile);
  63. error sprintf(gettext("failed to read %s: %s"), $outfile, $r) if $r;
  64. }
  65. else {
  66. $r = $im->Read($srcfile);
  67. error sprintf(gettext("failed to read %s: %s"), $file, $r) if $r;
  68. $r = $im->Resize(geometry => "${w}x${h}");
  69. error sprintf(gettext("failed to resize: %s"), $r) if $r;
  70. # don't actually write file in preview mode
  71. if (! $params{preview}) {
  72. my @blob = $im->ImageToBlob();
  73. writefile($imglink, $config{destdir}, $blob[0], 1);
  74. }
  75. else {
  76. $imglink = $file;
  77. }
  78. }
  79. }
  80. else {
  81. $r = $im->Read($srcfile);
  82. error sprintf(gettext("failed to read %s: %s"), $file, $r) if $r;
  83. $imglink = $file;
  84. }
  85. add_depends($imglink, $params{page});
  86. my ($fileurl, $imgurl);
  87. if (! $params{preview}) {
  88. $fileurl=urlto($file, $params{destpage});
  89. $imgurl=urlto($imglink, $params{destpage});
  90. }
  91. else {
  92. $fileurl="$config{url}/$file";
  93. $imgurl="$config{url}/$imglink";
  94. }
  95. if (! defined($im->Get("width")) || ! defined($im->Get("height"))) {
  96. error sprintf(gettext("failed to determine size of image %s"), $file)
  97. }
  98. my $imgtag='<img src="'.$imgurl.
  99. '" alt="'.(exists $params{alt} ? $params{alt} : '').
  100. '" width="'.$im->Get("width").
  101. '" height="'.$im->Get("height").'"'.
  102. (exists $params{title} ? ' title="'.$params{title}.'"' : '').
  103. (exists $params{class} ? ' class="'.$params{class}.'"' : '').
  104. (exists $params{id} ? ' id="'.$params{id}.'"' : '').
  105. ' />';
  106. if (! defined $params{link} || lc($params{link}) eq 'yes') {
  107. $imgtag='<a href="'.$fileurl.'">'.$imgtag.'</a>';
  108. }
  109. elsif ($params{link} =~ /^\w+:\/\//) {
  110. $imgtag='<a href="'.$params{link}.'">'.$imgtag.'</a>';
  111. }
  112. elsif (length bestlink($params{page}, $params{link})) {
  113. add_depends($params{page}, $params{link});
  114. $imgtag=htmllink($params{page}, $params{destpage},
  115. $params{link}, linktext => $imgtag,
  116. noimageinline => 1);
  117. }
  118. if (exists $params{caption}) {
  119. return '<table class="img">'.
  120. '<caption>'.$params{caption}.'</caption>'.
  121. '<tr><td>'.$imgtag.'</td></tr>'.
  122. '</table>';
  123. }
  124. else {
  125. return $imgtag;
  126. }
  127. } #}}}
  128. 1