Codice PHP:
<?php
//<!--
function GDVersion(){
if( !in_array('gd', get_loaded_extensions()) ) return 0;
elseif( isGD2supported() ) return 2;
else return 1;
}
function isGD2supported(){
global $GD2;
if( isset($GD2) AND $GD2 ) return $GD2;
else{
$php_ver_arr = explode('.', phpversion());
$php_ver = intval($php_ver_arr[0])*100+intval($php_ver_arr[1]);
if( $php_ver < 402 ){ // PHP <= 4.1.x
$GD2 = in_array('imagegd2',get_extension_funcs("gd"));
}
elseif( $php_ver < 403 ){ // PHP = 4.2.x
$im = @imagecreatetruecolor(10, 10);
if( $im ){
$GD2 = 1;
@imagedestroy($im);
}
else $GD2 = 0;
}
else{ // PHP = 4.3.x
$GD2 = function_exists('imagecreatetruecolor');
}
}
return $GD2;
}
function IsFormatSupported($format){
if( ($format == 'gif') AND (imagetypes() & IMG_GIF) )return true;
elseif( ($format == 'jpeg') AND (imagetypes() & IMG_JPG) )return true;
elseif( ($format == 'png') AND (imagetypes() & IMG_PNG) )return true;
else return false;
}
function resizeImage($im, $size, $w, $h, $uploaddir, $nomeFile, $estensioneFile) {
$newwidth = $size[0];
$newheight = $size[1];
if( $newwidth > $w ){
$newheight = ($w / $newwidth) * $newheight;
$newwidth = $w;
}
if( $newheight > $h ){
$newwidth = ($h / $newheight) * $newwidth;
$newheight = $h;
}
if( (GDVersion() == 2) AND ($size[2] != 1) ){
$new = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($new, $im, 0, 0, 0, 0, $newwidth, $newheight, $size[0], $size[1]);
}
else{
$new = imagecreate($newwidth, $newheight);
imagecopyresized($new, $im, 0, 0, 0, 0, $newwidth, $newheight, $size[0], $size[1]);
}
$previewFileName=$uploaddir."/".$nomeFile.$estensioneFile;
ImageJpeg($new,$previewFileName);
@imagedestroy($new);
}
function uploadFile($target, $w, $h, $nextPageOnError) {
$nome_file = $_FILES['file']['name'];
$tipo_file = $_FILES['file']['type'];
$dim_file = $_FILES['file']['size'];
$file_tmp = $_FILES['file']['tmp_name'];
$cod_err = $_FILES['file']['error'];
$estensioneFile = substr(basename($nome_file), strlen(basename($nome_file))-4);
$nomeCodificatoFile = time();
$tmpFileName = "buttami_".$nomeCodificatoFile;
$error[1] = "File troppo grande per il server";
$error[2] = "File troppo grande";
$error[3] = "Upload incompleto";
$error[4] = "File già esistente o nessun file inviato";
$error[6] = "Errore nella cartella dei file temporanei";
if ($cod_err==0){
$uploaddir = $_SERVER['DOCUMENT_ROOT']."$target";
$uploadfile = $uploaddir ."/". $tmpFileName . $estensioneFile;
$src = $uploadfile;
move_uploaded_file($file_tmp, $uploadfile);
$size = getimagesize($src);
if( $size[2] == 2 ){$im = @imagecreatefromjpeg($src);}
elseif( $size[2] == 1 ){$im = @imagecreatefromgif($src);}
elseif( $size[2] == 3 ){$im = @imagecreatefrompng($src);}
$tmpString=$nomeCodificatoFile."_preview";
resizeImage($im, $size, $w, $h, $uploaddir, $tmpString , $estensioneFile);
$w = 320;
$h = 240;
resizeImage($im, $size, $w, $h, $uploaddir, $nomeCodificatoFile, $estensioneFile);
@imagedestroy($im);
$previewFileName=$uploaddir."/".$tmpFileName.$estensioneFile;
unlink($previewFileName);
}
else { header("Location: ../../errore.php?motivo=uploadFile: $error[$cod_err]&continua=$nextPageOnError&invia=no"); exit; }
return $nomeCodificatoFile;
}
// -->
?>