<?php
echo 'content="text/plain; charset=utf-8"';
// En versiones de PHP anteriores a la 4.1.0, debería utilizarse $HTTP_POST_FILES en lugar
// de $_FILES.
//$campo= "campo$_REQUEST[campo]-$_REQUEST[item]";
$campo= "$_REQUEST[campo]-$_REQUEST[item]";
$tipo = $_FILES["$campo"]['type'];
//$nombrecillo = $_FILES['myfile']['nombrecillo'];

if($tipo=="image/png") {$ext = ".png"; $imagen=1;	} 
elseif($tipo=="image/jpeg") {$ext = ".jpg"; $imagen=1;	}
elseif($tipo=="image/gif") {$ext = ".gif"; $imagen=1;	}
else {
	$ext = explode(".", $_FILES["$campo"]['name']);
$ext = ".".strtolower($ext[count($ext) - 1]);
	
//	$ext = "novalida";
	 $imagen=0;} 
	 
	 
	 $nombrecillo= $_REQUEST['nombrecillo'];
$name = "$nombrecillo";
$dir_subida = '/home/images_secure/';
$fichero_subido = $dir_subida ."full/".$name;
//// extrar coordenadas del exif 

$imagenX = $_FILES['fileUpload']['tmp_name'];
$coordenadas = leer_exif($imagenX);
//$link = "$_SESSION[url]mapero.php?lat=$coordenadas[lon]&lon=$coordenadas[lat]&zoom=16&id=$name";
if($coordenadas !='') { 
$coordenadas = "$coordenadas"."&id=$_REQUEST[campo_mapa]"."[0]";
$alerta = "alert(' Se han detectado coordenadas en los metadatos de la imagen y se ubicará el mapa en ese lugar.');";


}

if (move_uploaded_file($_FILES["$campo"]['tmp_name'], $fichero_subido)) {
 if($imagen==1) {
 	//// extrar coordenadas del exif 

$imagenX = $_FILES["$campo"]['tmp_name'];
$coordenadas = leer_exif("$fichero_subido");
//$link = "$_SESSION[url]mapero.php?lat=$coordenadas[lon]&lon=$coordenadas[lat]&zoom=16&id=$name";
if($coordenadas !='') { 
$coordenadas = "$coordenadas"."&id=$_REQUEST[campo_mapa]"."[0]";
$alerta = "alert(' Se han detectado coordenadas en los metadatos de la imagen y se ubicará el mapa en ese lugar.');";
}
 	
 	
 	echo miniaturizar($name,"150",$dir_subida);
	echo miniaturizar($name,"300",$dir_subida);
	echo miniaturizar($name,"600",$dir_subida);
 }
} else {
    echo "¡Posible ataque de subida de ficheros!\n";
}

function miniaturizar($file,$width,$directorio) {
//$archivo = $file;
$archivo = "$directorio"."full/".$file;

$file_info = getimagesize($archivo);
$ratio = $file_info[0] / $file_info[1];
$newwidth = $width;
$newheight = round($newwidth / $ratio);
$ext = explode(".", $file);
$ext = strtolower($ext[count($ext) - 1]);
if ($ext == "jpeg") $ext = "jpg";
switch ($ext) {
        case "jpg":
                $img = imagecreatefromjpeg($archivo);
             //   $img = imagecreatefromstring($archivo);
        break;
        case "png":
                $img = imagecreatefrompng($archivo);
        break;
        case "gif":
                $img = imagecreatefromgif($archivo);
        break;
}
$thumb = imagecreatetruecolor($newwidth, $newheight);
imagealphablending( $thumb, false );
imagesavealpha( $thumb, true );
imagecopyresampled($thumb, $img, 0, 0, 0, 0, $newwidth, $newheight, $file_info[0], $file_info[1]);
if($ext=="jpg") {
imagejpeg($thumb,$directorio.$width."/$file", 80);
}else {
imagepng($thumb,$directorio.$width."/$file", 9);
}
}


// Convertir un string "1/123" a su representación float
function exif_float($value) {
  $pos = strpos($value, '/');
  if ($pos === false) return (float) $value;
  $a = (float) substr($value, 0, $pos);
  $b = (float) substr($value, $pos+1);
  return ($b == 0) ? ($a) : ($a / $b);
} 

function leer_exif($file){
	//$file = "/var/www/html/milfs/images/gps.jpg";
	$exif = exif_read_data( $file );
if ( !empty($exif['GPSLongitude']) && !empty($exif['GPSLatitude']) ) {
    $d = (float) $exif['GPSLongitude'][0];
    $m = exif_float($exif['GPSLongitude'][1] );
    $s = exif_float( $exif['GPSLongitude'][2] );
     
    $gps_longitude = (float) $d + $m/60 + $s/3600;
    if ( $exif['GPSLongitudeRef'] == 'W')
        $gps_longitude = -$gps_longitude;
     
    $d = $exif['GPSLatitude'][0];
    $m = exif_float($exif['GPSLatitude'][1] );
    $s = exif_float( $exif['GPSLatitude'][2] );
     
    $gps_latitude = (float) $d + $m/60 + $s/3600;
    if ( $exif['GPSLatitudeRef'] == 'S')
        $gps_latitude = -$gps_latitude;
        if($gps_latitude !='') {
  $resultado =   "../mapa.php?lon=$gps_latitude&lat=$gps_longitude&zoom=18&exif=exif";
										  }else{$resultado ="";}
//$resultado = "$gps_longitude $gps_latitude";        
        
        return $resultado;
}
}

?>