srcImage = $fileName; $infos = getimagesize($fileName); $this->srcWidth = $infos[0]; $this->srcHeight = $infos[1]; $this->srcType = $infos[2]; } /** * Setters **/ function setNewImage($newName) { $this->newImage = $newName; } function setNewWidth($newWidth) { $this->newWidth = $newWidth; } function setNewHeight($newHeight) { $this->newHeight = $newHeight; } /** * Resize an image without using any kind of specific resizing (crop, ration, etc ...) * * @param string $name * @param integer $width * @param integer $height * @param string $type * @param string $from * @return bool */ function hardResize($name, $width, $height, $type = "jpg", $from = "") { $this->setNewImage($name); $this->setNewWidth($width); $this->setNewHeight($height); if(empty($from)) { if(empty($this->srcImage)) return false; $from = $this->srcImage; } $this->getImageRessource($from); $thumb = imagecreatetruecolor($this->newWidth, $this->newHeight); if(!imagecopyresampled($thumb, $this->rscImg, 0, 0, 0, 0, $this->newWidth, $this->newHeight, $this->srcWidth, $this->srcHeight)) throw new Exception("Impossible de redimensionner l'image"); switch($type) { case 'jpg': case 'jpeg': return imagejpeg($thumb, $name, 100); break; case 'gif': return imagegif($thumb, $name); break; case 'png': return imagepng($thumb, $name); break; default: throw new Exception("Impossible de créer la nouvelle image le type $type ne convient pas"); } } /** * Prepare an image ressource * * @param string $from **/ function getImageRessource($from) { $infos = getimagesize($from); switch($infos[2]) { case IMG_GIF: $this->rscImg = imagecreatefromgif($from); break; case IMG_JPG: case IMG_JPEG: $this->rscImg = imagecreatefromjpeg($from); break; case IMG_PNG: $this->rscImg = imagecreatefrompng($from); break; default: throw new Exception("Image type not supported"); } } /** * Return the biggest ratio for width or height **/ private function getRatio($width, $height){ return max($this->srcWidth / $width, $this->srcHeight / $height); } /** * Resize an image keeping the ratio * * @param string $name * @param integer $width * @param integer $height * @param string $type * @param string $from * @return bool */ public function niceResize($name, $width, $height, $type = "jpg", $from = "") { $ratio = $this->getRatio($width, $height); $new_width = $this->srcWidth / $ratio; $new_height = $this->srcHeight / $ratio; return $this->hardResize($name, $new_width, $new_height, $type, $from); } /** * Resize an image using the ratio old width / new width * * @param string $name * @param integer $width * @param string $type * @param string $from * @return bool */ public function xResize($name, $width, $type = "jpg", $from = "") { $ratio = $this->srcWidth / $width; $new_width = $this->srcWidth / $ratio; $new_height = $this->srcHeight / $ratio; return $this->hardResize($name, $new_width, $new_height, $type, $from); } /** * Resize an image using the ratio old height / new height * * @param string $name * @param integer $height * @param string $type * @param string $from * @return bool */ public function yResize($name, $height, $type = "jpg", $from = "") { $ratio = $this->srcHeight / $height; $new_width = $this->srcWidth / $ratio; $new_height = $this->srcHeight / $ratio; return $this->hardResize($name, $new_width, $new_height, $type, $from); } /** * Function that take a part of an image * * @param string $name * @param integer $coordX * @param integer $coordY * @param integer $width * @param integer $height * @param string $type * @param string $from **/ public function crop($name, $coordX, $coordY, $width, $height, $type = "jpg", $from = "") { //On vérifie les coordonnées if($coordX + $width > $this->srcWidth) throw new Exception("Impossible de croper cette portion, X + longueur > largeur de l'image"); elseif($coordY + $height > $this->srcHeight) throw new Exception("Impossible de croper cette portion, Y + hauteur > hauteur de l'image"); $this->setNewImage($name); $this->setNewWidth($width); $this->setNewHeight($height); if(empty($from)) { if(empty($this->srcImage)) return false; $from = $this->srcImage; } $this->getImageRessource($from); $thumb = imagecreatetruecolor($this->newWidth, $this->newHeight); if(!imagecopyresampled($thumb, $this->rscImg, 0, 0, $coordX, $coordY, $this->newWidth, $this->newHeight, $this->newWidth, $this->newWidth)) throw new Exception("Impossible de redimensionner l'image"); switch($type) { case 'jpg': case 'jpeg': return imagejpeg($thumb, $name, 100); break; case 'gif': return imagegif($thumb, $name); break; case 'png': return imagepng($thumb, $name); break; default: throw new Exception("Impossible de créer la nouvelle image le type $type ne convient pas"); } } } ?>