#!/usr/bin/env perl use strict; use warnings; use JSON qw; use MIME::Base64 qw; use Image::Magick; use File::Temp qw; use Data::Dumper; exit unless exists $ENV{'TGUTILS_TYPE'}; exit unless $ENV{'TGUTILS_TYPE'} eq 'IMAGE'; $/ = undef; binmode STDIN; my $origimg = <>; my ($origh, $origpath) = tempfile(); my $newpath = $origpath . ".png"; # Prepare to read original file binmode $origh; print $origh $origimg; close $origh; # Convert image my $imagick = Image::Magick->new; my $out = $imagick->Read($origpath); die "ERROR: $out" if "$out"; $out = $imagick->Resize(geometry => '512x512'); die "ERROR: $out" if "$out"; $out = $imagick->Write($newpath); die "ERROR: $out" if "$out"; # Read new file open(my $newh, "<", $newpath) or die $!; binmode $newh; my $newimg = <$newh>; close $newh; unlink $origpath, $newpath; print encode_json({type => 'DOCUMENT', caption => 'Tenga, ayúdese', content => encode_base64($newimg, ''), filename => 'cosa.png'});