| 1 | package URI::Title::Image; | ||
| 2 | $URI::Title::Image::VERSION = '1.903'; | ||
| 3 | use warnings; | ||
| 4 | use strict; | ||
| 5 | |||
| 6 | use Image::Size; | ||
| 7 | |||
| 8 | sub types {( | ||
| 9 | 'image/gif', | ||
| 10 | 'image/jpg', | ||
| 11 | 'image/jpeg', | ||
| 12 | 'image/png', | ||
| 13 | 'image/x-png', | ||
| 14 | )} | ||
| 15 | |||
| 16 | sub pnginfo { | ||
| 17 | my ($data_ref) = @_; | ||
| 18 | my ($x, $y, $title) = (0, 0, ''); | ||
| 19 | if ( eval { require Image::ExifTool } ) { | ||
| 20 | my $info = Image::ExifTool::ImageInfo($data_ref); | ||
| 21 | ($x, $y, $title) = ($info->{ImageWidth}, $info->{ImageHeight}, $info->{Title}); | ||
| 22 | } | ||
| 23 | elsif( eval { require Image::PNG::Libpng } ) { | ||
| 24 | my $png = Image::PNG::Libpng::read_from_scalar($$data_ref); | ||
| 25 | $x = $png->get_image_width(); | ||
| 26 | $y = $png->get_image_height(); | ||
| 27 | my $text_chunks = $png->get_text(); | ||
| 28 | for (@$text_chunks) { | ||
| 29 | if ($_->{key} eq "Title") { | ||
| 30 | $title = $_->{text}; | ||
| 31 | last; | ||
| 32 | } | ||
| 33 | } | ||
| 34 | } | ||
| 35 | else { | ||
| 36 | ($x, $y) = imgsize($data_ref); | ||
| 37 | } | ||
| 38 | return ($x, $y, $title); | ||
| 39 | } | ||
| 40 | |||
| 41 | sub title { | ||
| 42 | my ($class, $url, $data, $type) = @_; | ||
| 43 | |||
| 44 | $type =~ s!^[^/]*/!!; | ||
| 45 | $type =~ s!^x-!!; | ||
| 46 | my $title = ""; | ||
| 47 | my $x = 0; | ||
| 48 | my $y = 0; | ||
| 49 | if ( $type =~ /png/ ) { | ||
| 50 | ($x, $y, $title) = pnginfo(\$data); | ||
| 51 | } | ||
| 52 | else { | ||
| 53 | ($x, $y) = imgsize(\$data); | ||
| 54 | } | ||
| 55 | $title ||= ( split m{/}, $url )[-1]; | ||
| 56 | return $x && $y | ||
| 57 | ? "$title ($type ${x}x${y})" | ||
| 58 | : "$title ($type)"; | ||
| 59 | } | ||
| 60 | |||
| 61 | 1; | ||
| 62 | |||
| 63 | __END__ |