Skip to content

Instantly share code, notes, and snippets.

@bohwaz
Last active May 16, 2016 01:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bohwaz/cbfba841e990ca85d414a89199a2f928 to your computer and use it in GitHub Desktop.
Save bohwaz/cbfba841e990ca85d414a89199a2f928 to your computer and use it in GitHub Desktop.
Download large images of art from National Gallery of Victoria (NGV) website
#!/usr/bin/php
<?php
/**
* Download a large size art image from National Gallery of Victoria (NGV) website
* Copyleft (C) 2016 BohwaZ http://bohwaz.net/ (Public domain)
*/
if (empty($argv[1]))
{
echo "Usage: " . $argv[0] . " NGV_URL" . PHP_EOL;
exit;
}
$url = $argv[1];
if (!preg_match('!^https?://www.ngv.vic.gov.au/explore/collection/work/(\d+)/!i', $url, $match))
{
echo "Invalid URL supplied." . PHP_EOL;
exit;
}
$body = file_get_contents($url);
if (!preg_match('/imgWidth\s*=\s*(\d+)/i', $body, $width)
|| !preg_match('/imgHeight\s*=\s*(\d+)/i', $body, $height)
|| !preg_match('/var\s+url\s*=\s*\'(.*?)\'/i',$body, $tiles_url)
|| !preg_match('!<title>(.*?)</title>!i',$body, $title))
{
echo "Can not find image size, title or tile URL" . PHP_EOL;
exit;
}
$width = (int)$width[1];
$height = (int)$height[1];
$tiles_url = $tiles_url[1] . 'TileGroup0/';
$comment = preg_replace('/\|\s*NGV.*$/', '', $title[1]) . ' -- Archived from ' . $url;
$filename = preg_replace('/\|\s*NGV.*$/', '', $title[1]);
$filename = str_replace('|', '-', $filename);
$filename = trim(preg_replace('/[^\w\d -]+/', '', $filename)) . '.jpg';
$tiles_x = ceil($width / 256);
$tiles_y = ceil($height / 256);
$tileSize = 256;
$levels = 0;
// Getting tile size
while ($width > $tileSize || $height > $tileSize)
{
$levels++;
$tileSize += $tileSize;
}
$z = $levels;
echo "Size: $width x $height ($tiles_x X $tiles_y Tiles, max zoom = $z)" . PHP_EOL;
$tmpdir = sys_get_temp_dir() . '/ngv-dl.' . uniqid();
mkdir($tmpdir);
echo "Downloading tiles";
$i = 0;
for ($y = 0; $y < $tiles_y; $y++)
{
for ($x = 0; $x < $tiles_x; $x++)
{
echo ".";
copy($tiles_url . $z . '-' . $x . '-' . $y . '.jpg', $tmpdir . '/tile-' . sprintf('%05d', $i++) . '.jpg');
}
}
echo PHP_EOL;
echo "Assembling tiles...";
$cmd = 'montage -quality 95 -mode concatenate -tile ' . ($tiles_x) . 'x' . ($tiles_y) . ' ' . $tmpdir . '/tile-*.jpg ' . escapeshellarg($filename);
#echo $cmd . PHP_EOL;
system($cmd);
// Adding comment
system('exiv2 -c ' . escapeshellarg($comment) . ' ' . escapeshellarg($filename));
system('rm -rf ' . escapeshellarg($tmpdir));
echo "OK" . PHP_EOL;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment