H
hoyoul
Hi,
I am using ImageMagick 6.2.0.7-2 and PerlMagick to create thumbnails
from images. Basically I am reading in an image with ImageMagick,
resizing it so either the width or the height is 100 pixels (depending
on which is smaller) and then cropping it to 100 by 100 pixels. So for
instance, I have an image that is 325 by 500 pixels. After the process
above, instead of ending up with a thumbnail that's 100 by 100 pixels
(centered), I get an image that's 100 by 154 (basically resized from
325 by 500) and the top and bottom of the image is white while the
middle 100 by 100 is the image. So instead of cropping the image, it's
just turning the cropped areas into a white background. Any clues as to
why? Here is the code:
sub createThumbnail {
my $this = shift;
if (!$this->{'image'} || !$this->{'size'}) { return; }
my $image = Image::Magick->new;
#$image->Read(blob => $this->{'image'});
$image->BlobToImage($this->{'image'});
my ($width, $height) = $image->Get('width', 'height');
my $offsetx = 0;
my $offsety = 0;
# Resize image before attempting to crop the image
if ($width <= $height) {
my $h = $this->round(($height / $width) * $this->{'_thumb_width'});
$offsety = $this->round(($h - $this->{'_thumb_height'}) / 2);
$image->Resize(width => $this->{'_thumb_width'}, height => $h);
}
else {
my $w = $this->round(($width / $height) *
$this->{'_thumb_height'});
$offsetx = $this->round(($w - $this->{'_thumb_width'}) / 2);
#$image->Resize(width => $w, height => $this->{'_thumb_height'});
$image->Resize(geometry => $w.'x'.$this->{'_thumb_height'});
}
# Croping image with a re-adjusted offset
$image->Crop(
width => $this->{'_thumb_width'},
height => $this->{'_thumb_height'},
x => $offsetx, y => $offsety);
print "Content-type: image/gif\n\n";
print $image->ImageToBlob;
exit;
}
I tried $image->Chop but that function doesn't seem to work at all with
my version. Any help would greatly be appreciated!
Thanks
Hoyoul
I am using ImageMagick 6.2.0.7-2 and PerlMagick to create thumbnails
from images. Basically I am reading in an image with ImageMagick,
resizing it so either the width or the height is 100 pixels (depending
on which is smaller) and then cropping it to 100 by 100 pixels. So for
instance, I have an image that is 325 by 500 pixels. After the process
above, instead of ending up with a thumbnail that's 100 by 100 pixels
(centered), I get an image that's 100 by 154 (basically resized from
325 by 500) and the top and bottom of the image is white while the
middle 100 by 100 is the image. So instead of cropping the image, it's
just turning the cropped areas into a white background. Any clues as to
why? Here is the code:
sub createThumbnail {
my $this = shift;
if (!$this->{'image'} || !$this->{'size'}) { return; }
my $image = Image::Magick->new;
#$image->Read(blob => $this->{'image'});
$image->BlobToImage($this->{'image'});
my ($width, $height) = $image->Get('width', 'height');
my $offsetx = 0;
my $offsety = 0;
# Resize image before attempting to crop the image
if ($width <= $height) {
my $h = $this->round(($height / $width) * $this->{'_thumb_width'});
$offsety = $this->round(($h - $this->{'_thumb_height'}) / 2);
$image->Resize(width => $this->{'_thumb_width'}, height => $h);
}
else {
my $w = $this->round(($width / $height) *
$this->{'_thumb_height'});
$offsetx = $this->round(($w - $this->{'_thumb_width'}) / 2);
#$image->Resize(width => $w, height => $this->{'_thumb_height'});
$image->Resize(geometry => $w.'x'.$this->{'_thumb_height'});
}
# Croping image with a re-adjusted offset
$image->Crop(
width => $this->{'_thumb_width'},
height => $this->{'_thumb_height'},
x => $offsetx, y => $offsety);
print "Content-type: image/gif\n\n";
print $image->ImageToBlob;
exit;
}
I tried $image->Chop but that function doesn't seem to work at all with
my version. Any help would greatly be appreciated!
Thanks
Hoyoul