Je cherche un moyen de diminuer la taille des photos uploader vers mon site, car actuellement les photos sont trop importantes et l'upload fonctionne mal du coup. Ca donne ceci comme message :
Code : Tout sélectionner
Fatal error: Allowed memory size of 12582912 bytes exhausted (tried to allocate 11392 bytes) in photo.php on line 30
Vu que je ne peux pas diminuer la résolution avec l'appareil photo, je cherche un moyen de diminuer l'image avant l'upload.
Voici mon code de l'upload pour le moment :
function thumbail($rep, $file, $maxWidth, $maxHeight){//Créé une image à partir de $file
$img = ImageCreateFromJpeg($rep.$file); ------->ligne 30<-------
//Dimensions de l'image
$imgWidth = imagesx($img);
$imgHeight = imagesy($img);
//Facteur largeur/hauteur des dimensions max
$whFact = $maxWidth/$maxHeight;
//Facteur largeur/hauteur de l'original
$imgWhFact = $imgWidth/$imgHeight;
//fixe les dimensions du thumb
if($whFact < $imgWhFact){//Si largeur déterminante
$thumbWidth = $maxWidth;
$thumbHeight = $thumbWidth/$imgWhFact;
} else { //Si hauteur déterminante
$thumbHeight = $maxHeight;
$thumbWidth = $thumbHeight*$imgWhFact;
}
//Crée le thumb (image réduite)
$imgThumb = ImageCreateTruecolor($thumbWidth, $thumbHeight);
//Insère l'image de base redimensionnée
ImageCopyResized($imgThumb, $img, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $imgWidth, $imgHeight);
//Nom du fichier thumb
//$nom_file = basename($path);
$imgThumbName = "v".$file;
//Crée le fichier thumb
$fp = fopen($rep.$imgThumbName, "w");
fclose($fp);
//Renvoie le thumb créé
ImageJpeg($imgThumb, $rep.$imgThumbName);
return $rep.$imgThumbName;
}
....
//Upload de la nouvelle image
$nomFichier = $_FILES["fichier_choisi"]["name"] ;
//nom temporaire sur le serveur:
$nomTemporaire = $_FILES["fichier_choisi"]["tmp_name"] ;
//type du fichier choisi:
$typeFichier = $_FILES["fichier_choisi"]["type"] ;
//poids en octets du fichier choisit:
$poidsFichier = $_FILES["fichier_choisi"]["size"] ;
//code de l'erreur si jamais il y en a une:
$codeErreur = $_FILES["fichier_choisi"]["error"] ;
//chemin qui mène au dossier qui va contenir les fichiers uplaod:
$chemin = "images/vehicule/" ;
move_uploaded_file($nomTemporaire, $chemin.$nomFichier);
if ($nomFichier != "")
chmod ($chemin.$nomFichier, 0644);
$extension = substr ( strrchr ( $nomFichier, '.' ) , 1 );
$tmpNom = "images/vehicule/".time().".".$extension;
rename($chemin.$nomFichier, $tmpNom);
if ($nomFichier != "")
chmod ($tmpNom, 0644);
$resNom = thumbail("images/vehicule/", basename($tmpNom), 200, 160);
if ($tmpNom != "")
chmod ($resNom, 0644);