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
| <?php
/* fonction utile pour connaitre l'extention d'un fichier */
function getExt ($sString, $lCase = true){
if(!strpos ($sString, '.') || !is_string ($sString))
return false;
$ext = substr($sString,strrpos($sString,'.')+1);
return ($lCase) ? strtolower($ext) : $ext;
}
$image_source_path = 'images/image.source.jpeg'; //image source
if(file_exists($image_source_path)){//on check si le fichier existe
$file_type = getExt($image_source_path); //on récupère l'extension du fichier
switch($file_type){ //on test l'extension
case'jpeg':
$isValid = true;
$imageBuffer = imagecreatefromjpeg($image_source_path);
break;
case'jpg':
$isValid = true;
$imageBuffer = imagecreatefromjpeg($image_source_path);
break;
case'gif':
$isValid = true;
$imageBuffer = imagecreatefromgif($image_source_path);
break;
case'png':
$isValid = true;
$imageBuffer = imagecreatefrompng($image_source_path);
break;
default:
$isValid = false;
exit('ce n\'est pas une image');
break;
}
if($isValid){ //si c'est une image
$heightMax = 240;
$weightMax = 320;
$heightImage = imagesy($imageBuffer); //on prend sa hauteur
$weightImage = imagesx($imageBuffer); //et sa largeur
$curr_height=$heightImage;
$curr_weight=$weightImage;
if($weightImage > $weightMax){
$ratio = $weightImage / $weightMax;
$newHeight = ($heightImage / $ratio) -1;
//la suite ???
//ajout Raideman
$newWeight=$weightMax;
$curr_height=$newHeight;
$curr_weight=$newWeight;
}
//on a retravaillé en fonction de la largeur, on vérifie la hauteur maintenant
if ($curr_height>$heightMax) {
$ratio = $curr_height / $heightMax;
$newWeight = ($curr_weight / $ratio) -1;
$newHeight=$heightMax;
}
//On a les dimensions finales $newWeight et $newHeight
//on cree une image vide aux nouvelles dimensiosn
$im=imagecreatetruecolor(round($newWeight), round($newHeight));
//on copie dedans notre image retaillés
imagecopyresampled($im,$imageBuffer,0,0,0,0,round($newWeight),round($newHeight),$weightImage,$heightImage);
//$dest est le chemin de destination
imagejpeg($im,$dest);
}
}else{
exit($image_source_path.' n\'existe pas.');
}
?> |
Partager