Ver Mensaje Individual
  #1 (permalink)  
Antiguo 13/08/2010, 03:30
Maydur
 
Fecha de Ingreso: agosto-2010
Mensajes: 14
Antigüedad: 16 años, 2 meses
Puntos: 1
Imagenes en negro

Buenas, vengo con otro problemilla.

He montado un codigo para subir imagenes al servidor y que les ajuste el tamaño hasta un maximo.

Código PHP:
$consulta = "INSERT INTO aires (nombre_aire, idmarca, precio, oferta, descripcion) values ('$nombre_aire','$idmarca','$precio', '$oferta','$descripcion')";
mysql_query($consulta,$db) or die ("Error: ".mysql_error()); 

$dir='images/aires';
$thumbdir= $dir . '/thumbs';
list (
$width, $height, $type, $attr) = getimagesize ($_FILES['subeimagen'] ['tmp_name']);
$error= 'The file you uploaded was not a supported filetype.';
switch (
$type) {
case 
IMAGETYPE_GIF:
    
$image=imagecreatefromgif($_FILES['subeimagen'] ['tmp_name']) or die ($error);
    break;
case 
IMAGETYPE_JPEG:
    
$image=imagecreatefromjpeg($_FILES['subeimagen'] ['tmp_name']) or die ($error);
    break;
case 
IMAGETYPE_PNG:
    
$image=imagecreatefrompng($_FILES['subeimagen'] ['tmp_name']) or die ($error);
    break;
default:
    die(
$error);
}
$last_id = mysql_insert_id();
$imagename = $last_id;

$query = "UPDATE aires SET image_filename = ('$imagename') WHERE id_aire = ('$last_id')";
$result = mysql_query($query, $db) or die (mysql_error($db));
// Establecer un ancho y alto máximo
$image_width = 200;
$image_height = 200;

// Obtener las nuevas dimensiones
$ratio_orig = $width/$height;

if (
$image_width/$image_height > $ratio_orig) {
   
$image_width = $image_height*$ratio_orig;
} else {
   
$image_height = $image_width/$ratio_orig;
}
// Redimensionar
$image = imagecreatetruecolor($image_width, $image_height);
imagecopyresampled($image, $image, 0, 0, 0, 0, $image_width, $image_height, $width, $height);
//guarda la imagen
imagejpeg($image, $dir . '/' . $imagename . '.jpg');


//crea una copia al 50% y la guarda en la carpeta thumbs
$thumb_width = $image_width * 0.50;
$thumb_height = $image_height * 0.50;
$thumb = imagecreatetruecolor ($thumb_width, $thumb_height);
imagecopyresampled ($thumb, $image, 0, 0, 0, 0, $thumb_width, $thumb_height, $image_width, $image_height);
imagejpeg ($thumb, $thumbdir . '/' . $imagename . '.jpg');
imagedestroy($thumb);
?> 
Lo hace todo correctamente, subo una imagen de cualquier tamaño y la ajusta hasta un maximo de 200x200, la guarda en su carpeta y luego en la carpeta thumbs me guarda otra copia al 50%.

El problema es que las imagenes que me guarda estan totalmente en NEGRO, no hay nada....
Eso si el tamaño y el formato estan bien.

Se que el fallo esta en esta parte de codigo:

Código PHP:
// Establecer un ancho y alto máximo
$image_width = 200;
$image_height = 200;

// Obtener las nuevas dimensiones
$ratio_orig = $width/$height;

if (
$image_width/$image_height > $ratio_orig) {
   
$image_width = $image_height*$ratio_orig;
} else {
   
$image_height = $image_width/$ratio_orig;
}
// Redimensionar
$image = imagecreatetruecolor($image_width, $image_height);
imagecopyresampled($image, $image, 0, 0, 0, 0, $image_width, $image_height, $width, $height); 
Ya que sin eso funciona perfecto pero sin cambiar el tamaño.
Pero no se que es lo que esta mal.