5/5 (1 votes)
Voilà quand on s'ennuir, ca ça donne ça :D
Dites moi s'ke vous en pensez :)
CODE PHP
<?php
function upload_img($nom_champ, $destination, $ext_auto, $taille_max, $nom, $largeur=false, $hauteur=false){
/*
by IgiX <hervet.geoffrey@gmail.com > 12/11/2oo8
@Return : mixed
> false en cas d'échec
> URL image en cas de succès
@$nom_champ => name du <input type="file" /> / 'image'
@destination => lien du dossier / './images' => non existance => false
@ext_autao => extensions autorisée / arrey('png', 'gif')
@taille_max => Poids max en octet / 100*1024 100Ko
@nom => nom de l'image / 'image'.mt_rand('0, 13371337').'_'
*/
//Verification existence
if(!isset($_FILES[$nom_champ]))
return false;
//Verification dossier
if(!is_dir($destination))
return false;
//Vérification erreurs
if($_FILES[$nom_champ]['error'] != 0)
return $_FILES[$nom_champ]['error'];
extract(pathinfo($GLOBALS['_FILES'][$nom_champ]['name']));
//Vérification extension
if(!in_array($extension, $ext_auto))
return false;
//Vérification poids
if($_FILES[$nom_champ]['size'] > $taille_max)
return false;
list($largeur_up, $hauteur_up, $constante_image_type, $width_height_html, $bits, $channles, $mime) = getimagesize($_FILES[$nom_champ]['tmp_name']);
//Vérification d'une largeur et d'une hauteur
if(($largeur_up * $hauteur_up) == 0)
return false;
//Si redimensionnement non demandé
$url_image = $destination.'/'.$nom.'.'.$extension;
if( !is_numeric($hauteur) OR !is_numeric($largeur) OR ($largeur * $hauteur) <1){
if(move_uploaded_file($_FILES[$nom_champ]['tmp_name'], $url_image))
return $url_image;
return false;
}
if($_FILES[$nom_champ]['type'] == 'image/jpeg')
$type = 'jpeg';
if($_FILES[$nom_champ]['type'] == 'image/gif')
$type = 'gif';
if($_FILES[$nom_champ]['type'] == 'image/png')
$type = 'png';
if(!isset($type))
return false;
$fonctioncreatfrom = 'imagecreatefrom'.$type;
$image_fonction = 'image'.$type;
$image_sourcee = $fonctioncreatfrom ($_FILES[$nom_champ]['tmp_name']);
$image_destination = imagecreatetruecolor($largeur, $hauteur);
imagecopyresampled($image_destination, $image_sourcee, 0, 0, 0, 0, $largeur, $hauteur, $largeur_up, $hauteur_up);
$image_fonction ($image_destination, $url_image);
return $url_image;
}