originalWidth = $img_info[0]; $this->originalHeight = $img_info[1]; $this->filename = $filename; switch(exif_imagetype($filename)){ case IMAGETYPE_JPEG: $this->image = imagecreatefromjpeg($filename); break; case IMAGETYPE_PNG: $this->image = imagecreatefrompng($filename); break; case IMAGETYPE_GIF: $this->image = imagecreatefromgif($filename); break; } if($keepOriginal){ $this->originalImage = clone $this->image; } } public function resetImage(){ if($this->originalImage != null){ $this->image = clone $this->originalImage; } } public function exifRotate(){ $exif = @exif_read_data($this->filename); if(isset($exif) && !empty($exif['Orientation'])) { switch($exif['Orientation']) { case 2: imageflip($this->image, IMG_FLIP_VERTICAL); break; case 3: $this->image = imagerotate($this->image,180,0); break; case 4: imageflip($this->image, IMG_FLIP_HORIZONTAL); break; case 5: $this->image = imagerotate($this->image,-90,0); imageflip($this->image, IMG_FLIP_HORIZONTAL); break; case 6: $this->image = imagerotate($this->image,-90,0); break; case 7: $this->image = imagerotate($this->image,-90,0); imageflip($this->image, IMG_FLIP_VERTICAL); break; case 8: $this->image = imagerotate($this->image,90,0); break; } } } public function cover($targetSize){ if($this->getWidth() > $this->getHeight()){ $targetW = $targetSize / $this->getHeight() * $this->getWidth(); $targetH = $targetSize; }else{ $targetW = $targetSize; $targetH = $targetSize / $this->getWidth() * $this->getHeight(); } $imageNew = imagecreatetruecolor($targetW, $targetH); imagecopyresampled ( $imageNew , $this->image , 0 , 0 , 0 , 0 , $targetW , $targetH , $this->getWidth() , $this->getHeight() ); $this->image = $imageNew; } public function contain($targetSize){ if($this->getWidth() < $this->getHeight()){ $targetW = $targetSize / $this->getHeight() * $this->getWidth(); $targetH = $targetSize; }else{ $targetW = $targetSize; $targetH = $targetSize / $this->getWidth() * $this->getHeight(); } $imageNew = imagecreatetruecolor($targetW, $targetH); imagecopyresampled ( $imageNew , $this->image , 0 , 0 , 0 , 0 , $targetW , $targetH , $this->getWidth() , $this->getHeight() ); $this->image = $imageNew; } public function smartFaceCrop($targetSize = -1){ if($targetSize != -1){ $this->cover($targetSize); } if($this->getWidth() > $this->getHeight()){ $x = ( $this->getWidth() - $targetSize) / 2; $y = 0; }else{ $x = 0; $y = 0; } $imageNew = imagecreatetruecolor($targetSize, $targetSize); imagecopyresized ( $imageNew , $this->image , 0 , 0 , $x , $y , $targetSize ,$targetSize , $targetSize , $targetSize ); $this->image = $imageNew; } public function getWidth(){ return imagesx($this->image); } public function getHeight(){ return imagesy($this->image); } public function getOriginalWidth(){ return $this->originalWidth; } public function getOriginalHeight(){ return $this->originalHeight; } public function outputImageFile($type = IMAGETYPE_JPEG, $quality = 100, $fileName = null){ switch($type){ case IMAGETYPE_JPEG: imagejpeg($this->image, $fileName, $quality); break; case IMAGETYPE_PNG: imagepng($this->image, $fileName, 9 - (($quality / 100) * 9), PNG_ALL_FILTERS); break; case IMAGETYPE_GIF: imagegif($this->image, $fileName); break; } } public function getImageObject(){ return $this->image; } } ?>