| 1 |
package Plagger::Plugin::Filter::ImageInfo; |
|---|
| 2 |
use strict; |
|---|
| 3 |
use base qw( Plagger::Plugin ); |
|---|
| 4 |
|
|---|
| 5 |
use Image::Info; |
|---|
| 6 |
use Plagger::UserAgent; |
|---|
| 7 |
|
|---|
| 8 |
sub register { |
|---|
| 9 |
my($self, $context) = @_; |
|---|
| 10 |
$context->register_hook( |
|---|
| 11 |
$self, |
|---|
| 12 |
'update.entry.fixup' => \&entry, |
|---|
| 13 |
'update.feed.fixup' => \&feed, |
|---|
| 14 |
); |
|---|
| 15 |
} |
|---|
| 16 |
|
|---|
| 17 |
sub entry { |
|---|
| 18 |
my($self, $context, $args) = @_; |
|---|
| 19 |
$self->fixup($context, $args->{entry}->icon); |
|---|
| 20 |
} |
|---|
| 21 |
|
|---|
| 22 |
sub feed { |
|---|
| 23 |
my($self, $context, $args) = @_; |
|---|
| 24 |
$self->fixup($context, $args->{feed}->image); |
|---|
| 25 |
} |
|---|
| 26 |
|
|---|
| 27 |
sub fixup { |
|---|
| 28 |
my($self, $context, $image) = @_; |
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
return unless $image && $image->{url}; |
|---|
| 32 |
return if $image->{width} && $image->{height}; |
|---|
| 33 |
|
|---|
| 34 |
$context->log(info => "Trying to fetch image size of $image->{url}"); |
|---|
| 35 |
|
|---|
| 36 |
my $info = $self->cache->get_callback( |
|---|
| 37 |
$image->{url}, |
|---|
| 38 |
sub { $self->fetch_image_info($image->{url}) }, |
|---|
| 39 |
"3 days", |
|---|
| 40 |
); |
|---|
| 41 |
|
|---|
| 42 |
if ($info) { |
|---|
| 43 |
$context->log(debug => "width=$info->{width}, height=$info->{height}"); |
|---|
| 44 |
$image->{width} = $info->{width}; |
|---|
| 45 |
$image->{height} = $info->{height}; |
|---|
| 46 |
} |
|---|
| 47 |
} |
|---|
| 48 |
|
|---|
| 49 |
sub fetch_image_info { |
|---|
| 50 |
my($self, $url) = @_; |
|---|
| 51 |
|
|---|
| 52 |
my $ua = Plagger::UserAgent->new; |
|---|
| 53 |
my $res = $ua->fetch($url); |
|---|
| 54 |
|
|---|
| 55 |
if ($res->is_error) { |
|---|
| 56 |
Plagger->context->log(error => "Error fetching $url"); |
|---|
| 57 |
return; |
|---|
| 58 |
} |
|---|
| 59 |
|
|---|
| 60 |
my $info = eval { Image::Info::image_info(\$res->content) }; |
|---|
| 61 |
$info; |
|---|
| 62 |
} |
|---|
| 63 |
|
|---|
| 64 |
1; |
|---|
| 65 |
|
|---|
| 66 |
__END__ |
|---|
| 67 |
|
|---|
| 68 |
=head1 NAME |
|---|
| 69 |
|
|---|
| 70 |
Plagger::Plugin::Filter::ImageInfo - Fetch image info (width/height etc.) for feed and entry images |
|---|
| 71 |
|
|---|
| 72 |
=head1 SYNOPSIS |
|---|
| 73 |
|
|---|
| 74 |
- module: Filter::ImageInfo |
|---|
| 75 |
|
|---|
| 76 |
=head1 DESCRIPTION |
|---|
| 77 |
|
|---|
| 78 |
This plugin tries to fetch feed image (logo) and entry image (buddy |
|---|
| 79 |
icon) and extracts image info like width & height. The data is parsed |
|---|
| 80 |
with L<Image::Info> module and cached for 3 days. |
|---|
| 81 |
|
|---|
| 82 |
=head1 AUTHOR |
|---|
| 83 |
|
|---|
| 84 |
Tatsuhiko Miyagawa |
|---|
| 85 |
|
|---|
| 86 |
=head1 SEE ALSO |
|---|
| 87 |
|
|---|
| 88 |
L<Plagger>, L<Image::Info> |
|---|
| 89 |
|
|---|
| 90 |
=cut |
|---|