Skip to content

Instantly share code, notes, and snippets.

@msroz
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save msroz/3329705f5bee43c4aebe to your computer and use it in GitHub Desktop.
Save msroz/3329705f5bee43c4aebe to your computer and use it in GitHub Desktop.
Image::Info#imgage_info
#!/usr/bine/env perl
use v5.20.1;
use warnings;
use Image::Info qw/image_info/;
use Data::Dumper;
use Text::ASCIITable;
my @images = qw{
img/jpeg.jpeg
img/gif.gif
img/jpeg.jpeg
};
for my $image_file (@images) {
my $info = image_info($image_file);
my $t = Text::ASCIITable->new(+{ headingText => "Results of Image::Info#image_info" });
$t->setCols(qw/key value/);
$t->addRow($_, $info->{$_}) for (keys %{$info});;
say $t;
}

Preparation

file size
img/png.png 600*400
img/gif.gif 600*400
img/jpeg.jpeg 600*400

Result

.--------------------------------------------------------------------------------------.
|                           Results of Image::Info#image_info                          |
+------------------------+-------------------------------------------------------------+
| key                    | value                                                       |
+------------------------+-------------------------------------------------------------+
| resolution             | 1/1                                                         |
| height                 |                                                         400 |
| JFIF_Version           |                                                        1.01 |
| JPEG_Type              | Baseline                                                    |
| ColorComponentsDecoded | ARRAY(0x7fc7a9a7bdd0)                                       |
| Comment                | CREATOR: gd-jpeg v1.0 (using IJG JPEG v62), default quality |
| SamplesPerPixel        |                                                           3 |
| BitsPerSample          | ARRAY(0x7fc7a9827c28)                                       |
| ColorComponents        | ARRAY(0x7fc7a9a7bd10)                                       |
| file_ext               | jpg                                                         |
| color_type             | YCbCr                                                       |
| file_media_type        | image/jpeg                                                  |
| width                  |                                                         600 |
'------------------------+-------------------------------------------------------------'


.------------------------------------.
|  Results of Image::Info#image_info |
+----------------------+-------------+
| key                  | value       |
+----------------------+-------------+
| width                |         600 |
| ScreenHeight         |         400 |
| file_media_type      | image/gif   |
| color_type           | Indexed-RGB |
| file_ext             | gif         |
| GlobalColorTableFlag |           1 |
| ColorTableSize       |          16 |
| height               |         400 |
| XPosition            |           0 |
| GIF_Version          |         87a |
| ScreenWidth          |         600 |
| YPosition            |           0 |
| resolution           | 1/1         |
| BackgroundColor      |           0 |
| ColorResolution      |           7 |
'----------------------+-------------'

.--------------------------------------------------------------------------------------.
|                           Results of Image::Info#image_info                          |
+------------------------+-------------------------------------------------------------+
| key                    | value                                                       |
+------------------------+-------------------------------------------------------------+
| color_type             | YCbCr                                                       |
| file_ext               | jpg                                                         |
| file_media_type        | image/jpeg                                                  |
| width                  |                                                         600 |
| BitsPerSample          | ARRAY(0x7fc7a989b130)                                       |
| SamplesPerPixel        |                                                           3 |
| ColorComponents        | ARRAY(0x7fc7a989b3b8)                                       |
| JFIF_Version           |                                                        1.01 |
| height                 |                                                         400 |
| Comment                | CREATOR: gd-jpeg v1.0 (using IJG JPEG v62), default quality |
| ColorComponentsDecoded | ARRAY(0x7fc7a989b4a8)                                       |
| JPEG_Type              | Baseline                                                    |
| resolution             | 1/1                                                         |
'------------------------+-------------------------------------------------------------'
#!/usr/bine/env perl
use v5.20.1;
use warnings;
use Image::Info qw/image_info/;
#use Image::Size;
use Data::Dumper;
use Text::ASCIITable;
use Furl;
my $image_file = $ARGV[0];
unless ($image_file) {
say 'file or url not found';
exit(0);
}
if ($image_file =~ /^https?:\/\//) {
say $image_file;
my $furl = Furl->new( timeout => 10 );
my $res = $furl->request(
method => 'GET',
url => $image_file,
);
die $res->status_line unless $res->is_success;
my $data = $res->content;
my $info = image_info(\$data);
my $t = Text::ASCIITable->new(+{ headingText => "Results of Image::Info#image_info" });
$t->setCols(qw/key value/);
$t->addRow($_, $info->{$_}) for ( sort { $a cmp $b } keys %{$info});
say $t;
}
else {
my $info = image_info($image_file);
my $t = Text::ASCIITable->new(+{ headingText => "Results of Image::Info#image_info" });
$t->setCols(qw/key value/);
$t->addRow($_, $info->{$_}) for ( sort { $a cmp $b } keys %{$info});
say $t;
}
#!/usr/bin/env perl
use v5.20.1;
use warnings;
use Imager;
use Image::Info qw(image_info);
use MIME::Base64 qw(encode_base64 decode_base64);
my $img_file = $ARGV[0];
unless($img_file && -f $img_file) {
say 'image file not found.';
exit(0);
}
my $imager = Hoge::Imager->new();
my $orientation = $imager->orientation($img_file);
say $orientation;
{
package Hoge::Imager;
use warnings;
use strict;
use Imager;
my $ORIENTATION_MAP = {
1 => { rigth => 0, mirror => undef },
2 => { rigth => 0, mirror => 'h' },
3 => { rigth => 0, mirror => 'hv' },
4 => { rigth => 0, mirror => 'v' },
5 => { rigth => 270, mirror => 'h' },
6 => { rigth => 90, mirror => undef },
7 => { rigth => 90, mirror => 'h' },
8 => { rigth => 270, mirror => undef },
};
sub new {
my ($class, %args) = @_;
my $self = bless {
%args
}, $class;
$self->{imager} = Imager->new;
return $self;
}
sub imager {
my ($self) = @_;
return $self->{imager};
}
sub orientation {
my ($self, $file) = @_;
$self->imager->read(file => $file, type => 'jpeg') or die $!;
my $orientation = $self->imager->tags(name => 'exif_orientation') || 1;
return $orientation;
}
sub get_image_info {
my ($self, $encoded_image) = @_;
return image_info($encoded_image);
}
sub decode_image {
my ($self, $encoded_image) = @_;
return decode_base64($encoded_image);
}
sub encode_image {
my ($self, $decoded_image) = @_;
return encode_base64($decoded_image, '');
}
1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment