class cImageresize {
var $imagepath;
var $imagename;
var $imagesize;
var $imagedetails;
var $directory;
var $filename;
var $fileextension;
var $imagehandler;
var $mime_photo;
var $source;
var $sourcehandlerwidth;
var $sourcehandlerheight;
var $rapport_dim;
var $error;
var $isok;
function cImageresize($inImagepath = null, $inFilemane = null) {
$this->imagepath = $inImagepath;
$this->imagename = basename($inImagepath);
$this->directory = dirname($inImagepath)."/";
list($this->filename, $this->fileextension) = split('[.]', $this->imagename);
$this->imagedetails = getimagesize($this->imagepath);
switch ($this->imagedetails[2]) {
case 1 :
$this->sourcehandler = ImageCreateFromGif($this->imagepath);
$this->mime_photo='image/gif';
break;
case 2 :
$this->sourcehandler = ImageCreateFromJPEG($this->imagepath);
$this->mime_photo='image/jpeg';
break;
case 3 :
$this->sourcehandler = ImageCreateFromPNG($this->imagepath);
$this->mime_photo='image/png';
break;
default:
echo '
Erreur de format d\'image '.$this->imagepath.'. Seuls les formats jpeg, gif et png sont supportés';
return false;
}
$this->sourcehandlerwidth = imagesx($this->sourcehandler);
$this->sourcehandlerheight = imagesy($this->sourcehandler);
//calcul le rapport entre largeur et longueur...
$this->rapport_dim = $this->sourcehandlerwidth / $this->sourcehandlerheight;
}
function crop() {
//test si image en portrait ou en paysage
if ( $this->sourcehandlerwidth >= $this->sourcehandlerheight ) {
$destwidth = $this->sourcehandlerheight;
$destheight = $this->sourcehandlerheight;
$destx = ($this->sourcehandlerwidth - $this->sourcehandlerheight) / 2;
$desty = 0;
} else {
$destheight = $this->sourcehandlerwidth;
$destwidth = $this->sourcehandlerwidth;
$destx = 0;
$desty = ($this->sourcehandlerheight - $destwidth) / 2;
}
//crée l'image (taille de l'imange source)
$this->imagehandler = imagecreatetruecolor($destwidth, $destheight) or die ("Cliquez sur le lien 'Envoyer' et transmettre la mail.
Envoyer");
$white = imagecolorallocate($this->imagehandler, 255, 255, 255);
Imagefill($this->imagehandler, 0, 0, $white);
Imagecolortransparent($this->imagehandler, $white);
$this->isok = imagecopyresized($this->imagehandler, $this->sourcehandler, 0, 0, $destx, $desty, $destwidth, $destheight, $destwidth, $destheight);
}
function thumb($newsize = null) {
//test si image en portrait ou en paysage
if ( $this->sourcehandlerwidth >= $this->sourcehandlerheight ) {
$destwidth = $newsize;
$destheight = $destwidth / $this->rapport_dim;
} else {
$destheight = $newsize;
$destwidth = $destheight * $this->rapport_dim;
}
//crée l'image (taille de l'imange source)
$this->imagehandler = imagecreatetruecolor($destwidth, $destheight) or die ("Cliquez sur le lien 'Envoyer' et transmettre la mail.
Envoyer");
$white = imagecolorallocate($this->imagehandler, 255, 255, 255);
Imagefill($this->imagehandler, 0, 0, $white);
Imagecolortransparent($this->imagehandler, $white);
//ajoute par dessus l'image source miniaturisée
$this->isok = imagecopyresampled($this->imagehandler, $this->sourcehandler, 0, 0, 0, 0, $destwidth, $destheight, $this->sourcehandlerwidth, $this->sourcehandlerheight);
}
function imageclose($inNewfilepath = null, $inNewformat = null) {
if($inNewfilepath) {
$this->directory = dirname($inNewfilepath)."/";
$this->filename = basename($inNewfilepath);
}
if($inNewformat) {
$this->mime_photo = $inNewformat;
}
// Fini l'image
switch ($this->mime_photo) {
case 'image/jpeg' :
$theNewpath = $this->directory.$this->filename.".jpg";
Imagejpeg ($this->imagehandler, $theNewpath);
break;
case 'image/gif' :
$theNewpath = $this->directory.$this->filename.".gif";
ImageGif ($this->imagehandler, $theNewpath);
break;
case 'image/png' :
$theNewpath = $this->directory.$this->filename.".png";
ImagePng ($this->imagehandler, $theNewpath);
break;
default:
Imagejpeg ($this->imagehandler, $theNewpath);
break;
}
if($this->isok) return true;
else return false;
}
}
?>