Codice PHP:
<?php
function CreateThumb($type, $srcdir, $dstdir, $name, $tw, $th, $tc) {
$filetype = $type;
$src = "$srcdir/$name";
$dst = "$dstdir/$name";
if ($filetype == 'jpg') {
$srcImg = imagecreatefromjpeg("$src");
} else
if ($filetype == 'jpeg') {
$srcImg = imagecreatefromjpeg("$src");
} else
if ($filetype == 'png') {
$srcImg = imagecreatefrompng("$src");
} else
if ($filetype == 'gif') {
$srcImg = imagecreatefromgif("$src");
}
$origWidth = imagesx($srcImg);
$origHeight = imagesy($srcImg);
$thumbWidth = $tw;
$ratio = $origWidth / $thumbWidth;
$thumbHeight = $origHeight / $ratio;
$thumbImg = imagecreatetruecolor($thumbWidth, $thumbHeight);
imagecopyresized($thumbImg, $srcImg, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $origWidth, $origHeight);
if ($filetype == 'jpg') {
$res = imagejpeg($thumbImg, "$dst", $tc);
} else
if ($filetype == 'jpeg') {
$res = imagejpeg($thumbImg, "$dst", $tc);
} else
if ($filetype == 'png') {
$res = imagepng($thumbImg, "$dst");
} else
if ($filetype == 'gif') {
$res = imagegif($thumbImg, "$dst");
}
return ($res);
};
function filtranome($name, $type) {
// qui filtri con varie funzioni
$nuovonome = $name;
$nuovonome = str_replace(array(" ", ".",","), [""], $nuovonome); // elimino alcuni caratteri
$nuovonome = strtolower($nuovonome); // tutto in minuscolo
$nuovonome .= '_'.time().'.'.$type;
return ($nuovonome);
};
// controllo che non ci siano stati errori nell'upload (codice = 0)
if ($_FILES['uploadfile']['error'] == 0) {
// upload ok
// controllo formato
$format = $_FILES['uploadfile']['type'];
list($p, $type) = explode('/', $format); $type = strtolower($type); // p.es. da 'image/PNG' a "png"
if (!(in_array($_FILES['uploadfile']['type'], array('image/jpeg','image/png','image/gif','image/jpg','image/bmp','image/PNG'))))
die("Formato file non valido");
// copio il file dalla cartella temporanea a quella di destinazione mantenendo il nome originale
copy($_FILES['uploadfile']['tmp_name'], "file_caricati/".filtranome($_FILES['uploadfile']['name']), $type) or die("Impossibile caricare il file");
// upload terminato, stampo alcune info sul file
echo "[b]Upload terminato con successo[/b]
Le Informazioni riguardanti il File che hai appena caricato sono:
";
echo "Nome file originale: ".$_FILES['uploadfile']['name']."
";
echo "Nome file caricato: ".filtranome($_FILES['uploadfile']['name'], $type)."
";
echo "Dimensione file: ".$_FILES['uploadfile']['size']."
";
echo "Tipo MIME file: ".$_FILES['uploadfile']['type'];
} else {
// controllo il tipo di errore
if ($_FILES['uploadfile']['error'] == 2) {
// errore, file troppo grande (> 1MB)
die("Errore, file troppo grande: il massimo consentito è 1MB");
} else {
// errore generico
die("Errore generico");
}
}
// $type già settato
$srcdir = "file_caricati";
$dstdir = "miniature";
$name = filtranome($_FILES['uploadfile']['name']);
// dimensioni della miniatura da creare
$thumbWidth = 60; // larghezza
$thumbHeight = 60; // altezza
// livello di compressione della miniatura
$thumbComp = 90;
$tw = $thumbWidth;
$th = $thumbHeight;
$tc = $thumbComp;
$res = CreateThumb($type, $srcdir, $dstdir, $name, $tw, $th, $tc);
if (!$res) die("Impossibile salvare la miniatura");
?>