2006-10-21 23:59:44 +02:00
|
|
|
#!/usr/bin/perl
|
|
|
|
# Ikiwiki enhanced image handling plugin
|
|
|
|
# Christian Mock cm@tahina.priv.at 20061002
|
|
|
|
package IkiWiki::Plugin::img;
|
|
|
|
|
|
|
|
use warnings;
|
|
|
|
use strict;
|
2007-04-27 04:55:52 +02:00
|
|
|
use IkiWiki 2.00;
|
2006-10-21 23:59:44 +02:00
|
|
|
|
|
|
|
my %imgdefaults;
|
|
|
|
|
|
|
|
sub import { #{{{
|
|
|
|
hook(type => "preprocess", id => "img", call => \&preprocess);
|
|
|
|
} #}}}
|
|
|
|
|
|
|
|
sub preprocess (@) { #{{{
|
|
|
|
my ($image) = $_[0] =~ /$config{wiki_file_regexp}/; # untaint
|
|
|
|
my %params=@_;
|
|
|
|
|
|
|
|
if (! exists $imgdefaults{$params{page}}) {
|
|
|
|
$imgdefaults{$params{page}} = {};
|
|
|
|
}
|
|
|
|
my $size = $params{size} || $imgdefaults{$params{page}}->{size} || 'full';
|
|
|
|
my $alt = $params{alt} || $imgdefaults{$params{page}}->{alt} || '';
|
|
|
|
|
|
|
|
if ($image eq 'defaults') {
|
|
|
|
$imgdefaults{$params{page}} = {
|
|
|
|
size => $size,
|
|
|
|
alt => $alt,
|
|
|
|
};
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2007-05-23 03:44:11 +02:00
|
|
|
my $file = bestlink($params{page}, $image);
|
|
|
|
if (! $file) {
|
|
|
|
# TODO: this may not be right, depending on where the file is
|
|
|
|
# created in the end
|
|
|
|
add_depends($params{page}, $image);
|
|
|
|
|
|
|
|
return "[[img ".sprintf(gettext("%s not found"), $image)."]]";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
add_depends($params{page}, $file);
|
|
|
|
}
|
2006-10-21 23:59:44 +02:00
|
|
|
|
|
|
|
my $dir = IkiWiki::dirname($file);
|
|
|
|
my $base = IkiWiki::basename($file);
|
2007-02-20 04:59:35 +01:00
|
|
|
|
|
|
|
eval q{use Image::Magick};
|
|
|
|
error($@) if $@;
|
2006-10-21 23:59:44 +02:00
|
|
|
my $im = Image::Magick->new;
|
|
|
|
my $imglink;
|
|
|
|
my $r;
|
|
|
|
|
|
|
|
if ($size ne 'full') {
|
|
|
|
my ($w, $h) = ($size =~ /^(\d+)x(\d+)$/);
|
2007-03-07 13:04:45 +01:00
|
|
|
return "[[img ".sprintf(gettext('bad size "%s"'), $size)."]]"
|
|
|
|
unless (defined $w && defined $h);
|
2006-10-21 23:59:44 +02:00
|
|
|
|
|
|
|
my $outfile = "$config{destdir}/$dir/${w}x${h}-$base";
|
|
|
|
$imglink = "$dir/${w}x${h}-$base";
|
2007-04-01 22:53:03 +02:00
|
|
|
|
|
|
|
will_render($params{page}, $imglink);
|
2006-10-21 23:59:44 +02:00
|
|
|
|
|
|
|
if (-e $outfile && (-M srcfile($file) >= -M $outfile)) {
|
|
|
|
$r = $im->Read($outfile);
|
2007-03-07 13:04:45 +01:00
|
|
|
return "[[img ".sprintf(gettext("failed to read %s: %s"), $outfile, $r)."]]" if $r;
|
2006-10-21 23:59:44 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
$r = $im->Read(srcfile($file));
|
2007-03-07 13:04:45 +01:00
|
|
|
return "[[img ".sprintf(gettext("failed to read %s: %s"), $file, $r)."]]" if $r;
|
2006-10-21 23:59:44 +02:00
|
|
|
|
|
|
|
$r = $im->Resize(geometry => "${w}x${h}");
|
2007-03-07 13:04:45 +01:00
|
|
|
return "[[img ".sprinftf(gettext("failed to resize: %s"), $r)."]]" if $r;
|
2006-10-21 23:59:44 +02:00
|
|
|
|
2007-03-06 23:37:05 +01:00
|
|
|
# don't actually write file in preview mode
|
|
|
|
if (! $params{preview}) {
|
|
|
|
my @blob = $im->ImageToBlob();
|
|
|
|
writefile($imglink, $config{destdir}, $blob[0], 1);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$imglink = $file;
|
|
|
|
}
|
2006-10-21 23:59:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$r = $im->Read(srcfile($file));
|
2007-03-07 13:04:45 +01:00
|
|
|
return "[[img ".sprintf(gettext("failed to read %s: %s"), $file, $r)."]]" if $r;
|
2006-10-21 23:59:44 +02:00
|
|
|
$imglink = $file;
|
|
|
|
}
|
|
|
|
|
|
|
|
add_depends($imglink, $params{page});
|
|
|
|
|
2007-03-06 23:37:05 +01:00
|
|
|
my ($fileurl, $imgurl);
|
|
|
|
if (! $params{preview}) {
|
2007-04-01 22:53:03 +02:00
|
|
|
$fileurl=urlto($file, $params{destpage});
|
|
|
|
$imgurl=urlto($imglink, $params{destpage});
|
2007-03-06 23:37:05 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
$fileurl="$config{url}/$file";
|
|
|
|
$imgurl="$config{url}/$imglink";
|
|
|
|
}
|
|
|
|
|
2007-05-23 03:44:11 +02:00
|
|
|
if (! defined($im->Get("width")) || ! defined($im->Get("height"))) {
|
|
|
|
return "[[img ".sprintf(gettext("failed to determine size of image %s"), $file)."]]";
|
|
|
|
}
|
|
|
|
|
2007-03-06 23:37:05 +01:00
|
|
|
return '<a href="'.$fileurl.'"><img src="'.$imgurl.
|
2006-10-21 23:59:44 +02:00
|
|
|
'" alt="'.$alt.'" width="'.$im->Get("width").
|
|
|
|
'" height="'.$im->Get("height").'" /></a>';
|
|
|
|
} #}}}
|
|
|
|
|
2007-03-06 23:37:05 +01:00
|
|
|
1
|