Gaurav Pant
Gaurav Pant

Reputation: 4209

create thumbnail of png image using Perl Image::Magic

I am using below code to get thumbnail image from large image.

I am using image magic for the same.

$thumbnail_width = 100;
$thumbnail_height = 60;

open my $file, "data2";
$image_data = <$file>;
create_thumbnail($image_data);


sub create_thumbnail {
    my ($image_data, $garb) = @_;
    use Image::Magick;
    #my $image = Image::Magick->new(magick=>'jpg');
    #my $image = Image::Magick->new(magick=>'png');
    #print "===>$image_data<===";
    my $image = Image::Magick->new;
    $error = $image->BlobToImage($image_data);
    print "1--".$error;
    $error = $image->SetAttribute(quality => 80);
    print "2--".$error;
    $error = $image->SetAttribute(compression => 'JPEG');
    print "3--".$error;
    $error = $image->Thumbnail(width => $thumbnail_width, height => $thumbnail_height);
    print "4--".$error;
    my $thumbnail_data = $image->ImageToBlob();

    print "Content-type: image/jpeg\nContent-length: " . length($thumbnail_data) . "\n\n";
    print STDERR "===>$thumbnail_data<=====";
    #print $thumbnail_data;
}

I am getting below error.

1--Exception 425: negative or zero image size ' @ error/gif.c/ReadGIFImage/13692--3--4--Exception 410: no images definedThumbnail' @ error/Magick.xs/XS_Image__Magick_Mogrify/7403Content-type: image/jpeg Content-length:

The image from which I want to create thumbnail image is of type PNG.I am using ImageMagick-6.9.1-2.

Upvotes: 1

Views: 530

Answers (1)

mob
mob

Reputation: 118635

You are only reading the input up to the first occurrence of \n, and so you are passing incomplete/corrupt data to your create_thumbnail function.

The canonical way to load the entire contents of a file handle into a scalar variable would look like:

open my $file, "data2";
$image_data = do { local $/; <$file> };

but if the local $/ looks too esoteric, these are equivalent:

$image_data = join '', <$file>;

use File::Slurp;
$image_data = read_file("data2");  # don't need $file handle

Upvotes: 0

Related Questions