Cuando subimos una imagen por medio de flash con Filereference, recibimos el archivo en upload.php de esta forma y lo guarda perfecto en el servidor:
Código :
<?php $uploadFile = $_FILES['Filedata']['name']; move_uploaded_file($_FILES['Filedata']['tmp_name'], $uploadFile); ?>
Hasta aquí todo marcha bien ....
Lo que quiero hacer es incluir la libreria GD, para que me guarde la imagen con su miniatura, tal y como lo hago con un formulario html y php como lo muestro a continuación.
Código :
<?
$mensaje = "";
if(isset($_POST['enviar'])){
// Mime types permitidos
$mimetypes = array("image/jpeg", "image/pjpeg", "image/gif", "image/png");
//
// Obtenemos la información de la imagen
$name = $_FILES['archivo']['name'];
$type = $_FILES['archivo']['type'];
$size = $_FILES['archivo']['size'];
$tmp_name = $_FILES['archivo']['tmp_name'];
$error = $_FILES['archivo']['error'];
//
$archivoTemp = getimagesize($_FILES['archivo'] ['tmp_name']);
//
//
// Verificamos si el archivo es una imagen válida
if(!in_array($type, $mimetypes)){
$mensaje = "El archivo que intentas subir no es una imagen válida";
} else {
$mensaje = "Imagen guardada";
}
// Creamos el thumbnail
switch($type) {
case $mimetypes[0]:
case $mimetypes[1]:
$imagen = imagecreatefromjpeg($tmp_name);
break;
case $mimetypes[2]:
$imagen = imagecreatefromgif($tmp_name);
break;
case $mimetypes[3]:
$imagen = imagecreatefrompng($tmp_name);
break;
}
// Definimos la medida máxima
$thumb_max = 120; // de la muestra (thumbnail)
$detalle_max = 420; // de la imagen detalle
// Evaluamos si la imagen es horizontal
if($archivoTemp[0]>$archivoTemp[1]) {
// Definimos las medidas de las imagenes
$thumb_w = $thumb_max;
$thumb_h = ($archivoTemp[1]/$archivoTemp[0])*$thumb_max;
$detalle_w = $detalle_max;
$detalle_h = ($archivoTemp[1]/$archivoTemp[0])*$detalle_max;
} else {
$thumb_w = ($archivoTemp[0]/$archivoTemp[1])*$thumb_max;
$thumb_h = $thumb_max;
$detalle_w = ($archivoTemp[0]/$archivoTemp[1])*$detalle_max;
$detalle_h = $detalle_max;
}
// Creamos las imágenes
$thumb = imagecreatetruecolor($thumb_w,$thumb_h);
$detalle = imagecreatetruecolor($detalle_w,$detalle_h);
// Copiamos la original escalada
imagecopyresampled($thumb,$imagen,0,0,0,0, $thumb_w,$thumb_h,imagesx($imagen),imagesy($imagen));
imagecopyresampled($detalle,$imagen,0,0,0,0, $detalle_w,$detalle_h,imagesx($imagen),imagesy($imagen));
// Destruimos la imagen original
imagedestroy($imagen);
//
//
define("carpetaThumbs", "imagenes/thumbs/");
define("carpetaDetalles", "imagenes/");
//damos salida a la imagen
switch($type) {
case $mimetypes[0]:
case $mimetypes[1]:
imagejpeg($thumb, carpetaThumbs.$name);
imagejpeg($detalle, carpetaDetalles.$name);
break;
case $mimetypes[2]:
imagegif($thumb, carpetaThumbs.$name);
imagegif($detalle, carpetaDetalles.$name);
break;
case $mimetypes[3]:
imagepng($thumb, carpetaThumbs.$name);
imagepng($detalle, carpetaDetalles.$name);
break;
}
// Destruimos las imagenes temporales
imagedestroy($thumb);
imagedestroy($detalle);
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Subir imagenes</title>
</head>
<body>
<form action="" method="post" enctype="multipart/form-data" name="form1">
<label>Imagen 1:</label>
<p>
<input type="file" name="archivo" />
</p>
<p>
<label for="Submit"></label>
<input type="submit" name="enviar" value="Submit"/>
</p>
<p><? echo $mensaje?></p>
</form>
</body>
</html>Necesito hacer esto con Flash + php y de esta forma no me funciona.
Nota: En el servidor esta activa la librería GD y las carpetas donde se guardarán las imagenes tienen todos los permisos (777).
Código :
<?php
// Mime types permitidos
$mimetypes = array("image/jpeg", "image/pjpeg", "image/gif", "image/png");
//
// Obtenemos la información de la imagen
$name = $_FILES['Filedata']['name'];
$type = $_FILES['Filedata']['type'];
$size = $_FILES['Filedata']['size'];
$tmp_name = $_FILES['Filedata']['tmp_name'];
$error = $_FILES['Filedata']['error'];
//
$archivoTemp = getimagesize($_FILES['Filedata'] ['tmp_name']);
//
// Creamos el thumbnail
switch($type) {
case $mimetypes[0]:
case $mimetypes[1]:
$imagen = imagecreatefromjpeg($tmp_name);
break;
case $mimetypes[2]:
$imagen = imagecreatefromgif($tmp_name);
break;
case $mimetypes[3]:
$imagen = imagecreatefrompng($tmp_name);
break;
}
// Definimos la medida máxima
$thumb_max = 120; // de la muestra (thumbnail)
$detalle_max = 420; // de la imagen detalle
// Evaluamos si la imagen es horizontal
if($archivoTemp[0]>$archivoTemp[1]) {
// Definimos las medidas de las imagenes
$thumb_w = $thumb_max;
$thumb_h = ($archivoTemp[1]/$archivoTemp[0])*$thumb_max;
$detalle_w = $detalle_max;
$detalle_h = ($archivoTemp[1]/$archivoTemp[0])*$detalle_max;
} else {
$thumb_w = ($archivoTemp[0]/$archivoTemp[1])*$thumb_max;
$thumb_h = $thumb_max;
$detalle_w = ($archivoTemp[0]/$archivoTemp[1])*$detalle_max;
$detalle_h = $detalle_max;
}
// Creamos las imágenes
$thumb = imagecreatetruecolor($thumb_w,$thumb_h);
$detalle = imagecreatetruecolor($detalle_w,$detalle_h);
// Copiamos la original escalada
imagecopyresampled($thumb,$imagen,0,0,0,0, $thumb_w,$thumb_h,imagesx($imagen),imagesy($imagen));
imagecopyresampled($detalle,$imagen,0,0,0,0, $detalle_w,$detalle_h,imagesx($imagen),imagesy($imagen));
// Destruimos la imagen original
imagedestroy($imagen);
//
//
define("carpetaThumbs", "thumbs/");
//damos salida a la imagen
switch($type) {
case $mimetypes[0]:
case $mimetypes[1]:
imagejpeg($thumb, carpetaThumbs.$name);
imagejpeg($detalle, $name);
break;
case $mimetypes[2]:
imagegif($thumb, carpetaThumbs.$name);
imagegif($detalle, $name);
break;
case $mimetypes[3]:
imagepng($thumb, carpetaThumbs.$name);
imagepng($detalle, $name);
break;
}
// Destruimos las imagenes temporales
imagedestroy($thumb);
imagedestroy($detalle);
?>Muchas gracias de antemano ...
