This repository has been archived by the owner on May 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpictures.pl
93 lines (79 loc) · 2.57 KB
/
pictures.pl
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/perl
use GD;
if ($ARGV[0] eq "THUMBS") {
CreateThumbs($ARGV[1]);
} elsif ($ARGV[0] eq 'IMPORT') {
my $pid = $ARGV[1];
if ($ARGV[2] =~ /http:/) {
system("wget -O ../data/photos/$ARGV[1].jpeg \"$ARGV[2]\"");
} else {
if ($pid eq "") {
if ($ARGV[2] !~ /(^|\/)(\d+)\.jpe?g$/i) { die; }
$pid = $2;
}
system("cp $ARGV[2] ../data/photos/$pid.jpeg");
}
CreateThumbs($pid);
open CREDIT, ">../data/photos/$pid-credit.txt";
print CREDIT "$ARGV[3] $ARGV[4]\n";
close CREDIT;
} elsif ($ARGV[0] eq 'BIOGUIDE') {
require "general.pl";
require "db.pl";
GovDBOpen();
@ids = DBSelect("people", ['id', 'bioguideid'], ["EXISTS(SELECT * FROM people_roles WHERE personid=id AND startdate>='2004-01-01')"]);
DBClose();
for my $id (@ids) {
my ($pid, $bgid) = @$id;
if ($bgid !~ /^([A-Z])(\d+)$/) { die $bgid; }
my ($a, $b) = ($1, $2);
if (-e "../data/photos/$pid.jpeg") { next; }
sleep 1;
print "$pid $bgid...\n";
my $response = $UA->get(lc("http://bioguide.congress.gov/bioguide/photo/$a/$bgid.jpg"));
if (!$response->is_success) { next; }
print "\tGot it.\n";
open PHOTO, ">../data/photos/$pid.jpeg";
print PHOTO $response->content;
close PHOTO;
open PHOTO, ">../data/photos/$pid-credit.txt";
print PHOTO lc("http://bioguide.congress.gov/scripts/biodisplay.pl?index=$bgid") . " Biographical Directory of the United States Congress\n";
close PHOTO;
CreateThumbs($pid);
}
} else {
1;
}
sub CreateThumbs {
my $id = shift;
resize("../data/photos/$id.jpeg", "../data/photos/$id-200px.jpeg", 200);
resize("../data/photos/$id.jpeg", "../data/photos/$id-100px.jpeg", 100);
resize("../data/photos/$id.jpeg", "../data/photos/$id-50px.jpeg", 50);
}
sub resize {
my ($src, $dst, $width) = @_;
my $srcimg = GD::Image->new($src) or die "Could not open image $src";
my $height = $width * 1.2;
my $dstimg = GD::Image->new($width, $height, 1);
my ($srcx, $srcy) = (0,0);
my ($srcw, $srch) = ($srcimg->width, $srcimg->height);
# Since the aspect ratio of the input image surely won't
# match with our uniform ratio of 1.2, we will crop the image
# automatically and center in the larger axis.
if ($height/$width*$srcimg->width < $srcimg->height) {
$srcy = ($srcimg->height - $height/$width*$srcimg->width)/2;
$srch -= $srcy*2;
} else {
$srcx = ($srcimg->width - $width/$height*$srcimg->height)/2;
$srcw -= $srcx*2;
}
$dstimg->copyResampled($srcimg,
0,0, # dest x/y
$srcx,$srcy, # src x/y
$width,$height, # dest w/h
$srcw, $srch);
open PNG, ">$dst" or die $@;
print PNG $dstimg->jpeg();
close PNG;
}
1;