我就是这样做的。做任何你想做的事,你可能需要编辑它。我也使用缓存。
public function Flat($Coors='', $Zoom=15, $Size='250x200'){
# Ok, on doit avoir des coordonnées valides
$Coors = array_map('trim', explode(',', $Coors));
$CoorsName = array();
foreach($Coors as $X){
if(!is_numeric($X)){
$Error = 1;
} else {
if($X[0] == '-'){
$CoorsName[] = substr($X, 0, 6);
} else {
$CoorsName[] = substr($X, 0, 5);
}
}
}
$Coors = implode(',', $Coors);
$CoorsName = implode('_', $CoorsName);
# Le zoom doit aussi être valide
if(!is_numeric($Zoom) OR !is_int($Zoom)){
$Error = 1;
}
# Maintenant on doit s'assurer de la grandeur
$Size = array_map('trim', explode('x', $Size));
foreach($Size as $X){
if(!is_numeric($X)){
$Error = 1;
}
}
$Size = implode('x', $Size);
# Ok, avant d'aller chercher l'image, on doit vérifier le cache
$ImageExist = false;
# Est-ce que les dossiers existes ?
$CachePath = '/home/.../cache/maps/flat/';
$PathLevel = array( $CachePath.$Zoom.'/',
$CachePath.$Zoom.'/'.$Size.'/',
$CachePath.$Zoom.'/'.$Size.'/'.$CoorsName.'/');
foreach($PathLevel as $Path){
if(!file_exists($Path)){
mkdir($Path, 0777);
}
}
# Chemin complet de l'image
$ImagePath = $PathLevel[2].$Coors.'.png';
$GoogleURL = 'http://maps.googleapis.com/maps/api/staticmap?';
$Args = array( 'center' => $Coors,
'zoom' => $Zoom,
'size' => $Size,
'markers' => 'icon:http://www.domain.com/pin.png|'.$Coors,
'sensor' => 'false');
$URL_GET = '';
foreach($Args as $Arg => $Value){
$URL_GET.= ($URL_GET != '' ? '&' : '') . $Arg.'='.$Value.'&';
}
$GoogleURL.= ($URL_GET != '' ? '?'.$URL_GET : '');
# On va vérifier si l'image existe maintenant
if(!file_exists($ImagePath)){
$Content = file_get_contents($GoogleURL);
$fHandle = fopen($ImagePath, 'a');
fwrite($fHandle, $Content);
fclose($fHandle);
// file_put_contents($ImagePath, $Content);
} else {
$ImageExist = true;
}
# Header requis
header('Pragma: public'); // Requis
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Cache-Control: private', false); // Requis
header('Content-Type: image/png');
header('Content-Transfer-Encoding: binary');
# On va afficher l'image maintenant
if($ImageExist === true){
header('Content-Length: '.filesize($ImagePath));
readfile($ImagePath);
} else {
header('Content-Length: '.strlen($Content));
echo $Content;
}
}