Skip to content

Instantly share code, notes, and snippets.

@bionoren
Created February 15, 2014 05:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bionoren/9015030 to your computer and use it in GitHub Desktop.
Save bionoren/9015030 to your computer and use it in GitHub Desktop.
Fetches background images from http://gtgraphics.de/ (some of which are used in Windows 8). Organizes by resolution.
<?php
//Fetching some of the older backgrounds requires the PHP rar extension (available through pecl). If this extension is not installed, those images will be skipped.
for($i = 1; true; $i++) {
$tmpDir = sys_get_temp_dir();
if(substr($tmpDir, -1) != '/') {
$tmpDir .= '/';
}
if(file_exists($tmpDir.'imageList'.$i)) {
$imageListPage = file_get_contents($tmpDir.'imageList'.$i);
} else {
$imageListPage = file_get_contents('http://gtgraphics.de/category/wallpapers/'.$i.'/');
file_put_contents($tmpDir.'imageList'.$i, $imageListPage);
}
if(strpos($imageListPage, 'Page '.$i.'</title>') === false) {
break;
}
print 'Fetched page list '.$i."\n";
preg_match_all('#href="(http://gtgraphics.de/image/.+?)"#', $imageListPage, $matches, PREG_SET_ORDER);
for($j = 0; $j < count($matches); $j++) {
$url = $matches[$j][1];
if(file_exists($tmpDir.md5($url))) {
$imagePage = file_get_contents($tmpDir.md5($url));
} else {
$imagePage = file_get_contents($url);
file_put_contents($tmpDir.md5($url), $imagePage);
}
print 'Fetched image page '.($j+1).'/'.count($matches);
preg_match_all('#href="(http://gtgraphics.de/(.+?.jpg))"\>(\d+x\d+)#', $imagePage, $imageMatches, PREG_SET_ORDER);
for($k = 0; $k < count($imageMatches); $k++) {
@mkdir('./images/'.$imageMatches[$k][3]);
$dest = './images/'.$imageMatches[$k][3].'/'.$imageMatches[$k][2];
if(!file_exists($dest)) {
file_put_contents($dest, file_get_contents($imageMatches[$k][1]));
}
print ' '.$imageMatches[$k][3];
}
if(function_exists('rar_open')) {
preg_match('# href="(http://gtgraphics.de/(.+?\.rar).*?)"#', $imagePage, $imageMatch);
if($imageMatch) {
$dest = $tmpDir.$imageMatch[2];
if(!file_exists($dest)) {
file_put_contents($dest, file_get_contents($imageMatch[1]));
}
$archive = @RarArchive::open($dest);
if($archive !== FALSE) {
$archive->setAllowBroken(true);
foreach($archive->getEntries() as $entry)
{
$name = $entry->getName();
if(substr($name, -4) != '.jpg') {
continue;
}
$entry->extract(null, $tmpDir.'tmp.jpg');
$size = getimagesize($tmpDir.'tmp.jpg');
$imageDest = './images/'.$size[0].'x'.$size[1].'/'.$nameMatch[1];
if(!file_exists($imageDest)) {
$entry->extract(null, $imageDest);
}
print ' '.$size[0].'x'.$size[1];
}
$archive->close();
} else {
print ' BROKEN ARCHIVE';
}
}
}
print "\n";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment