source: subversion/applications/utils/tagwatch/getPhotos.pl @ 5086

Last change on this file since 5086 was 5079, checked in by ojw, 12 years ago

Cache the original (full-size) photos before they get resized, and only
get new versions if the file on web has changed

File size: 2.4 KB
Line 
1#-----------------------------------------------------------------
2# Downloads photos showing a real-life example of each OpenStreetmap
3# tag
4#-----------------------------------------------------------------
5# Usage: perl getPhotos.pl
6# Will create an ./html/photos directory and fill it with JPEG images
7# Uses input from http://wiki.openstreetmap.org/index.php/Tagwatch/Photos
8#-----------------------------------------------------------------
9# This file is part of Tagwatch
10# Tagwatch is free software: you can redistribute it and/or modify
11# it under the terms of the GNU General Public License as published by
12# the Free Software Foundation, either version 3 of the License, or
13# (at your option) any later version.
14#
15# Tagwatch is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18# GNU General Public License for more details.
19#
20# You should have received a copy of the GNU General Public License
21# along with Tagwatch.  If not, see <http://www.gnu.org/licenses/>.
22#---------------------------------------------------------------
23use strict;
24use MediaWiki;
25use LWP::Simple;
26use GD;
27
28my $c = MediaWiki->new;
29$c->setup({'wiki' => {
30            'host' => 'wiki.openstreetmap.org',
31            'path' => ''}});
32
33my $PDir = "html/Photos";
34my $CacheDir = "original_photos";
35mkdir "html" if ! -d "html";
36mkdir $PDir if ! -d $PDir;
37mkdir $CacheDir if ! -d $CacheDir;
38
39foreach my $Line(split(/\n/, $c->text("Tagwatch/Photos"))){
40  if($Line =~ m{^\*\s*(\w+)=(\w+)\s+(.*?)\s*$}){
41    print "Fetching: $1 = $2\n";
42   
43    # Download the image
44    my $Filename = "$PDir/$1_$2.jpg";
45    my $URL = $3;
46   
47    my $CacheName = $URL;
48    $CacheName =~ s/\W/_/g;
49    $CacheName = "$CacheDir/$CacheName.jpg";
50    mirror($URL, $CacheName);
51   
52    my $Data;
53    {
54    local($/);
55    open(LOAD, $CacheName) || die;
56    binmode LOAD;
57    $Data = <LOAD>;
58    close LOAD;
59    }
60   
61    # Open it in GD
62    my $Image1 = GD::Image->newFromJpegData($Data);
63
64    # Calculate image size
65    my $WO = $Image1->width;
66    my $W = 200;
67    my $Ratio = $W / $WO;
68    my $HO = $Image1->height;
69    my $H = $HO * $Ratio;
70   
71    # Make a resized copy, and save that
72    my $Image2 = new GD::Image($W,$H);
73    $Image2->copyResampled($Image1,0,0,0,0,$W,$H,$WO,$HO);
74    open(IMOUT, ">$Filename") || die;
75    binmode IMOUT;
76    print IMOUT $Image2->jpeg();
77    close IMOUT;
78
79  }
80}
Note: See TracBrowser for help on using the repository browser.