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