use warnings use strict use Image Magick use Data Dumper use HTTP Requ

 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
use warnings;
use strict;
use Image::Magick;
use Data::Dumper;
use HTTP::Request;
use LWP::UserAgent;
my $file_name = "/tmp/xuj/favicon";
my $HTTP_request = HTTP::Request->new('GET', 'http://www.spaces.ru/favicon.ico');
my $user_agent = LWP::UserAgent->new();
$user_agent->timeout(1);
push @{$user_agent->requests_redirectable}, 'GET';
my $response = $user_agent->request($HTTP_request);
my $status_line = $response->status_line();
unless ($status_line =~ m/200/) {
print qq|\t- ошибка при запросе картинки, status_line: $status_line\n|;
next;
}
my $image_data = $response->content();
my $pict_file;
# сохраняем .ico файл
open ($pict_file, '>', $file_name.'.ico') or die "cannot open $file_name.ico: $!";
binmode $pict_file;
print {$pict_file} $image_data;
close($pict_file) or warn "cannot close $file_name.ico: $!";
my $IM = Image::Magick->new();
$IM->Read($file_name.'.ico');
print Dumper($IM)."\n";
my $last_w = 0;
my $last_h = 0;
if (@$IM) {
my $big_image;
for my $img (@$IM) {
my $width = $img->Get('width');
my $height = $img->Get('height');
print "$width x $height\n";
if ($width == 16 && $height == 16) {
$big_image = $img;
last;
}
if ($width > $last_w || $height > $last_h) {
$big_image = $img;
$last_w = $width if ($width > $last_w);
$last_h = $width if ($height > $last_h);
}
}
if ($big_image->Get('width') != 16 || $big_image->Get('height') != 16) {
$big_image->Resize(width => 16, height => 16);
}
$big_image->Write(filename => $file_name.'xuj.png', background => 'none', alpha => 'on', 'flatten' => 1);
}