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 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
| <?php
class ThumbnailImage{
private $image;
private $quality=100;
private $mimetype;
private $image_properties=array();
private $initial_file_size;
private $message=array();
//Constructeur de notre classe
function __construct($file,$thumbnailsize=100){//100 pixels
if(is_file($file)){
$this->initial_file_size=filesize($file); //Retourne la taile en bit de l'image
if($this->image_properties=getimagesize($file)){ //retourne un tableau contenant la largeur,la hauteur et le type de l'image
$this->mimetype=image_type_to_mime_type($this->image_properties[2]);
/* l'indice 0 correspond à la largeur du fichier
l'indice 1 correspond à la longueur du fichier
l'indice 2 correspond au type de fichier:jpeg,gif,..
Les types MIME sont par exemple :image/jpeg;image/png,..... */
//Création de l'image
switch($this->image_properties[2]){
case IMAGETYPE_JPEG://Cas où l'image est JPEG
$this->image=imagecreatefromjpeg($file);
break;
case IMAGETYPE_GIF://Cas où l'image est GIF
$this->image=imagecreatefromgif($file);
break;
case IMAGETYPE_PNG://Cas où l'image est PNG
$this->image=imagecreatefrompng($file);
break;
default:
$this->message[]="IMPOSSIBLE DE CREER CETTE IMAGE!";
}
if($this->createThumb($thumbnailsize)){
$this->message[]="CREATION DU CLICHE REDUIT AVEC SUCCES !";
}else{
$this->message[]="ECHEC DE CREATION DU CLICHE REDUIT !";
}//appel de la methode createThumb qui nous crée un thumbnail
}else{//cas où $file n'est un fichier
$this->message[]="TYPE DE FICHIER INCONNU!";
}
}else{//cas où aucun fichier n'a été donné en paramètre ou le fichier est corrompu
$this->message[]="FICHIER CORROMPU OU INEXISTANT!";
}
}//FIN CONSTRUCTEUR
public function getMessage(){
$output="";
foreach($this->message as $message){
$output.="{$message}<br />";
}
return $output;
}// Fin de la methode getMessage
private function calculateReduction($thumbnailsize){
$srcWidth = $this->image_properties[0];
$srcHeigth = $this->image_properties[1];
//ajustement
if($srcWidth < $srcHeigth){
$reduction = round($srcHeigth/$thumbnailsize);
}else{
$reduction = round($srcWidth/$thumbnailsize);
}
return $reduction;
}// Fin function calculateReduction($thumbnailsize)
private function createThumb($thumbnailsize){
//on récupère la largeur et la hauteur de l'image initiale
$srcWidth = $this->image_properties[0];
$srcHeigth = $this->image_properties[1];
if($srcWidth > $thumbnailsize || $srcHeigth > $thumbnailsize){// Cas où la longueur ou la largeur depasse la taille du cliché réduit que nous voulons
$reduction = $this->calculateReduction($thumbnailsize);
//On recupère les proportions pour créer le cliché réduit
$destWidth = $srcWidth/$reduction;
$destHeigth = $srcHeigth/$reduction;
$dest_image = imagecreatetruecolor($destWidth, $destHeigth);//On crée une image temporaire
if(imagecopyresampled($dest_image,$this->image,0,0,0,0,$destWidth, $destHeigth, $srcWidth, $srcHeigth)){//On réduit et on recopie l'ancienne image
imagedestroy($this->image); //On détruit l'ancienne image( sous forme de ressource créée initialement)
$this->image = $dest_image; //On réaffecte à l'attribut image la nouvelle image
return true;
}else{
return false;
}
}
}//fin de la function createThumb
public function getImage(){//Ici nous tentons d'afficher l'image que nous avons créée dans un navigateur
$typemime= $this->getMimeType();
header("Content-type: {$typemime}");
switch($this->image_properties[2]){
case IMAGETYPE_JPEG:
imagejpeg($this->image, "",$this->getQuality());
break;
case IMAGETYPE_PNG:
imagepng($this->image, "",$this->getQuality());
break;
case IMAGETYPE_GIF:
imagegif($this->image,$this->getQuality());
break;
default:
die("Impossible de créer cette image!");
}
}//public function getImage
public function getMimeType(){
return $this->mimetype;
}
public function getInitialFileSize(){
return $this->initial_file_size;
}//
public function setQuality($quality){
if($quality > 100 || $quality < 1){
$quality = 75;
}
if($this->image_properties[2] == IMAGETYPE_JPEG || $this->image_properties[2] == IMAGETYPE_PNG){
$this->quality = $quality;
}
}//function setQuality($quality)
public function getQuality(){
$quality = null;
if($this->image_properties[2] ==IMAGETYPE_JPEG || $this->image_properties[2] == IMAGETYPE_PNG){
$quality = $this->quality;
}
return $quality;
}
}
?> |
Partager