Codice PHP:
/////////////////////////////////////////////////////////
// restituisce l'estensione del file in minuscolo
function getExtension($file){
$dots = explode(".", $file);
return strtolower(array_pop($dots));
}
////////////////////////////////////////////////////////
// upload file in una cartella definita con formato YmsHms_9999_nome.ext
function saveFile($file, $path){
global $_FILES;
$datastore = date("YmdHis");
$tmp = $_FILES[$file]['tmp_name'];
$nome = preg_replace('/[^0-9a-z._-]/i', '', $_FILES[$file]['name']);
$random = rand(1000,9999);
if (is_uploaded_file($tmp)) {
$miofile = $path . $datastore . "_" . $random . "_" . $nome;
if (@filesize($miofile) == 0 && @copy($tmp, $miofile)) {
return ($datastore . "_" . $random . "_" . $nome);
}
}
return false;
}
/////////////////////////////////////////////////////////
// creazione di un thumb, dato un file - MANTIENE LE PROPORZIONI
// le trasparenze gif sono NERE
function createThumb($file, $prefix_thumb, $max_width_thumb, $max_height_thumb){
if (is_file($file)) {
$ext = getExtension($file);
$filename = explode("/", $file);
$thumb = $prefix_thumb . array_pop($filename);
$thumb = implode("/", $filename) . "/" .$thumb;
$imageInfo = getimagesize($file);
$src_width = $imageInfo[0];
$src_height = $imageInfo[1];
$proporzioni_width = $src_width / $max_width_thumb;
$proporzioni_height = $src_height / $max_height_thumb;
if($proporzioni_width > $proporzioni_height){
$dest_width = $max_width_thumb ;
$dest_height = $src_height / $proporzioni_width;
}else{
$dest_width = $src_width / $proporzioni_height ;
$dest_height = $max_height_thumb;
}
switch ($ext){
case "jpg":
$src_img = imagecreatefromjpeg($file);
$dst_img = imagecreatetruecolor($dest_width,$dest_height);
imagecopyresampled ($dst_img, $src_img, 0, 0, 0, 0, $dest_width, $dest_height, $src_width, $src_height);
imagejpeg($dst_img, $thumb);
imagedestroy($src_img);
break;
case "jpeg":
$src_img = imagecreatefromjpeg($file);
$dst_img = imagecreatetruecolor($dest_width,$dest_height);
imagecopyresampled ($dst_img, $src_img, 0, 0, 0, 0, $dest_width, $dest_height, $src_width, $src_height);
imagejpeg($dst_img, $thumb);
imagedestroy($src_img);
break;
case "gif":
$src_img = imagecreatefromgif($file);
$dst_img = imagecreatetruecolor($dest_width,$dest_height);
imagecopyresampled ($dst_img, $src_img, 0, 0, 0, 0, $dest_width, $dest_height, $src_width, $src_height);
imagegif($dst_img, $thumb);
imagedestroy($src_img);
break;
default:
@copy($file, $thumb);
break;
} // switch
return $thumb;
} // if is file
return false;
}