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