1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
| <?php
private function uploadFile($file, $name, $directory, $newname, $sizemax, $type,$constraint) {
//print_r($directory.$name);
copy($file, $directory.$name) or die ("Impossible d'uploader le fichier");
$fullpath = $directory.$this->uploadedFileName ; // chemin complet de l'image sur le serveur
// Si c'est un fichier image, on vérifie que c'est bien un fichier image (type MIME)
if ($constraint=="image") {
//echo $type;
if ($type!="image/jpeg" && $type!="image/png" && $type!="image/gif" && $type!="image/jpg") {
@unlink($fullpath);
echo '<script>alert("Ce n\'est pas un fichierr image ! + '.$type.'");</script>' ;
}
}
// redimensionnement si c'est un hom (home par exemple)
if ($constraint=="home") {
switch($type){
case "image/jpeg":
$function_image_create = "ImageCreateFromJpeg";
$function_image_new = "ImageJpeg";
break;
case "image/png":
$function_image_create = "ImageCreateFromPng";
$function_image_new = "ImagePNG";
break;
case "image/gif":
$function_image_create = "ImageCreateFromGif";
$function_image_new = "ImageGif";
break;
case "image/jpg":
$function_image_create = "ImageCreateFromJpeg";
$function_image_new = "ImageJpeg";
break;
case "image/pjpeg":
$function_image_create = "ImageCreateFromJpeg";
$function_image_new = "ImageJpeg";
break;
default:
@unlink($fullpath);
if ($newname=="avatar") {
echo '<script>alert("Votre avatar n\'est pas un fichier image, vous aurez donc l\'avatar par défaut !\n Vous pouvez toutefois le modifier dans votre profil.");</script>' ;
} else {
echo '<script>alert("Ce n\'est pas un fchier image !+'.$type.'");</script>' ;
}
exit;
break;
}
list($width, $height) = getimagesize($fullpath); // on récupère les dimensions de l'image
// si l'un des côté est supérieur à $sizemax, on redimensionne en conservant les proportions
if ($width>$sizemax || $height>$sizemax) {
$ratio = $height/$width;
$newheight = ($height > $width) ? $sizemax : $sizemax*$ratio;
$newwidth = $newheight/$ratio;
$home = ImageCreateTrueColor($newwidth,$newheight);
/**appel de la fonction a utiliser selon l'extension donnée**/
$source = @$function_image_create($fullpath);
ImageCopyResampled($home, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
@$function_image_new($home,$fullpath);
imagejpeg($home,NULL,$this->qualite);
}
}
?> |
Partager