summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/img.pm
blob: 0606f5001170190b4177eea63240ad808889ffa6 (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;
  8. my %imgdefaults;
  9. sub import { #{{{
  10. hook(type => "preprocess", id => "img", call => \&preprocess);
  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. add_depends($params{page}, $image);
  28. my $file = bestlink($params{page}, $image)
  29. || return "[[img ".sprintf(gettext("%s not found"), $image)."]]";
  30. my $dir = IkiWiki::dirname($file);
  31. my $base = IkiWiki::basename($file);
  32. eval q{use Image::Magick};
  33. error($@) if $@;
  34. my $im = Image::Magick->new;
  35. my $imglink;
  36. my $r;
  37. if ($size ne 'full') {
  38. my ($w, $h) = ($size =~ /^(\d+)x(\d+)$/);
  39. return "[[img ".sprintf(gettext('bad size "%s"'), $size)."]]"
  40. unless (defined $w && defined $h);
  41. my $outfile = "$config{destdir}/$dir/${w}x${h}-$base";
  42. $imglink = "$dir/${w}x${h}-$base";
  43. if (-e $outfile && (-M srcfile($file) >= -M $outfile)) {
  44. $r = $im->Read($outfile);
  45. return "[[img ".sprintf(gettext("failed to read %s: %s"), $outfile, $r)."]]" if $r;
  46. }
  47. else {
  48. $r = $im->Read(srcfile($file));
  49. return "[[img ".sprintf(gettext("failed to read %s: %s"), $file, $r)."]]" if $r;
  50. $r = $im->Resize(geometry => "${w}x${h}");
  51. return "[[img ".sprinftf(gettext("failed to resize: %s"), $r)."]]" if $r;
  52. # don't actually write file in preview mode
  53. if (! $params{preview}) {
  54. will_render($params{page}, $imglink);
  55. my @blob = $im->ImageToBlob();
  56. writefile($imglink, $config{destdir}, $blob[0], 1);
  57. }
  58. else {
  59. $imglink = $file;
  60. }
  61. }
  62. }
  63. else {
  64. $r = $im->Read(srcfile($file));
  65. return "[[img ".sprintf(gettext("failed to read %s: %s"), $file, $r)."]]" if $r;
  66. $imglink = $file;
  67. }
  68. add_depends($imglink, $params{page});
  69. my ($fileurl, $imgurl);
  70. if (! $params{preview}) {
  71. $fileurl=IkiWiki::abs2rel($file, IkiWiki::dirname($params{destpage}));
  72. $imgurl=IkiWiki::abs2rel($imglink, IkiWiki::dirname($params{destpage}));
  73. }
  74. else {
  75. $fileurl="$config{url}/$file";
  76. $imgurl="$config{url}/$imglink";
  77. }
  78. return '<a href="'.$fileurl.'"><img src="'.$imgurl.
  79. '" alt="'.$alt.'" width="'.$im->Get("width").
  80. '" height="'.$im->Get("height").'" /></a>';
  81. } #}}}
  82. 1