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;
|
2008-12-23 22:34:19 +01:00
|
|
|
use IkiWiki 3.00;
|
2006-10-21 23:59:44 +02:00
|
|
|
|
|
|
|
my %imgdefaults;
|
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub import {
|
2008-08-03 22:40:12 +02:00
|
|
|
hook(type => "getsetup", id => "img", call => \&getsetup);
|
2007-05-23 18:50:41 +02:00
|
|
|
hook(type => "preprocess", id => "img", call => \&preprocess, scan => 1);
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2006-10-21 23:59:44 +02:00
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub getsetup () {
|
2008-08-03 22:40:12 +02:00
|
|
|
return
|
|
|
|
plugin => {
|
|
|
|
safe => 1,
|
|
|
|
rebuild => undef,
|
2010-02-12 12:35:52 +01:00
|
|
|
section => "widget",
|
2008-08-03 22:40:12 +02:00
|
|
|
},
|
2016-05-04 09:54:19 +02:00
|
|
|
img_allowed_formats => {
|
|
|
|
type => "string",
|
|
|
|
default => [qw(jpeg png gif)],
|
|
|
|
description => "Image formats to process (jpeg, png, gif, pdf, svg or 'everything' to accept all)",
|
|
|
|
# ImageMagick has had arbitrary code execution flaws,
|
|
|
|
# and the whole delegates mechanism is scary from
|
|
|
|
# that perspective
|
|
|
|
safe => 0,
|
|
|
|
rebuild => 0,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
sub allowed {
|
|
|
|
my $format = shift;
|
|
|
|
my $allowed = $config{img_allowed_formats};
|
|
|
|
$allowed = ['jpeg', 'png'] unless defined $allowed && @$allowed;
|
|
|
|
|
|
|
|
foreach my $a (@$allowed) {
|
|
|
|
return 1 if $a eq $format || $a eq 'everything';
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2008-08-03 22:40:12 +02:00
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub preprocess (@) {
|
2006-10-21 23:59:44 +02:00
|
|
|
my ($image) = $_[0] =~ /$config{wiki_file_regexp}/; # untaint
|
|
|
|
my %params=@_;
|
|
|
|
|
2010-01-29 03:07:23 +01:00
|
|
|
if (! defined $image) {
|
|
|
|
error("bad image filename");
|
|
|
|
}
|
|
|
|
|
2008-06-08 06:02:33 +02:00
|
|
|
if (exists $imgdefaults{$params{page}}) {
|
|
|
|
foreach my $key (keys %{$imgdefaults{$params{page}}}) {
|
|
|
|
if (! exists $params{$key}) {
|
|
|
|
$params{$key}=$imgdefaults{$params{page}}->{$key};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-13 04:59:46 +02:00
|
|
|
if (! exists $params{size} || ! length $params{size}) {
|
2008-06-08 06:02:33 +02:00
|
|
|
$params{size}='full';
|
2006-10-21 23:59:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($image eq 'defaults') {
|
2008-06-08 06:02:33 +02:00
|
|
|
$imgdefaults{$params{page}} = \%params;
|
2006-10-21 23:59:44 +02:00
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
Avoid %links accumulating duplicates. (For TOVA)
This is sorta an optimisation, and sorta a bug fix. In one
test case I have available, it can speed a page build up from 3
minutes to 3 seconds.
The root of the problem is that $links{$page} contains arrays of
links, rather than hashes of links. And when a link is found,
it is just pushed onto the array, without checking for dups.
Now, the array is emptied before scanning a page, so there
should not be a lot of opportunity for lots of duplicate links
to pile up in it. But, in some cases, they can, and if there
are hundreds of duplicate links in the array, then scanning it
for matching links, as match_link and some other code does,
becomes much more expensive than it needs to be.
Perhaps the real right fix would be to change the data structure
to a hash. But, the list of links is never accessed like that,
you always want to iterate through it.
I also looked at deduping the list in saveindex, but that does
a lot of unnecessary work, and doesn't completly solve the problem.
So, finally, I decided to add an add_link function that handles deduping,
and make ikiwiki-transition remove the old dup links.
2009-05-06 05:40:09 +02:00
|
|
|
add_link($params{page}, $image);
|
2009-09-28 02:57:27 +02:00
|
|
|
add_depends($params{page}, $image);
|
2009-07-27 22:22:26 +02:00
|
|
|
|
2008-01-09 08:31:11 +01:00
|
|
|
# optimisation: detect scan mode, and avoid generating the image
|
|
|
|
if (! defined wantarray) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-05-23 03:44:11 +02:00
|
|
|
my $file = bestlink($params{page}, $image);
|
2008-07-21 22:38:40 +02:00
|
|
|
my $srcfile = srcfile($file, 1);
|
2008-07-21 22:53:52 +02:00
|
|
|
if (! length $file || ! defined $srcfile) {
|
|
|
|
return htmllink($params{page}, $params{destpage}, $image);
|
2008-07-21 22:38:40 +02:00
|
|
|
}
|
2006-10-21 23:59:44 +02:00
|
|
|
|
2008-03-24 01:01:26 +01:00
|
|
|
my $dir = $params{page};
|
2006-10-21 23:59:44 +02:00
|
|
|
my $base = IkiWiki::basename($file);
|
2016-05-04 09:52:40 +02:00
|
|
|
my $extension;
|
|
|
|
my $format;
|
|
|
|
|
|
|
|
if ($base =~ m/\.([a-z0-9]+)$/) {
|
|
|
|
$extension = $1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
error gettext("Unable to detect image type from extension");
|
|
|
|
}
|
|
|
|
|
|
|
|
# Never interpret well-known file extensions as any other format,
|
|
|
|
# in case the wiki configuration unwisely allows attaching
|
|
|
|
# arbitrary files named *.jpg, etc.
|
|
|
|
if ($extension =~ m/^(jpeg|jpg)$/is) {
|
|
|
|
$format = 'jpeg';
|
|
|
|
}
|
|
|
|
elsif ($extension =~ m/^(png)$/is) {
|
|
|
|
$format = 'png';
|
|
|
|
}
|
|
|
|
elsif ($extension =~ m/^(gif)$/is) {
|
|
|
|
$format = 'gif';
|
|
|
|
}
|
|
|
|
elsif ($extension =~ m/^(svg)$/is) {
|
|
|
|
$format = 'svg';
|
|
|
|
}
|
|
|
|
elsif ($extension =~ m/^(pdf)$/is) {
|
|
|
|
$format = 'pdf';
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
# allow ImageMagick to auto-detect (potentially dangerous)
|
|
|
|
$format = '';
|
|
|
|
}
|
|
|
|
|
2016-05-04 09:54:19 +02:00
|
|
|
error sprintf(gettext("%s image processing disabled in img_allowed_formats configuration"), $format ? $format : "\"$extension\"") unless allowed($format ? $format : "everything");
|
|
|
|
|
2011-06-29 20:40:30 +02:00
|
|
|
my $issvg = $base=~s/\.svg$/.png/i;
|
2014-04-07 11:19:04 +02:00
|
|
|
my $ispdf = $base=~s/\.pdf$/.png/i;
|
2014-04-07 11:32:25 +02:00
|
|
|
my $pagenumber = exists($params{pagenumber}) ? int($params{pagenumber}) : 0;
|
|
|
|
if ($pagenumber != 0) {
|
|
|
|
$base = "p$pagenumber-$base";
|
|
|
|
}
|
2007-02-20 04:59:35 +01:00
|
|
|
|
|
|
|
eval q{use Image::Magick};
|
2008-07-13 21:05:34 +02:00
|
|
|
error gettext("Image::Magick is not installed") if $@;
|
2014-04-07 11:15:51 +02:00
|
|
|
my $im = Image::Magick->new();
|
2006-10-21 23:59:44 +02:00
|
|
|
my $imglink;
|
2014-07-15 11:57:57 +02:00
|
|
|
my $imgdatalink;
|
2016-05-04 09:52:40 +02:00
|
|
|
my $r = $im->Read("$format:$srcfile\[$pagenumber]");
|
2009-09-28 02:53:02 +02:00
|
|
|
error sprintf(gettext("failed to read %s: %s"), $file, $r) if $r;
|
2014-09-16 10:37:41 +02:00
|
|
|
|
|
|
|
if (! defined $im->Get("width") || ! defined $im->Get("height")) {
|
|
|
|
error sprintf(gettext("failed to get dimensions of %s"), $file);
|
|
|
|
}
|
|
|
|
|
2009-08-29 05:23:06 +02:00
|
|
|
my ($dwidth, $dheight);
|
|
|
|
|
2014-07-15 00:23:56 +02:00
|
|
|
if ($params{size} eq 'full') {
|
|
|
|
$dwidth = $im->Get("width");
|
|
|
|
$dheight = $im->Get("height");
|
|
|
|
} else {
|
2008-09-09 21:20:06 +02:00
|
|
|
my ($w, $h) = ($params{size} =~ /^(\d*)x(\d*)$/);
|
2009-07-19 13:36:46 +02:00
|
|
|
error sprintf(gettext('wrong size format "%s" (should be WxH)'), $params{size})
|
2008-09-09 21:20:06 +02:00
|
|
|
unless (defined $w && defined $h &&
|
|
|
|
(length $w || length $h));
|
2014-07-15 00:23:56 +02:00
|
|
|
|
|
|
|
if ($im->Get("width") == 0 || $im->Get("height") == 0) {
|
|
|
|
($dwidth, $dheight)=(0, 0);
|
|
|
|
} elsif (! length $w || (length $h && $im->Get("height")*$w > $h * $im->Get("width"))) {
|
|
|
|
# using height because only height is given or ...
|
|
|
|
# because original image is more portrait than $w/$h
|
|
|
|
# ... slimness of $im > $h/w
|
|
|
|
# ... $im->Get("height")/$im->Get("width") > $h/$w
|
|
|
|
# ... $im->Get("height")*$w > $h * $im->Get("width")
|
|
|
|
|
|
|
|
$dheight=$h;
|
|
|
|
$dwidth=$h / $im->Get("height") * $im->Get("width");
|
|
|
|
} else { # (! length $h) or $w is what determines the resized size
|
|
|
|
$dwidth=$w;
|
|
|
|
$dheight=$w / $im->Get("width") * $im->Get("height");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($dwidth < $im->Get("width") || $ispdf) {
|
|
|
|
# resize down, or resize to pixels at all
|
|
|
|
|
|
|
|
my $outfile = "$config{destdir}/$dir/$params{size}-$base";
|
|
|
|
$imglink = "$dir/$params{size}-$base";
|
|
|
|
|
|
|
|
will_render($params{page}, $imglink);
|
|
|
|
|
|
|
|
if (-e $outfile && (-M $srcfile >= -M $outfile)) {
|
|
|
|
$im = Image::Magick->new;
|
|
|
|
$r = $im->Read($outfile);
|
|
|
|
error sprintf(gettext("failed to read %s: %s"), $outfile, $r) if $r;
|
2006-10-21 23:59:44 +02:00
|
|
|
}
|
|
|
|
else {
|
2014-07-15 00:23:56 +02:00
|
|
|
$r = $im->Resize(geometry => "${dwidth}x${dheight}");
|
|
|
|
error sprintf(gettext("failed to resize: %s"), $r) if $r;
|
|
|
|
|
2014-07-15 11:57:57 +02:00
|
|
|
$im->set(($issvg || $ispdf) ? (magick => 'png') : ());
|
|
|
|
my @blob = $im->ImageToBlob();
|
2014-07-15 00:23:56 +02:00
|
|
|
# don't actually write resized file in preview mode;
|
|
|
|
# rely on width and height settings
|
|
|
|
if (! $params{preview}) {
|
|
|
|
writefile($imglink, $config{destdir}, $blob[0], 1);
|
2007-03-06 23:37:05 +01:00
|
|
|
}
|
|
|
|
else {
|
2014-07-15 11:57:57 +02:00
|
|
|
eval q{use MIME::Base64};
|
|
|
|
error($@) if $@;
|
|
|
|
$imgdatalink = "data:image/".$im->Get("magick").";base64,".encode_base64($blob[0]);
|
2007-03-06 23:37:05 +01:00
|
|
|
}
|
2006-10-21 23:59:44 +02:00
|
|
|
}
|
2014-07-15 00:23:56 +02:00
|
|
|
|
|
|
|
# always get the true size of the resized image (it could be
|
|
|
|
# that imagemagick did its calculations differently)
|
|
|
|
$dwidth = $im->Get("width");
|
2009-08-29 05:23:06 +02:00
|
|
|
$dheight = $im->Get("height");
|
2014-07-15 00:23:56 +02:00
|
|
|
} else {
|
|
|
|
$imglink = $file;
|
2006-10-21 23:59:44 +02:00
|
|
|
}
|
2009-09-28 02:53:02 +02:00
|
|
|
|
|
|
|
if (! defined($dwidth) || ! defined($dheight)) {
|
|
|
|
error sprintf(gettext("failed to determine size of image %s"), $file)
|
|
|
|
}
|
2006-10-21 23:59:44 +02:00
|
|
|
|
2007-03-06 23:37:05 +01:00
|
|
|
my ($fileurl, $imgurl);
|
2014-07-15 11:57:57 +02:00
|
|
|
my $urltobase = $params{preview} ? undef : $params{destpage};
|
|
|
|
$fileurl=urlto($file, $urltobase);
|
|
|
|
$imgurl=$imgdatalink ? $imgdatalink : urlto($imglink, $urltobase);
|
2007-03-06 23:37:05 +01:00
|
|
|
|
2010-10-13 18:57:16 +02:00
|
|
|
if (! exists $params{class}) {
|
2010-07-05 20:04:49 +02:00
|
|
|
$params{class}="img";
|
|
|
|
}
|
|
|
|
|
2010-06-12 22:16:24 +02:00
|
|
|
my $attrs='';
|
|
|
|
foreach my $attr (qw{alt title class id hspace vspace}) {
|
|
|
|
if (exists $params{$attr}) {
|
|
|
|
$attrs.=" $attr=\"$params{$attr}\"";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-15 21:00:07 +02:00
|
|
|
my $imgtag='<img src="'.$imgurl.
|
2009-08-29 05:23:06 +02:00
|
|
|
'" width="'.$dwidth.
|
|
|
|
'" height="'.$dheight.'"'.
|
2010-06-12 22:16:24 +02:00
|
|
|
$attrs.
|
2010-01-07 22:09:34 +01:00
|
|
|
(exists $params{align} && ! exists $params{caption} ? ' align="'.$params{align}.'"' : '').
|
2007-07-15 21:00:07 +02:00
|
|
|
' />';
|
|
|
|
|
2010-01-07 21:36:49 +01:00
|
|
|
my $link;
|
2010-01-07 21:41:16 +01:00
|
|
|
if (! defined $params{link}) {
|
2010-01-07 21:36:49 +01:00
|
|
|
$link=$fileurl;
|
2007-07-15 21:00:07 +02:00
|
|
|
}
|
2007-12-28 22:14:43 +01:00
|
|
|
elsif ($params{link} =~ /^\w+:\/\//) {
|
2010-01-07 21:36:49 +01:00
|
|
|
$link=$params{link};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (defined $link) {
|
2010-01-07 22:09:34 +01:00
|
|
|
$imgtag='<a href="'.$link.'">'.$imgtag.'</a>';
|
2007-12-28 22:14:43 +01:00
|
|
|
}
|
2009-06-18 16:34:48 +02:00
|
|
|
else {
|
|
|
|
my $b = bestlink($params{page}, $params{link});
|
|
|
|
|
|
|
|
if (length $b) {
|
2009-10-09 19:37:06 +02:00
|
|
|
add_depends($params{page}, $b, deptype("presence"));
|
2009-06-18 16:34:48 +02:00
|
|
|
$imgtag=htmllink($params{page}, $params{destpage},
|
|
|
|
$params{link}, linktext => $imgtag,
|
2010-01-05 15:23:22 +01:00
|
|
|
noimageinline => 1,
|
2010-01-07 21:36:49 +01:00
|
|
|
);
|
2009-06-18 16:34:48 +02:00
|
|
|
}
|
2007-09-22 18:46:27 +02:00
|
|
|
}
|
2008-06-08 05:45:40 +02:00
|
|
|
|
2008-06-08 06:02:33 +02:00
|
|
|
if (exists $params{caption}) {
|
2010-01-05 15:23:22 +01:00
|
|
|
return '<table class="img'.
|
2010-01-07 22:09:34 +01:00
|
|
|
(exists $params{align} ? " align-$params{align}" : "").
|
2010-01-05 15:23:22 +01:00
|
|
|
'">'.
|
2008-06-08 06:02:33 +02:00
|
|
|
'<caption>'.$params{caption}.'</caption>'.
|
2008-06-08 05:45:40 +02:00
|
|
|
'<tr><td>'.$imgtag.'</td></tr>'.
|
|
|
|
'</table>';
|
|
|
|
}
|
2007-07-15 21:00:07 +02:00
|
|
|
else {
|
|
|
|
return $imgtag;
|
|
|
|
}
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2006-10-21 23:59:44 +02:00
|
|
|
|
2007-03-06 23:37:05 +01:00
|
|
|
1
|