Seguendo alcune guide, sono arrivare questo codice (per ora abbastanza sporco), che funge per JPG e GIF; Per quanto riguarda le PNG, soffre di un brutto problema, tutte le thumbs hanno lo sfondo nero!
Come si fa a farlo in modo corretto?
Ecco il codice:
codice:
// Directory in cui uppare l'immagine
$uploaddir = IMG_ANN;
// Nome dell'immagine da uppare
$final_filename = PRE_IMG_GR."{$infoAnnuncio['idUtente']}{$infoAnnuncio['numeroCasuale']}".POST_IMG_GR;
// Path completo per l'update
$nuovaImg = $uploaddir.$final_filename;
if( $dataBase->inserisciAnnuncio($infoAnnuncio) ) {
print "Annuncio inserito!
";
// ID dell'annuncio appena inserito
$idAnnuncioInserito = $dataBase->getIdAnnuncio($infoAnnuncio['idUtente'],$infoAnnuncio['ipInvioAnnuncio'] );
print "Id Ultimo annuncio inserito : ".$idAnnuncioInserito."
";
if (is_uploaded_file($_FILES['immagine']['tmp_name'])) { // Controllo di un corretto upload
// Parametri dell'immagine inserita
list($width, $height, $type, $attr) = getimagesize($_FILES['immagine']['tmp_name']);
if( $type == 2 ) {
$nuovaImg .= ".jpg";
}
elseif( $type == 1 ) {
$nuovaImg .= ".gif";
}
elseif( $type == 3 ) {
$nuovaImg .= ".png";
}
// Se la copia va a buon fine si crea la thumbs
if( copy($_FILES['immagine']['tmp_name'], $nuovaImg) ) {
//if( move_uploaded_file($_FILES['immagine']['tmp_name'], $nuovaImg) ) {
// Imposta i permetti all'immagine appena inserita
chmod($nuovaImg, 0777);
$src = $_FILES['immagine']['tmp_name'];
print "TIPO : ".$type."
";
// Controllo tipo immagine
if( $type == 2 ) {
$source = imagecreatefromjpeg($src);
}
elseif( $type == 1 ) {
$source = imagecreatefromgif($src);
}
elseif( $type == 3 ) {
$source = imagecreatefrompng($src);
}
if( $type != 1 ) {
$thumb = imagecreatetruecolor(DIM_THUMBS, DIM_THUMBS);
imagecopyresampled($thumb, $source, 0, 0, 0, 0, DIM_THUMBS, DIM_THUMBS, $width, $height);
}
else {
$thumb = imagecreate(DIM_THUMBS, DIM_THUMBS);
imagecopyresized($thumb, $source, 0, 0, 0, 0, DIM_THUMBS, DIM_THUMBS, $width, $height);
}
$uploaddir = IMG_ANNUNI_THUMB;
$nomeThumb = PRE_IMG_PC."{$infoAnnuncio['idUtente']}{$infoAnnuncio['numeroCasuale']}".POST_IMG_PC;
$nuovaImgThumb = $uploaddir.$nomeThumb;
print "Immagine thumbs : ".$nuovaImgThumb."
";
// Salvo l'immagine ridimensionata
//imagegif($thumb, $nuovaImgThumb);
if( $type == 2 ) {
$nuovaImgThumb .= ".jpg";
}
elseif( $type == 1 ) {
$nuovaImgThumb .= ".gif";
}
elseif( $type == 3 ) {
$nuovaImgThumb .= ".png";
}
if( $type == 2 ) {
@imagejpeg($thumb, $nuovaImgThumb, 100);
//@imagejpeg($thumb, '', 100);
}
elseif( $type == 1 ) {
@imagegif($thumb, $nuovaImgThumb);
//@imagegif($thumb);
}
elseif( $type == 3 ) {
@imagepng($thumb, $nuovaImgThumb);
//@imagepng($thumb);
}
chmod($nuovaImgThumb, 0777);
imagedestroy($thumb);
$fotoDaInserire[0]['urlGrande'] = $final_filename;
$fotoDaInserire[0]['urlPiccola'] = $nuovaImgThumb;
$fotoDaInserire[0]['desc'] = "IMMAGINE";
$fotoDaInserire[0]['nome'] = "IMMAGINE";
$dataBase->inserisciFotoAnnuncio($idAnnuncioInserito, $fotoDaInserire);
print "UP OK!
";
}
else {
print "ERRORE UPLOAD IMG
";
}
}
else {
print "Non è stato uppato niente
";
}
THx!