+ eval q{use Image::Magick};
+ error gettext("Image::Magick is not installed") if $@;
+ my $im = Image::Magick->new;
+ my $imglink;
+ my $r = $im->Read($srcfile);
+ error sprintf(gettext("failed to read %s: %s"), $file, $r) if $r;
+
+ my ($dwidth, $dheight);
+
+ if ($params{size} ne 'full') {
+ my ($w, $h) = ($params{size} =~ /^(\d*)x(\d*)$/);
+ error sprintf(gettext('wrong size format "%s" (should be WxH)'), $params{size})
+ unless (defined $w && defined $h &&
+ (length $w || length $h));
+
+ if ((length $w && $w > $im->Get("width")) ||
+ (length $h && $h > $im->Get("height"))) {
+ # resizing larger
+ $imglink = $file;
+
+ # don't generate larger image, just set display size
+ if (length $w && length $h) {
+ ($dwidth, $dheight)=($w, $h);
+ }
+ # avoid division by zero on 0x0 image
+ elsif ($im->Get("width") == 0 || $im->Get("height") == 0) {
+ ($dwidth, $dheight)=(0, 0);
+ }
+ # calculate unspecified size from the other one, preserving
+ # aspect ratio
+ elsif (length $w) {
+ $dwidth=$w;
+ $dheight=$w / $im->Get("width") * $im->Get("height");
+ }
+ elsif (length $h) {
+ $dheight=$h;
+ $dwidth=$h / $im->Get("height") * $im->Get("width");
+ }