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
| <?php
/**
* Classe de modification d'images.<br />Pour l'instant seule le redimensionnement et possible.
*/
class Image {
/**
* Image à modifier.
*/
private $img;
/**
* Type de l'image à modifier.
*/
private $type;
/**
* Image modifiée.
*/
private $dest;
/**
* Le constructeur appel la méthode setImage.
*
* @param $img Chemin vers l'image originale.
*/
public function __construct($img='./') {
$this->setImage($img);
}
/**
* Définis le chemin vers l'image à modifier.
*
* @param $img Chemin vers l'image originale.
*/
public function setImage($img='./') {
if(!($this->type = exif_imagetype($img))) throw new Exception("<stong>".$img."</stong> n'est pas une image");
$this->img = $img;
}
/**
* Redimensionne l'image originale.
*
* @param $width Largeur voulue pour l'image redimentionnée.
*
* @param $height Longueur voulue pour l'image redimentionnée.
*
* @param $percent Si True, $width et $height sont compris comme des rapport d'agrandissement et non comme des valeurs absolues.
*
* @param $proportional Si True, les proportions de l'image originale sont gardés.
*/
public function resize($width=0, $height=0, $percent=False, $proportional=True) {
if(!is_numeric($width) || !is_numeric($height) || $width <= 0 || $height <= 0)
throw new Exception("<strong>Les dimensions ".$width."x".$height."</strong> ne sont pas valides.");
$imgInfo = getImageSize($this->img);
//dimensions de l'image d'orgine :
$initWidth = $imgInfo[0];
$initHeight = $imgInfo[1];
if($percent) {
$width = $initWidth * ($width/100);
$height = $initHeight * ($height/100);
}
if($proportional) {
if($initWidth > $initHeight) $height = $initHeight * ($width/$initWidth);
else $width = $initWidth * ($height/$initHeight);
}
if($this->type == 2) $src = imagecreatefromjpeg($this->img);
else throw new Exception("Ce type d'images n'est pas supporté pour l'instant.");
$this->dest = imagecreate($width, $height);
imagecopyresampled($this->dest, $src, 0, 0, 0, 0, $width, $height, $initWidth, $initHeight);
}
/**
* Enregistre l'image modifiée.
*/
public function save($dir, $qualite=100) {
if($this->type == 2) imagejpeg($this->dest, $dir, $qualite);
else throw new Exception("Ce type d'images n'est pas supporté pour l'instant.");
}
}
?> |
Partager