novoTamanho(100, 100,'cortar'); // aqui, novo nome para salvar e a qualidade do jpg (ideal: 90) $resize->salvar('novonome.jpg', "100"); // Opcionalmente, voce podera fazer o download da imagem desta forma: $resize->salvar('novonome.jpg', "100",1); */ class AriFoto { private $ext; private $image; private $newImage; private $origWidth; private $origHeight; private $resizeWidth; private $resizeHeight; /** * Class constructor requires to send through the image filename * * @param string $filename - Filename of the image you want to resize */ public function __construct( $filename ) { if(file_exists($filename)) { $this->setImage( $filename ); } else { throw new Exception('Image ' . $filename . ' can not be found, try another image.'); } } /** * Set the image variable by using image create * * @param string $filename - The image filename */ private function setImage( $filename ) { $size = getimagesize($filename); $this->ext = $size['mime']; switch($this->ext) { // Image is a JPG case 'image/jpg': case 'image/jpeg': // create a jpeg extension $this->image = imagecreatefromjpeg($filename); break; // Image is a GIF case 'image/gif': $this->image = @imagecreatefromgif($filename); break; // Image is a PNG case 'image/png': $this->image = @imagecreatefrompng($filename); break; // Mime type not found default: throw new Exception("File is not an image, please use another file type.", 1); } $this->origWidth = imagesx($this->image); $this->origHeight = imagesy($this->image); } /** * Save the image as the image type the original image was * * @param String[type] $savePath - The path to store the new image * @param string $imageQuality - The qulaity level of image to create * * @return Saves the image */ public function salvar($savePath, $imageQuality="90", $download = false) { switch($this->ext) { case 'image/jpg': case 'image/jpeg': // Check PHP supports this file type if (imagetypes() & IMG_JPG) { imagejpeg($this->newImage, $savePath, $imageQuality); } break; case 'image/gif': // Check PHP supports this file type if (imagetypes() & IMG_GIF) { imagegif($this->newImage, $savePath); } break; case 'image/png': $invertScaleQuality = 9 - round(($imageQuality/100) * 9); // Check PHP supports this file type if (imagetypes() & IMG_PNG) { imagepng($this->newImage, $savePath, $invertScaleQuality); } break; } if($download) { header('Content-Description: File Transfer'); header("Content-type: application/octet-stream"); header("Content-disposition: attachment; filename= ".$savePath.""); readfile($savePath); } imagedestroy($this->newImage); } /** * Resize the image to these set dimensions * * @param int $width - Max width of the image * @param int $height - Max height of the image * @param string $resizeOption - Scale option for the image * * @return Save new image */ public function novoTamanho( $width, $height, $resizeOption = 'default' ) { switch(strtolower($resizeOption)) { case 'exato': $this->resizeWidth = $width; $this->resizeHeight = $height; break; case 'cortar': $cropit = 1; $this->resizeWidth = $width; $this->resizeHeight = $height; break; case 'largura': $this->resizeWidth = $width; $this->resizeHeight = $this->resizeHeightByWidth($width); break; case 'altura': $this->resizeWidth = $this->resizeWidthByHeight($height); $this->resizeHeight = $height; break; default: if($this->origWidth > $width || $this->origHeight > $height) { if ( $this->origWidth > $this->origHeight ) { $this->resizeHeight = $this->resizeHeightByWidth($width); $this->resizeWidth = $width; } else if( $this->origWidth < $this->origHeight ) { $this->resizeWidth = $this->resizeWidthByHeight($height); $this->resizeHeight = $height; } } else { $this->resizeWidth = $width; $this->resizeHeight = $height; } break; } if(@$cropit==1){ $width1 = $this->origWidth; $height1 = $this->origHeight; //list($width1,$height1)=getimagesize($this->image); // check if ratios match $_ratio=array($width1/$height1,$width/$height); if ($_ratio[0] != $_ratio[1]) { // crop image // find the right scale to use $_scale=min((float)($width1/$width),(float)($height1/$height)); // coords to crop $cropX=(float)($width1-($_scale*$width)); $cropY=(float)($height1-($_scale*$height)); // cropped image size $cropW=(float)($width1-$cropX); $cropH=(float)($height1-$cropY); $crop=ImageCreateTrueColor($cropW,$cropH); // crop the middle part of the image to fit proportions ImageCopy( $crop, $this->image, 0, 0, (int)($cropX/2), (int)($cropY/2), $cropW, $cropH ); } // do the thumbnail $this->newImage =ImageCreateTrueColor($width,$height); if (isset($crop)) { // been cropped ImageCopyResampled( $this->newImage, $crop, 0, 0, 0, 0, $width, $height, $cropW, $cropH ); ImageDestroy($crop); } } else { $this->newImage = imagecreatetruecolor($this->resizeWidth, $this->resizeHeight); imagecopyresampled($this->newImage, $this->image, 0, 0, 0, 0, $this->resizeWidth, $this->resizeHeight, $this->origWidth, $this->origHeight); } } /** * Get the resized height from the width keeping the aspect ratio * * @param int $width - Max image width * * @return Height keeping aspect ratio */ private function resizeHeightByWidth($width) { return floor(($this->origHeight/$this->origWidth)*$width); } /** * Get the resized width from the height keeping the aspect ratio * * @param int $height - Max image height * * @return Width keeping aspect ratio */ private function resizeWidthByHeight($height) { return floor(($this->origWidth/$this->origHeight)*$height); } } ?>