Ciao a tutti, ho un problema con l'upload e ridimensionamento delle immagini.
Con le JPG tutto procede correttamente, ma se carica una GIF o una PNG, le immagini che escono sono interamente nere.
Dove sbaglio? Posto il codice.
codice:
if (is_uploaded_file($_FILES['image']['tmp_name'])) {
// Controllo che il file non superi 1 Mb
if ($_FILES['image']['size'] > 5000000) {
echo("
Il file non deve superare 5 Mb!! Torna <a href=\"javascript: history.go(-1)\">indietro</a>.</p>");
}
else{
list($width, $height, $type, $attr) = getimagesize($_FILES['image']['tmp_name']);
if (($type==1) || ($type==2) || ($type==3)) {
if($width > 726){
$newwidth = 726;
}
else{
$newwidth = $width;
}
$newheight = $newwidth * $height / $width;
$descr = mysql_real_escape_string($_POST[descrizione]);
$ora = time();
$sql = "insert into foto(id, utente, descrizione, ora) values(NULL, \"$_SESSION[utenteconnesso]\", \"$descr\", \"$ora\");";
$query = mysql_query($sql) or die (mysql_error());
$sql = "select max(id) as id from foto where utente = \"$_SESSION[utenteconnesso]\"";
$query = mysql_query($sql) or die(mysql_error());
$dati = mysql_fetch_assoc($query);
$ultimo_id = $dati[id];
$thumb = imagecreatetruecolor($newwidth, $newheight);
switch( $type )
{
case '1':
$img = imagecreatefromgif($_FILES['image']['tmp_name']);
break;
case '2':
$img = imagecreatefromjpeg($_FILES['image']['tmp_name']);
break;
case '3':
$img = imagecreatefrompng($_FILES['image']['tmp_name']);
break;
}
imagecopyresampled($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
switch($type) {
case '1':
$simg = imagegif($thumb, $_SERVER['DOCUMENT_ROOT']."/foto/".$ultimo_id.".gif", 80);
break;
case '2':
$simg = imagejpeg($thumb, $_SERVER['DOCUMENT_ROOT']."/foto/".$ultimo_id.".jpg", 80);
break;
case '3':
$simg = imagepng($thumb, $_SERVER['DOCUMENT_ROOT']."/foto/".$ultimo_id.".png", 80);
break;
}
header("Location: http://www.miosito.com/photo_profile.php?id=$_SESSION[utenteconnesso]");
if($width > 130){
$newwidth = 130;
}
else{
$newwidth = $width;
}
$newheight = $newwidth * $height / $width;
$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($_FILES['image']['tmp_name']);
imagecopyresampled($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
switch($type) {
case '1':
$simg = imagegif($thumb, $_SERVER['DOCUMENT_ROOT']."/p_foto/".$ultimo_id.".gif", 100);
break;
case '2':
$simg = imagejpeg($thumb, $_SERVER['DOCUMENT_ROOT']."/p_foto/".$ultimo_id.".jpg", 100);
break;
case '3':
$simg = imagepng($thumb, $_SERVER['DOCUMENT_ROOT']."/p_foto/".$ultimo_id.".png", 100);
break;
}
}
else{
echo("Formato non valido!!");
}
}
}
else{
echo("
Non hai caricato nessuna foto! Torna <a href=\"javascript: history.go(-1)\">indietro</a>!</p>");
}