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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
| if(is_uploaded_file($_FILES["upFile"]['tmp_name'])){
$width = 120; // Largeur maximum vignette
$height = 120; // Hauteur maximum vignette
$width2 = 800; // Largeur maximum image
$height2 = 200; // Hauteur maximum image
$file=$_FILES["upFile"]['tmp_name'];
$folder_big = 'upload/'; //Dossier d'enregistrement
$taille_maxi = 10000000; //Taille max
$taille = $_FILES['upFile']['size'];
$extension = strrchr($_FILES['upFile']['name'], '.');
$extensions = array('.jpg','.JPG','.png','.PNG','.gif','.GIF');
if(!in_array($extension, $extensions)){
alert("Veuillez choisir un fichier au format .jpg, .png ou .gif");
exit;
}
elseif($taille>$taille_maxi){
alert("Fichier trop volumineux");
exit;
}
else{
list($width_orig, $height_orig, $image_type) = getimagesize($file);
list($width_orig2, $height_orig2, $image_type2) = getimagesize($file);
//On garde l'échelle de l'image VIGNETTE
if (($width_orig > $height_orig) && ($width_orig > $width))
$height = (int) (($width / $width_orig) * $height_orig);
elseif($height_orig > $height)
$height = $height;
else
$height = $height_orig;
if (($height_orig > $width_orig) && ($height_orig > $height))
$width = (int) (($height / $height_orig) * $width_orig);
elseif($width_orig > $width)
$width = $width;
else
$width = $width_orig;
//On garde l'échelle de l'image BIG
if (($width_orig2 > $height_orig2) && ($width_orig2 > $width2))
$height2 = (int) (($width2 / $width_orig2) * $height_orig2);
elseif($height_orig2 > $height2)
$height2 = $height2;
else
$height2 = $height_orig2;
if (($height_orig2 > $width_orig2) && ($height_orig2 > $height2))
$width2 = (int) (($height2 / $height_orig2) * $width_orig2);
elseif($width_orig2 > $width2)
$width2 = $width2;
else
$width2 = $width_orig2;
//Création des miniatures avec leur fond respectif
$image_p = imagecreatetruecolor($width, $height);
$image_b = imagecreatetruecolor($width2, $height2);
$ext = strrchr($_FILES["upFile"]['name'], '.');
if(($ext == '.jpeg') || ($ext == '.jpg') || ($ext == '.JPEG') || ($ext == '.JPG')){
$file_ok = imagecreatefromjpeg($file);
$file_ok2 = imagecreatefromjpeg($file);
}
elseif ($ext == '.png') {
$file_ok = imagecreatefrompng($file);
$file_ok2 = imagecreatefrompng($file);
//Fond transparent (pour les png avec transparence)
imagesavealpha($image_p, true);
$trans_color = imagecolorallocatealpha($image_p, 0, 0, 0, 127);
imagefill($image_p, 0, 0, $trans_color);
}
elseif ($ext == '.gif') {
$file_ok = imagecreatefromgif($file);
$file_ok2 = imagecreatefromgif($file);
//Fond transparent (pour les gifs avec transparence)
$red = rand(0,255);
$green = rand(0,255);
$blue = rand(0,255);
$transparent = imagecolorallocate($image_p, $red, $green, $blue);
imagefill($image_p, 0, 0, $transparent);
imagecolortransparent($image_p, $transparent);
imagetruecolortopalette($image_p, false, 256);
$transparent2 = imagecolorallocate($image_b, $red, $green, $blue);
imagefill($image_b, 0, 0, $transparent2);
imagecolortransparent($image_b, $transparent2);
imagetruecolortopalette($image_b, false, 256);
}
imagecopyresampled($image_p, $file_ok, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
imagecopyresampled($image_b, $file_ok2, 0, 0, 0, 0, $width2, $height2, $width_orig2, $height_orig2);
$file = date("Ymd-His").$extension;
$file2 = 'big-'.$file;
//Ecriture de l'image
if(!is_writeable(dirname($folder_big)))
echo 'Impossible d\'enregistrer l\'image dans le dossier';
else {
if ($ext == '.png'){
imagepng($image_p, $folder_big.$file, 9);
imagepng($image_b, $folder_big.$file2, 9);
}
elseif ($ext == '.gif') {
imagegif($image_p, $folder_big.$file, 100);
imagegif($image_b, $folder_big.$file2, 100);
}
else {
imagejpeg($image_p, $folder_big.$file, 80);
imagejpeg($image_b, $folder_big.$file2, 80);
}
}
imagedestroy($image_p);
imagedestroy($file_ok);
imagedestroy($image_b);
imagedestroy($file_ok2);
echo 'Upload effectué avec succès !';
}
} |
Partager