Ir para conteúdo

POWERED BY:

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

Cesão

[Resolvido] Crop Imagem em PHP

Recommended Posts

Olá, amigos.

Não se confundam... este é um tópico PHP. hehe

 

Seguinte. Usei um plug-in de recortar imagem em jQuery, mas no final da ação, existe uma página que mostra a imagem recortada, mas essa página, é em PHP. Como programo em ASP, não manjo nada de PHP, mas pude perceber que, na realidade, a página em PHP não recorta REALMENTE a imagem. Ele apenas pega pontos da imagem enviada por formulário, como um map, e mostra na página redimensionando e abaixando a qualidade.

 

Gostaria apenas de saber, como fazer para SALVAR em uma pasta esse resultado. Pegar realmente a imagem mostrada e salvá-la exatamente como ela está, em um diretório.

 

Vejam como é a página que mostra a thumb (imagem recortada):

 

<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
	$targ_w = $targ_h = 80;
	$jpeg_quality = 90;

	$src = $_POST['foto'];
	$img_r = imagecreatefromjpeg($src);
	$dst_r = ImageCreateTrueColor( $targ_w, $targ_h );

	imagecopyresampled($dst_r,$img_r,0,0,$_POST['x'],$_POST['y'],
	$targ_w,$targ_h,$_POST['w'],$_POST['h']);

	header('Content-type: image/jpeg');
	imagejpeg($dst_r,null,$jpeg_quality);

	exit;
}

?>

Gostaria de saber o que preciso fazer para, ao invés de apenas mostrar a imagem, ele realmente salvá-la em um diretório.

Compartilhar este post


Link para o post
Compartilhar em outros sites

TÁ AE, TEM 2 CÓDIGOS DIFERENTES... você ESCOLHE QUAL QUER..

 

 

Códigos:

 

<?php

if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
        $targ_w = 80;
        $targ_h = 80;
        
        $src = $_POST['foto'];

        $diretorio = "fotos/";
        $foto_nome = "foto_".date('dmy').time()."";
        $nome_foto  = "$foto_nome.jpg";

        reduz_imagem_jpg($src, $targ_w , $targ_h , $diretorio.$nome_foto, $_POST['x'], $_POST['y'], $_POST['w'], $_POST['h']);

}
?>

Função:

    function reduz_imagem_jpg($src, $targ_w, $targ_h, $diretorio, $x, $y, $w, $h) {
 
        $image_p = imagecreatetruecolor($targ_w, $targ_h);
        $image   = imagecreatefromjpeg($src);

        imagecopyresampled($dst_r,$img_r,0,0,$x,$y,$targ_w,$targ_h,$_POST['w'],$_POST['h']);
        imagecopyresampled($image_p, $image,0,0,$x,$y,$targ_w,$targ_h,$_POST['w'],$_POST['h']);

        return imagejpeg($image_p, $diretorio, 90);
    }

 

 

 

Se não der certo.. veja se consegue por esse simples..

 

<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
        $targ_w = $targ_h = 80;
        $jpeg_quality = 90;

        $src = $_POST['foto'];
        $img_r = imagecreatefromjpeg($src);
        $dst_r = ImageCreateTrueColor( $targ_w, $targ_h );

        $ft_nome = "fotos/imagem_".date('dmy').time()."";
        $diretorio = "$ft_nome.jpg";        
 
        imagecopyresampled($dst_r,$img_r,0,0,$_POST['x'],$_POST['y'],$targ_w,$targ_h,$_POST['w'],$_POST['h']);

        header('Content-type: image/jpeg');
        imagejpeg($dst_r,$diretorio,$jpeg_quality);

        exit;
}

?>

 

 

Se der problema, avisa ae, pq eu acho q fiz errado..

Compartilhar este post


Link para o post
Compartilhar em outros sites

Good, valeu pelas ideias. Ficou assim:

<?php

if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
	$targ_w = $targ_h = 80;
	$jpeg_quality = 90;

	$src = $_POST['foto'];
	$img_r = imagecreatefromjpeg($src);
	$dst_r = ImageCreateTrueColor( $targ_w, $targ_h );
	
	$array_endereco = explode("/",$src);
	$ft_nome = "../images/downloads/thumbs/$array_endereco[3]";
	$diretorio = "$ft_nome";

	imagecopyresampled($dst_r,$img_r,0,0,$_POST['x'],$_POST['y'],$targ_w,$targ_h,$_POST['w'],$_POST['h']);
	
	header('Content-type: image/jpeg');
	imagejpeg($dst_r,$diretorio,$jpeg_quality);
	
	header("Location: downloads.asp");
	
	exit;
}

?>

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.