codice:
if($_FILES[foto][size] > 0){ //se è stata inviata una foto
if($_FILES[foto][type]!="application/x-zip-compressed"){//se si tratta di un singolo jpeg
addPhoto($_FILES[foto][tmp_name], $_POST[ide], $_POST[aid], $conn);
print "<span class=\"testo\">La foto è stata inserita correttamente. Puoi inserirne un'altra o tornare all'evento.
";
}
else{ //è stato inviato uno zip
require_once('../../pclzip.lib.php');
$archive = new PclZip($_FILES[foto][tmp_name]);
$list = $archive->extract(PCLZIP_OPT_PATH, "../../eventi/temp",
PCLZIP_OPT_REMOVE_ALL_PATH);
for($n=0; $n<count($list); $n++){ //per ogni file nell'archivio
if(!$list[$n][folder]){//se è un file
addPhoto($list[$n][filename], $_POST[ide], $_POST[aid], $conn);
unlink($list[$n][filename]);
}
else{
rmdir($list[$n][filename]);
}
}
}
//la funzione addPhoto è questa:
function addPhoto($filename, $idevento, $aid, $conn){
mysql_query("...")or die("Errore nell'inserimento della foto: ".mysql_error());
$r=mysql_query("...", $conn);
$ri=mysql_fetch_row($r);
copyResized($filename, "../../eventi/foto/".$ri[0].".jpg", 485, '*', 'image/pjpeg') or die("Impossibile copiare la foto");
}
//e la funzione copyResized è questa:
function copyResized($source, $dest, $w, $h, $mime){
if($mime=='image/pjpeg') $image = imagecreatefromjpeg($source);
elseif($mime=='image/gif') $image = imagecreatefromgif($source);
elseif($mime=='image/x-png') $image = imagecreatefrompng($source);
else return false;
if ($image === false) return false;
// Get original width and height
$width = imagesx($image);
$height = imagesy($image);
if($w=='*' && $h=='*'){
copy($source, $dest);
return true;
}
if($h=='*'){
if($width<=$w){
copy($source, $dest);
return true;
}
$h=$height * ($w/$width);
}
elseif($w=='*'){
if($height<=$h){
copy($source, $dest);
return true;
}
$w=$width * ($h/$height);
}
else{
if($height<=$h && $width<=$w){
copy($source, $dest);
return true;
}
if($height/$h > $width/$w)
$w=$width * ($h/$height);
else
$h=$height * ($w/$width);
}
$image_resized = imagecreatetruecolor($w, $h);
imagecopyresampled($image_resized, $image, 0, 0, 0, 0, $w, $h, $width, $height);
if($mime=='image/pjpeg') imagejpeg($image_resized, $dest);
elseif($mime=='image/gif') imagegif($image_resized, $dest);
elseif($mime=='image/x-png') imagepng($image_resized, $dest);
else return false;
return true;
}