summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/img.pm
blob: 8888b3a56d8bb2a350fe22a310cf8cb23c390c6a (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. will_render($params{page}, $imglink);
  44. if (-e $outfile && (-M srcfile($file) >= -M $outfile)) {
  45. $r = $im->Read($outfile);
  46. return "[[img ".sprintf(gettext("failed to read %s: %s"), $outfile, $r)."]]" if $r;
  47. }
  48. else {
  49. $r = $im->Read(srcfile($file));
  50. return "[[img ".sprintf(gettext("failed to read %s: %s"), $file, $r)."]]" if $r;
  51. $r = $im->Resize(geometry => "${w}x${h}");
  52. return "[[img ".sprinftf(gettext("failed to resize: %s"), $r)."]]" if $r;
  53. # don't actually write file in preview mode
  54. if (! $params{preview}) {
  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=urlto($file, $params{destpage});
  72. $imgurl=urlto($imglink, $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