summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/img.pm
blob: abc7e099ace54d78ef33f919ca1ebf6eaa2ad6ba (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. my $title = $params{title} || $imgdefaults{$params{page}}->{title} || '';
  21. if ($image eq 'defaults') {
  22. $imgdefaults{$params{page}} = {
  23. size => $size,
  24. alt => $alt,
  25. title => $title,
  26. };
  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. error($@) if $@;
  39. my $im = Image::Magick->new;
  40. my $imglink;
  41. my $r;
  42. if ($size ne 'full') {
  43. my ($w, $h) = ($size =~ /^(\d+)x(\d+)$/);
  44. return "[[img ".sprintf(gettext('bad size "%s"'), $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="'.$alt.'" width="'.$im->Get("width").
  88. '" height="'.$im->Get("height").'"'.
  89. (defined $title ? ' title="'.$title.'"' : '').
  90. (exists $params{class} ? ' class="'.$params{class}.'"' : '').
  91. (exists $params{id} ? ' id="'.$params{id}.'"' : '').
  92. ' />';
  93. if (! defined $params{link} || lc($params{link}) eq 'yes') {
  94. return '<a href="'.$fileurl.'">'.$imgtag.'</a>';
  95. }
  96. elsif ($params{link} =~ /^\w+:\/\//) {
  97. return '<a href="'.$params{link}.'">'.$imgtag.'</a>';
  98. }
  99. elsif (length bestlink($params{page}, $params{link})) {
  100. add_depends($params{page}, $params{link});
  101. return htmllink($params{page}, $params{destpage},
  102. $params{link}, linktext => $imgtag,
  103. noimageinline => 1);
  104. }
  105. else {
  106. return $imgtag;
  107. }
  108. } #}}}
  109. 1