- my $r;
-
- if ($size ne 'full') {
- my ($w, $h) = ($size =~ /^(\d+)x(\d+)$/);
- return "[[img ".sprintf(gettext('bad size "%s"'), $size)."]]"
- unless (defined $w && defined $h);
-
- my $outfile = "$config{destdir}/$dir/${w}x${h}-$base";
- $imglink = "$dir/${w}x${h}-$base";
-
- if (-e $outfile && (-M srcfile($file) >= -M $outfile)) {
- $r = $im->Read($outfile);
- return "[[img ".sprintf(gettext("failed to read %s: %s"), $outfile, $r)."]]" if $r;
+ 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");
+ }