Bottoni 0 Denunciar post Postado Setembro 9, 2011 Bom dia a todos. Estou realizando o crop em imagens JPEG mas elas estão ficando muito ruins. A qualidade da imagem está péssima!! Alguém já passou por isso aqui? Estou realizando o crop da seguinte forma: // Carrego uma imagem $image = new Image("minhaIMG.jpg"); // Realizo o crop $image->cropImage($x, $y, $w, $h); // Redimensiono a imagem $image->resize(300, 300, false); // E finalmente salvo $image->save("minhaIMG_Crop.jpg"); Todos estes métodos são da seguinte classe: class Image { private $nome; private $x; private $y; private $mime; private $resource; private $arquivoTemporario; public function __construct($campo, $arquivo = false) { if($arquivo==false){ //Guarda o nome do arquivo $this->nome=$_FILES[$campo]['name']; $op = new operacoes(); $op->trataNomeFotos($this->nome); $this->arquivoTemporario=$_FILES[$campo]['tmp_name']; /* confere se existe o arquivo temporário */ if(!is_file($this->arquivoTemporario)) die('Erro 1: Não foi possivel obter o arquivo temporário, confira se o campo existe ou se ele não se encontra vazio'); /* absorvendo mime */ $this->mime = $_FILES[$campo]['type']; /* Constrói recurso a partir do mime Utiliza a função image_type_to_mime_type para manter compatibilidade */ switch($this->mime) { case image_type_to_mime_type(IMAGETYPE_GIF): $this->resource = @imagecreatefromgif($this->arquivoTemporario) or die('Erro 2: Não foi possível criar a imagem.'); break; case image_type_to_mime_type(IMAGETYPE_JPEG) : $this->resource = @imagecreatefromjpeg($this->arquivoTemporario) or die('Erro 2: Não foi possível criar a imagem.'); break; case image_type_to_mime_type(IMAGETYPE_PNG): $this->resource = @imagecreatefrompng($this->arquivoTemporario) or die('Erro 2: Não foi possível criar a imagem.'); break; case image_type_to_mime_type(IMAGETYPE_WBMP): $this->resource = @imagecreatefromwbmp($this->arquivoTemporario) or die('Erro 2: Não foi possível criar a imagem.'); break; case 'image/pjpeg': $this->resource = @imagecreatefromjpeg($this->arquivoTemporario) or die('Erro 2: Não foi possível criar a imagem.'); break; default: $this->resource = @imagecreatefromjpeg($this->arquivoTemporario) or die('Erro 2: Não foi possível criar a imagem.'); break; } } //caso a foto venha dum arquivo já existente else { $ext = substr($campo,-3,3); $temp = explode('/',$campo); $nomeArquivo = $temp[count($temp)-1]; $this->nome = $nomeArquivo; $nome = $campo; $nome = explode(".".$ext, $nome); $nome = $nome[0]; switch($ext){ case 'gif': $this->resource = @imagecreatefromgif($campo) or die('Erro 2: Não foi possível criar a imagem.'); break; case 'jpg' : case 'peg' : $this->resource = @imagecreatefromjpeg($campo) or die('Erro 2: Não foi possível criar a imagem.'); break; case 'png': $this->resource = @imagecreatefrompng($campo) or die('Erro 2: Não foi possível criar a imagem.'); break; case 'bmp': $this->resource = @imagecreatefromwbmp($campo) or die('Erro 2: Não foi possível criar a imagem.'); break; default: $this->resource = @imagecreatefromjpeg($campo) or die('Erro 2: Não foi possível criar a imagem.'); break; } } /* Pegando valores X e Y */ $this->x = imagesx($this->resource); $this->y = imagesy($this->resource); } public function getNome(){ return $this->nome; } public function cropImage($x, $y, $w, $h) { $rsc2 = imagecreatetruecolor($w, $h); imagecopyresampled($rsc2,$this->resource,0,0,$x,$y, $w,$h,$w,$h); $this->resource = $rsc2; $this->x = $w; $this->y = $h; } /** * Redimensionamento * @internal Cria um novo recurso resampleado e substitui o antigo. * @param Integer $x, Integer $y Novas dimensões. Para redimensionar em somente um eixo deixar o outro com o valor 0 * @example $img->resize(800,600) , $img->resize(0,200); */ public function resize($newX=0,$newY=0, $corrigirProporcao=true){ //Pega tamanho da imagem $width = imagesx($this->resource); $height = imagesy($this->resource); //caso: O programador setou os dois valores if($newX>0&&$newY>0){ if($corrigirProporcao) { //Cria o novas dimensões com base na largura (sem esticar a foto) if($width > $newX || $height > $newX) { $fator_1 = $newX/$width; $fator_2 = $newX/$height; } else { $fator_2 = 1; $fator_1 = 1; } if($fator_1<$fator_2) $coef = $fator_1; else $coef = $fator_2; $img_width = $width*$coef; $img_height = $height*$coef; } else { $img_width = $newX; $img_height = $newY; } } //Outro caso: o programador setou somente a largura else if($newX>0){ //Cria o novas dimensões com base na largura (sem esticar a foto) $img_width = $newX; $img_height = $height*($newX/$width); } //Ultimo caso: O programador setou somente a altura else { //Cria o novas dimensões com base na altura(sem esticar a foto) $img_width = $width*($newY/$height); $img_height = $newY; } $newImg = imagecreatetruecolor($img_width,$img_height); //imagecopyresized($newImg,$this->resource,0,0,0,0,$img_width,$img_height,$this->x,$this->y); imagecopyresampled($newImg,$this->resource,0,0,0,0,$img_width,$img_height,$this->x,$this->y); $this->resource = $newImg; $this->x = $img_width; $this->y = $img_height; } /** * Função de salvamento * @param String $caminho recebe algo como ../../../imagens_internas/teste.jpg para salvar com o mesmo nome anterior utilize o método getNome() associado ao caminho inicial * @internal Salva o recurso em um arquivo de imagem. * @return Boolean */ public function save($caminho) { /* Nomes randômicos quando a imagem já existe neste caminho */ while(file_exists($caminho)){ $temp = explode('/',$caminho); $nomeArquivo = $temp[count($temp)-1]; $dir = str_replace($nomeArquivo,'',$caminho) ; $this->nome=(string)rand(000,999).$nomeArquivo; $caminho = $dir.$this->nome; } /* Booleano que confere a criação do arquivo */ $b = true; /* Constroe arquivo a partir do recurso de acordo com o mime */ switch($this->mime) { case image_type_to_mime_type(IMAGETYPE_GIF): $b = imagegif($this->resource,$caminho); break; case image_type_to_mime_type(IMAGETYPE_JPEG): $b = imagejpeg($this->resource,$caminho); break; case image_type_to_mime_type(IMAGETYPE_PNG): $b = imagepng($this->resource,$caminho); break; case 'image/pjpeg': $b = imagejpeg($this->resource,$caminho); break; default: //BMP e outros formatos serão convertidos em JPG $b = imagejpeg($this->resource,$caminho); } return $b; } public function __destruct() { @imagedestroy($this->resource); @imagedestroy($newImg); } } Muito obrigado! Abraço, Bottoni Compartilhar este post Link para o post Compartilhar em outros sites
H. Romeu 0 Denunciar post Postado Setembro 9, 2011 Tente outras classes, usu essa dai ,e nunca tive poblemas... Canvas >>> link Compartilhar este post Link para o post Compartilhar em outros sites