summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/img.pm
blob: 14a32f63542fb827e40ff926771982436e02f59a (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. my $file = bestlink($params{page}, $image);
  29. my $dir = $params{page};
  30. my $base = IkiWiki::basename($file);
  31. eval q{use Image::Magick};
  32. error($@) if $@;
  33. my $im = Image::Magick->new;
  34. my $imglink;
  35. my $r;
  36. if ($size ne 'full') {
  37. my ($w, $h) = ($size =~ /^(\d+)x(\d+)$/);
  38. return "[[img ".sprintf(gettext('bad size "%s"'), $size)."]]"
  39. unless (defined $w && defined $h);
  40. my $outfile = "$config{destdir}/$dir/${w}x${h}-$base";
  41. $imglink = "$dir/${w}x${h}-$base";
  42. will_render($params{page}, $imglink);
  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 ".sprintf(gettext("failed to resize: %s"), $r)."]]" if $r;
  52. # don't actually write file in preview mode
  53. if (! $params{preview}) {
  54. my @blob = $im->ImageToBlob();
  55. writefile($imglink, $config{destdir}, $blob[0], 1);
  56. }
  57. else {
  58. $imglink = $file;
  59. }
  60. }
  61. }
  62. else {
  63. $r = $im->Read(srcfile($file));
  64. return "[[img ".sprintf(gettext("failed to read %s: %s"), $file, $r)."]]" if $r;
  65. $imglink = $file;
  66. }
  67. add_depends($imglink, $params{page});
  68. my ($fileurl, $imgurl);
  69. if (! $params{preview}) {
  70. $fileurl=urlto($file, $params{destpage});
  71. $imgurl=urlto($imglink, $params{destpage});
  72. }
  73. else {
  74. $fileurl="$config{url}/$file";
  75. $imgurl="$config{url}/$imglink";
  76. }
  77. if (! defined($im->Get("width")) || ! defined($im->Get("height"))) {
  78. return "[[img ".sprintf(gettext("failed to determine size of image %s"), $file)."]]";
  79. }
  80. my $imgtag='<img src="'.$imgurl.
  81. '" alt="'.$alt.'" width="'.$im->Get("width").
  82. '" height="'.$im->Get("height").'"'.
  83. (exists $params{class} ? ' class="'.$params{class}.'"' : '').
  84. (exists $params{id} ? ' id="'.$params{id}.'"' : '').
  85. ' />';
  86. if (! defined $params{link} || lc($params{link}) eq 'yes') {
  87. return '<a href="'.$fileurl.'">'.$imgtag.'</a>';
  88. }
  89. elsif (length bestlink($params{page}, $params{link})) {
  90. return htmllink($params{page}, $params{destpage},
  91. $params{link}, linktext => $imgtag);
  92. }
  93. else {
  94. return $imgtag;
  95. }
  96. } #}}}
  97. 1