Perdón ... vi que el código que pegué al ponerlo resumido no queda claro ... aquí lo pego entero ...
A esta página php llega un Post con contenido html y una imagen ... acá pego la gestión sobre la imagen que es lo que no puedo guardar en la base de datos ... me interesa solamente el nombre de la imagen ... no la ruta completa ...
Código PHP:
if ($imagen != "") {
$tipo_archivo = $_FILES["imagen"]['type'];
$tmp_name = $_FILES["imagen"]["tmp_name"];
$name = $_FILES["imagen"]["name"];
$datos = getimagesize($imagen);
$tiempo = time();
if ($name!="" and !(strpos($tipo_archivo, "gif") || strpos($tipo_archivo, "jpeg")))
{
$ok = 0;
$desde = $_SERVER[HTTP_REFERER];
header("Location: $desde&error=no_extension");
exit;
}
else {
$ok = 1;
}
if ($name!="" and $ok == "1") {
$ruta="../../images/home/";
$ruta_thumb = "../../images/home/thumb/";
$campo = $_FILES["imagen"];
function imgResample2 ($imagen, $dir="../../images/home/", $anchura=250, $anchura_thumb="100", $watermark="", $watermark_thumb="", $pos="L-T", $wmdist=0, $c1=95, $c2=97){
if($_FILES["imagen"]['name']!=''){
if($dir!=""){
if(!file_exists($dir)) mkdir($dir);
}
$name=$_FILES["imagen"]['name'];
$type=$_FILES["imagen"]['type'];
$image_name = $name;
$temp=$_FILES["imagen"]['tmp_name'];
if($type=="image/pjpeg"){
$img = @imagecreatefromjpeg($temp) or die("No se encuentra la imagen $image_name<br>\n");
}
if($_FILES["imagen"]['type']=="image/gif"){
$img = @imagecreatefromgif($temp) or die("No se encuentra la imagen $image_name<br>\n");
$image_name = str_replace(".gif", ".jpg", $image_name);
}
if($_FILES["imagen"]['type']=="image/x-png"){
$img = @imagecreatefrompng($temp) or die("No se encuentra la imagen $image_name<br>\n");
$image_name = str_replace(".png", ".jpg", $image_name);
}
imagealphablending($img, true);
$dimensiones = getimagesize($temp);
$ratio = ($dimensiones[0] / $anchura);
$altura = round($dimensiones[1] / $ratio);
$image = imagecreatetruecolor($anchura,$altura);
$FFF = imagecolorallocate($img, 255,255,255);
imagefill($image, 0, 0, $FFF);
imagecopyresampled ($image, $img, 0, 0, 0, 0, $anchura, $altura, $dimensiones[0], $dimensiones[1]);
$pos = explode("-",$pos);
$wmhorz=$pos[0];
$wmvert=$pos[1];
if($watermark != ''){
$logo = imagecreatefrompng($watermark);
$margen = $wmdist;
$logoW =imagesx($logo);
$logoH =imagesy($logo);
switch ($wmhorz){
case "L":
$xpos=$margen;
break;
case "R":
$xpos=$anchura-$logoW-$margen;
break;
}
switch ($wmvert){
case "T":
$ypos=$margen;
break;
case "B":
$ypos=$altura-$logoH-$margen;
break;
}
imagecopy($image, $logo, $xpos, $ypos, 0, 0, $logoW, $logoH);
imagedestroy($logo);
}
imagejpeg($image,"$dir$image_name", $c1
imagedestroy($image);
imagedestroy($img);
if($anchura_thumb != 0 || $anchura_thumb == $anchura || $anchura_thumb == "" || $anchura_thumb == NULL) {
$subdir = "thumb";
if($dir!=""){
if(!file_exists($dir.$subdir)) mkdir($dir.$subdir);
} else {
if(!file_exists($subdir)) mkdir($subdir);
}
$name=$_FILES["imagen"]['name'];
$type=$_FILES["imagen"]['type'];
$thumb_name = $subdir."/".$name;
$temp=$_FILES["imagen"]['tmp_name'];
if($type=="image/pjpeg"){
$img = @imagecreatefromjpeg($temp) or die("No se encuentra la imagen $image_name<br>\n");
}
if($_FILES["imagen"]['type']=="image/gif"){
$img = @imagecreatefromgif($temp) or die("No se encuentra la imagen $image_name<br>\n");
$thumb_name = str_replace(".gif", ".jpg", $thumb_name);
}
if($_FILES["imagen"]['type']=="image/x-png"){
$img = @imagecreatefrompng($temp) or die("No se encuentra la imagen $image_name<br>\n");
$thumb_name = str_replace(".png", ".jpg", $thumb_name);
}
imagealphablending($img, true);
$dimensiones = getimagesize($temp);
$ratio_thumb = ($dimensiones[0] / $anchura_thumb);
$altura_thumb = round($dimensiones[1] / $ratio_thumb);
$thumb = imagecreatetruecolor($anchura_thumb,$altura_thumb);
$FFF = imagecolorallocate($img, 255,255,255);
imagefill($thumb, 0, 0, $FFF);
imagecopyresampled ($thumb, $img, 0, 0, 0, 0, $anchura_thumb, $altura_thumb, $dimensiones[0], $dimensiones[1]);
if($watermark_thumb != ''){
$logo_thumb = imagecreatefrompng($watermark_thumb);
$margen = $wmdist;
$logoW_thumb =imagesx($logo_thumb);
$logoH_thumb =imagesy($logo_thumb);
switch ($wmhorz){
case "L":
$xposThumb=$margen/2;
break;
case "R":
$xposThumb=$anchura_thumb-$logoW_thumb-($margen/2);
break;
} //end switch hotz
switch ($wmvert){
case "T":
$yposThumb=$margen/2;
break;
case "B":
$yposThumb=$altura_thumb-$logoH_thumb-($margen/2);
break;
}
imagecopy($thumb, $logo_thumb, $xposThumb, $yposThumb, 0, 0, $logoW_thumb, $logoH_thumb);
imagedestroy($logo_thumb);
}
//TERMINA WATERMARK THUMB
imagejpeg($thumb,"$dir$thumb_name", $c2);
imagedestroy($thumb);
imagedestroy($img);
} else {
$thumb_name = $image_name;
} // end if != 0
//TERMINA THUMBNAIL
}
//$unique = time();
$resampled[0] = $dir.$image_name;
$resampled[1] = $dir.$thumb_name;
$resampled[2] = "<img src=\"$dir$image_name?i=$unique\">";
$resampled[3] = "<img src=\"$dir$thumb_name?i=$unique\">";
return $resampled;
}
# END imgResample2
imgResample2('imagen');
// ELIMINA IMAGEN ANTERIOR
$im = mysql_query("select home_imagen_1, home_imagen_1_thumb from home");
$img = mysql_fetch_array($im);
unlink("$ruta/$img[home_imagen_1]");
unlink("$ruta_thumb/$img[home_imagen_1_thumb]");
$clave = time();
mysql_query("update home set home_imagen_1='".$clave."-".$image_name."', home_imagen_1_thumb='".$clave."-".$thumb_name."'");
}
$desde = $_SERVER[HTTP_REFERER];
header ("Location: $desde");
exit;
}