summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/img.pm
blob: 748d28aceae70a3494b8a9f587c0f2de0595db6c (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 => "preprocess", id => "img", call => \&preprocess, scan => 1);
  11. } #}}}
  12. sub preprocess (@) { #{{{
  13. my ($image) = $_[0] =~ /$config{wiki_file_regexp}/; # untaint
  14. my %params=@_;
  15. if (exists $imgdefaults{$params{page}}) {
  16. foreach my $key (keys %{$imgdefaults{$params{page}}}) {
  17. if (! exists $params{$key}) {
  18. $params{$key}=$imgdefaults{$params{page}}->{$key};
  19. }
  20. }
  21. }
  22. if (! exists $params{size}) {
  23. $params{size}='full';
  24. }
  25. if ($image eq 'defaults') {
  26. $imgdefaults{$params{page}} = \%params;
  27. return '';
  28. }
  29. push @{$links{$params{page}}}, $image;
  30. # optimisation: detect scan mode, and avoid generating the image
  31. if (! defined wantarray) {
  32. return;
  33. }
  34. my $file = bestlink($params{page}, $image);
  35. my $srcfile = srcfile($file, 1);
  36. if (! length $file || ! defined $srcfile) {
  37. return htmllink($params{page}, $params{destpage}, $image);
  38. }
  39. my $dir = $params{page};
  40. my $base = IkiWiki::basename($file);
  41. eval q{use Image::Magick};
  42. error gettext("Image::Magick is not installed") if $@;
  43. my $im = Image::Magick->new;
  44. my $imglink;
  45. my $r;
  46. if ($params{size} ne 'full') {
  47. my ($w, $h) = ($params{size} =~ /^(\d+)x(\d+)$/);
  48. error sprintf(gettext('bad size "%s"'), $params{size})
  49. unless (defined $w && defined $h);
  50. my $outfile = "$config{destdir}/$dir/${w}x${h}-$base";
  51. $imglink = "$dir/${w}x${h}-$base";
  52. will_render($params{page}, $imglink);
  53. if (-e $outfile && (-M $srcfile >= -M $outfile)) {
  54. $r = $im->Read($outfile);
  55. error sprintf(gettext("failed to read %s: %s"), $outfile, $r) if $r;
  56. }
  57. else {
  58. $r = $im->Read($srcfile);
  59. error sprintf(gettext("failed to read %s: %s"), $file, $r) if $r;
  60. $r = $im->Resize(geometry => "${w}x${h}");
  61. error sprintf(gettext("failed to resize: %s"), $r) if $r;
  62. # don't actually write file in preview mode
  63. if (! $params{preview}) {
  64. my @blob = $im->ImageToBlob();
  65. writefile($imglink, $config{destdir}, $blob[0], 1);
  66. }
  67. else {
  68. $imglink = $file;
  69. }
  70. }
  71. }
  72. else {
  73. $r = $im->Read($srcfile);
  74. error sprintf(gettext("failed to read %s: %s"), $file, $r) if $r;
  75. $imglink = $file;
  76. }
  77. add_depends($imglink, $params{page});
  78. my ($fileurl, $imgurl);
  79. if (! $params{preview}) {
  80. $fileurl=urlto($file, $params{destpage});
  81. $imgurl=urlto($imglink, $params{destpage});
  82. }
  83. else {
  84. $fileurl="$config{url}/$file";
  85. $imgurl="$config{url}/$imglink";
  86. }
  87. if (! defined($im->Get("width")) || ! defined($im->Get("height"))) {
  88. error sprintf(gettext("failed to determine size of image %s"), $file)
  89. }
  90. my $imgtag='<img src="'.$imgurl.
  91. '" alt="'.(exists $params{alt} ? $params{alt} : '').
  92. '" width="'.$im->Get("width").
  93. '" height="'.$im->Get("height").'"'.
  94. (exists $params{title} ? ' title="'.$params{title}.'"' : '').
  95. (exists $params{class} ? ' class="'.$params{class}.'"' : '').
  96. (exists $params{id} ? ' id="'.$params{id}.'"' : '').
  97. ' />';
  98. if (! defined $params{link} || lc($params{link}) eq 'yes') {
  99. $imgtag='<a href="'.$fileurl.'">'.$imgtag.'</a>';
  100. }
  101. elsif ($params{link} =~ /^\w+:\/\//) {
  102. $imgtag='<a href="'.$params{link}.'">'.$imgtag.'</a>';
  103. }
  104. elsif (length bestlink($params{page}, $params{link})) {
  105. add_depends($params{page}, $params{link});
  106. $imgtag=htmllink($params{page}, $params{destpage},
  107. $params{link}, linktext => $imgtag,
  108. noimageinline => 1);
  109. }
  110. if (exists $params{caption}) {
  111. return '<table class="img">'.
  112. '<caption>'.$params{caption}.'</caption>'.
  113. '<tr><td>'.$imgtag.'</td></tr>'.
  114. '</table>';
  115. }
  116. else {
  117. return $imgtag;
  118. }
  119. } #}}}
  120. 1