summaryrefslogtreecommitdiff
path: root/pegatino.pl
blob: 1a732deebc116e79ae35f20f6fac0820a6a12d9b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/usr/bin/env perl

use strict;
use warnings;

use JSON qw<encode_json>;
use MIME::Base64 qw<encode_base64>;
use Image::Magick;
use File::Temp qw<tempfile>;
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'});