Ganesh Ganesh - 16 days ago 6
Perl Question

how to resize image in memory using convert command

I am using the following code in a ImageMagic

use Image::Magick;
use Image::Magick::Square;

my $img_url = 'https://d2v48i7nl75u94.cloudfront.net/uploads/f1a14e1ce5028c34da667ec3f74416d8.jpeg';
my ($is_success, $status_line, $content, undef) = &HTTPRequest("GET", $img_url, undef, undef, undef, undef, undef, undef, undef, undef, undef, 1, undef, undef, undef, undef, 30);
my $magick = Image::Magick->new;
$magick->BlobToImage( $content );
$magick->Scale('200x200');
$magick->Extent( gravity => 'Center', geometry => '200x200',background=>'white' );
my $img_content = $magick->ImageToBlob();
my $filename = "dev-tUJmYqP0q4-front-32604604-small.jpg";
$small_success = &S3Store("images/$filename", $img_content, "image/jpg");


against to import "Image::Magick" (above code), I want use convert command like this.

my $img_url = 'https://d2v48i7nl75u94.cloudfront.net/uploads/f1a14e1ce5028c34da667ec3f74416d8.jpeg';
$convert = `convert $img_url -resize 300x300 -gravity center -extent 200x200 -background white -alpha remove /tmp/$pre-$rand_id-thumb.jpg 2>&1`;


I am trying to avoid writing the output to disk via the command and then re-reading the output file into the application.
Is there anything like ImageToBlob in convert command?

Thanks.

Answer

you can open the output of convert piped as a filehandle.

In your case

my $img_url = 'https://d2v48i7nl75u94.cloudfront.net/uploads/f1a14e1ce5028c34da667ec3f74416d8.jpeg';
open( my $fh, 'convert $img_url -resize 300x300 -gravity center -extent 200x200 -background white -alpha remove jpeg:- 2>&1 |');
local $/;
$convert = <$fh>;

might do the job.

HTH Georg

Comments