Nahmd,
Zur allgemeinen Ergötzung und Selbstbedienung hier das (wie üblich) hochaufwendige™ Machwerk:
<?php
#===============================================================================
# $Id: thumbcache.php,v 1.14 2013/03/24 22:45:51 wolf Exp wolf $
#===============================================================================
#
# Provides Thumbnail-JPEG for Image-URL
#
# image= Image or Website URL. Mandatory.
# DEBUG= If set, error messages instead of replacement picture
#
#===============================================================================
$THUMB_WIDTH = 200;
$THUMB_HEIGHT = 300;
$THUMB_MINRATIO = 0.4;
$THUMB_MAXRATIO = 2.0;
$POS_CACHE_TIME = 7 * 24 * 3600; # 1 week
$NEG_CACHE_TIME = 1 * 6 * 3600; # 6 hours
$ERROR_IMAGE = '/i/w/sorry_160x80.gif';
$CACHE_DIR = '/tmp';
#-------------------------------------------------------------------------------
# Wikimedia commons needs non default User-Agent
#-------------------------------------------------------------------------------
$USER_AGENT = 'thumbcache.php @ http://geschichtskarten.openstreetmap.de/historische_objekte/; prereads, thumbnails and caches images to display in map popup info boxes. In case of trouble contact <XXX@YYY> or <UUU@VVV>.';
ini_set('user_agent', $USER_AGENT);
#-------------------------------------------------------------------------------
# Error handler
#-------------------------------------------------------------------------------
$URLs = array();
function Fail ($message) {
global $URLs;
if ($_GET['DEBUG']) {
header ("HTTP/1.0 400 Something went wrong.");
} else {
global $ERROR_IMAGE;
header ("HTTP/1.0 302 Not found.");
header ("Location: {$ERROR_IMAGE}");
}
header ("Content-Type: text/plain");
echo join (";\n", $URLs),"\n";
echo "ERROR: $message\n";
exit (1);
}
#-------------------------------------------------------------------------------
# Image URL
#-------------------------------------------------------------------------------
if (!preg_match("#^(https?://\S+)$#", $_GET['image'], $match)) {
Fail ("Parameter 'image=' missing or unknown protocol");
}
$url = $match[1];
#-------------------------------------------------------------------------------
# Checksum image url
#-------------------------------------------------------------------------------
$hash = md5 ($url);
#-------------------------------------------------------------------------------
# Cache entry
#-------------------------------------------------------------------------------
$cachePath = "{$CACHE_DIR}/{$hash}.jpg";
$message = "'$url' not in cache.";
#-------------------------------------------------------------------------------
# If not in cache, must fetch
#-------------------------------------------------------------------------------
$mustFetch = !file_exists ($cachePath);
#-------------------------------------------------------------------------------
# If in cache, check if stale
#-------------------------------------------------------------------------------
if (!$mustFetch) {
#-------------------------------------------------------------------------------
# Determine max age for positive and negative cache entries
#-------------------------------------------------------------------------------
$maxAge = filesize($cachePath)>0 ? $POS_CACHE_TIME : $NEG_CACHE_TIME;
#-------------------------------------------------------------------------------
# If too old, must fetch
#-------------------------------------------------------------------------------
if (time() - filemtime ($cachePath) > $maxAge) $mustFetch = TRUE;
}
if ($mustFetch || $_SERVER['HTTP_PRAGMA']=="no-cache" ||
$_SERVER['HTTP_CACHE_CONTROL']=='no-cache') {
$message = getThumb ($url, $cachePath);
touch ($cachePath);
}
#-------------------------------------------------------------------------------
# If still not in cache, throw error-
#-------------------------------------------------------------------------------
$size = @filesize ($cachePath);
if (!$size) Fail ($message);
#-------------------------------------------------------------------------------
# Deliver image from cache
#-------------------------------------------------------------------------------
header ("Content-Type: image/jpeg");
header ("Content-Length: $size");
readfile ($cachePath);
exit(0);
#-------------------------------------------------------------------------------
# If webpage instead of image retrieved, try to extract image url.
#-------------------------------------------------------------------------------
function getThumb ($url, $cachePath) {
global $URLs;
for ($redirect=0; $redirect<3; $redirect++) {
$URLs[] = $url;
#---------------------------------------------------------------
# Fetch content of URL
#---------------------------------------------------------------
$data = @file_get_contents ($url);
if ($data === FALSE) return "Couldn't fetch URL '$url'.";
$size = strlen($data);
#---------------------------------------------------------------
# Decode image.
#---------------------------------------------------------------
$image = @imagecreatefromstring ($data);
#---------------------------------------------------------------
# If image then :-)
#---------------------------------------------------------------
if ($image !== FALSE) break;
#---------------------------------------------------------------
# Else: OSM-Wiki and link to /w/images/ ?
#---------------------------------------------------------------
#src="/w/images/thumb/6/68/Stundenstein_Heideweg.jpg/800px-Stundenstein_Heideweg.jpg"
#---------------------------------------------------------------
if (preg_match ('#http://(wiki\.openstreetmap\.org)/#', $url, $host) &&
preg_match ('#src="(/w/images/thumb/[^<>"]+)"#', $data, $match)) {
$url = "http://{$host[1]}{$match[1]}";
$URLs[] = 'A';
continue;
}
#---------------------------------------------------------------
# Else: Link to //upload.wikimedia.org/* ?
#---------------------------------------------------------------
if (preg_match ('#href="(//upload.wikimedia.org/[^\\s"<>]+jpe?g)"#i', $data, $match)) {
$url = "http:{$match[1]}";
$URLs[] = 'B';
continue;
}
#---------------------------------------------------------------
# Else: Wikipedia page and link to /wiki/[Namespace}:* ?
#---------------------------------------------------------------
# <a href="/wiki/{Namespace}:{filename}jp[e]g" class="image"
#---------------------------------------------------------------
if (preg_match ('#//(\w+\.wikipedia\.org)/#', $url, $host) &&
preg_match ('#<a href="(/wiki/\w+:[^<>"]+jpe?g)"\s+class="image"[^>]*><img#i',
$data, $match)) {
$url = "http://{$host[1]}{$match[1]}";
$URLs[] = 'C';
continue;
}
#---------------------------------------------------------------
# Else: Wikipedia page and srcset ?
#---------------------------------------------------------------
# srcset="//upload.wikimedia.org/wikipedia/de/1/1a/Denkmal_sanitaetsrat_dr_baumann.jpg
#---------------------------------------------------------------
if (preg_match ('#srcset="(//upload.wikimedia.org/[^\\s"<>]+jpe?g)\\s#i', $data, $match)) {
$url = "http:{$match[1]}";
$URLs[] = 'D';
continue;
}
#---------------------------------------------------------------
# If no image then :-(
#---------------------------------------------------------------
break;
}
#-----------------------------------------------------------------------
# No image ?
#-----------------------------------------------------------------------
if ($image === FALSE) {
return "No image found for URL '$url' ($size bytes).";
}
#-----------------------------------------------------------------------
# Scale image
#-----------------------------------------------------------------------
global $THUMB_WIDTH;
global $THUMB_HEIGHT;
global $THUMB_MINRATIO;
global $THUMB_MAXRATIO;
$origWidth = imagesx($image);
$origHeight= imagesy($image);
#-----------------------------------------------------------------------
# Trim if neccessary
#-----------------------------------------------------------------------
$widthTrim = max (0, round (($origWidth - $origHeight*$THUMB_MAXRATIO) / 2));
$heightTrim= max (0, round (($origHeight- $origWidth /$THUMB_MINRATIO) / 2));
$trimmedWidth = $origWidth - 2 * $widthTrim;
$trimmedHeight= $origHeight- 2 * $heightTrim;
#-----------------------------------------------------------------------
# Scale down into given box preserving w/h-ratio. NO scale up.
#-----------------------------------------------------------------------
$scale = min (1, $THUMB_WIDTH/$trimmedWidth, $THUMB_HEIGHT/$trimmedHeight);
$thumbWidth = round ($scale * $trimmedWidth);
$thumbHeight= round ($scale * $trimmedHeight);
$thumb = imagecreatetruecolor($thumbWidth, $thumbHeight);
imagecopyresampled ($thumb, $image,
0, 0, $widthTrim, $heightTrim,
$thumbWidth, $thumbHeight, $trimmedWidth, $trimmedHeight);
#-----------------------------------------------------------------------
# Store thumbnail
#-----------------------------------------------------------------------
$tempFile = "{$cachePath}." . getmypid() . ".tmp";
if (imagejpeg ($thumb, $tempFile)) {
@chmod ($tempFile, 0440);
if (!rename ($tempFile, $cachePath)) {
@unlink ($cachePath);
@rename ($tempFile, $cachePath);
}
}
@unlink ($tempFile);
return '';
}
#===============================================================================
# $Id: thumbcache.php,v 1.14 2013/03/24 22:45:51 wolf Exp wolf $
#===============================================================================
?>
Gruß Wolf