Jump to content


Photo

Upload De Imagens [resolvido]


  • Faça o login para participar
2 replies to this topic

#1 Forgotten Canha

Forgotten Canha

    Novato no fórum

  • Usuários
  • 16 posts
  • Sexo:Não informado

Posted 29/01/2007, 16:06

Alguém pode me dar uma mãozinha aqui?

O antigo programador da empresa onde trabalho começou a fazer um sistema de upload de imagens, mas nunca terminou.
Eu decidi "adotar" então o script dele, mas as fotos não são upadas ao servidor.
Esse script deveria redimensionar a foto e criar uma thumb e depois salvar os dois numa pasta.

Alguém consegue ver onde o erro está?

Agradeço desde já!

//Busca o nome do arquivo do formulário
$vet_foto = $_FILES["foto"];

		//Nome do arquivo de upload temporário
		$arq_foto = $vet_foto["tmp_name"];

		//Pega informações do arquivo
		$arq_info = getimagesize($arq_foto);

		//Pega o tamanho da foto
		$arq_w = $arq_info[0];
		$arq_h = $arq_info[1];
		
		//Define o tamanho que a foto deve ter
		$arq_new_w = 370;
		$arq_new_h = 260;
		$thumb_w = 120;
		$thumb_h = 80;
						
		//Checa se a foto escolhida tem o tamanho mínimo para upload
		if($arq_w >= $arq_new_w || $arq_h >= $arq_new_h)
		{
			//Pega o tipo mime do arquivo enviado
			$arq_mime = $arq_info["mime"];
	
			if($arq_mime == "image/jpeg")
			{
					//Gera o nome do arquivo a ser salvo com base no id (pasta chmod 0777)
					$arq_foto_new = "upado/$id.jpg";
					$arq_thumb_new = "upado/$id.thumb.jpg";

					
					//Define o tamanho para redimensionamento proporcional da foto
					$currwidth  = $arq_w;
					$currheight = $arq_h;
					$maxwidth   = $arq_new_w;
					$maxheight  = $arq_new_h;
	
					if($currwidth > $currheight && $currwidth > $maxwidth)
					{
							$percent = ($maxwidth * 100) / $currwidth;
							$nwidth  = $maxwidth;
							$nheight = ($percent * $currheight) / 100;
					} else if($currwidth < $currheight && $currheight > $maxheight)
					{
							$percent = ($maxheight * 100) / $currheight;
							$nheight = $maxheight;
							$nwidth  = ($percent * $currwidth) / 100;
					} else {
							$nwidth  = $maxwidth;
							$nheight = $maxheight;
					}
	
					$nwidth  = intval($nwidth);
					$nheight = intval($nheight);
	
					//Imagem original
					$src_img = imagecreatefromjpeg($arq_foto);
	
					//Imagem redimensionada proporcionalmente
					$dst_img_res = imagecreatetruecolor($nwidth, $nheight);
					imagecopyresampled($dst_img_res, $src_img, 0, 0, 0, 0, $nwidth, $nheight, $arq_w, $arq_h);

					//Criação do thumb
					$thumb_new = imagecreatetruecolor($thumb_w, $thumb_h);
					imagecopyresampled($thumb_new, $dst_img_res, 0, 0, 0, 0, $thumb_w, $thumb_h, $nwidth, $nheight);
	
					//Imagem cortada para o tamanho correto após redimensionamento
					$dst_img = imagecreatetruecolor($maxwidth, $maxheight);
					imagecopy($dst_img, $dst_img_res, 0, 0, 0, 0, $maxwidth, $maxheight);
	
					//Cria e salva a imagem em um arquivo com qualidade 70
					$arq_foto_new_ok = imagejpeg($dst_img, "$arq_foto_new", 70);
					$arq_thumb_new = imagejpeg($thumb_new, "$arq_thumb_new", 40);
			}
		}

Edição feita por: Forgotten Canha, 30/01/2007, 16:08.


#2 hostdesigner

hostdesigner

    Super Veterano

  • Usuários
  • 2910 posts
  • Sexo:Masculino
  • Localidade:Quirinópolis-GO
  • Interesses:Programação, Mulheres, Diversão, Mulheres, Música, Mulheres, Meu Carro, Mais mulheres, Internet, Outras Mulheres, Quase por último PAZ e por ultimo Outras Váááárias Mulheres...

Posted 29/01/2007, 16:20

Se você disser qual erro ajuda muito...

Acho que ninguém vai copiar o script e testar pra você...

Falopa!

#3 Forgotten Canha

Forgotten Canha

    Novato no fórum

  • Usuários
  • 16 posts
  • Sexo:Não informado

Posted 29/01/2007, 16:22

Opa, foi mal.

Na verdade, não aparece erro nenhum. Ele apenas conclui o script mas a foto não aparece no diretório.

*editado*

Troquei o script por outro que achei:

$size_h = 80;
	$size_w = 120;

	$filedir = '../../../home/upado/'; //diretorio da foto
	$thumbdir = '../../../home/upado/'; //diretorio do thumbnail
	$prefix = 'tbn_'; //prefixo da thumbnail

	$maxfile = '2000000';
	$mode = '0666';
	
	$userfile_name = $_FILES['image']['name'];
	$userfile_tmp = $_FILES['image']['tmp_name'];
	$userfile_size = $_FILES['image']['size'];
	$userfile_type = $_FILES['image']['type'];
	
	
	if (isset($_FILES['image']['name'])) 
	{
		$prod_img = $filedir.$userfile_name;

		$prod_img_thumb = $thumbdir.$prefix.$userfile_name;
		move_uploaded_file($userfile_tmp, $prod_img);
		chmod ($prod_img, octdec($mode));
		
		$sizes = getimagesize($prod_img);

		$aspect_ratio = $sizes[1]/$sizes[0]; 

		if ($sizes[1] <= $size)
		{
			$new_width = $sizes[0];
			$new_height = $sizes[1];
		}else{
			$new_height = $size_h;
			$new_width = $size_w;
//para redimensionar de acordo com a altura, comente a linha acima e descomente a abaixo
			//$new_width = abs($new_height/$aspect_ratio); 
		}

		$destimg=ImageCreateTrueColor($new_width,$new_height)
			or die('Erro ao criar imagem');
		$srcimg=ImageCreateFromJPEG($prod_img)
			or die('Erro ao abrir arquivo fonte');
		if(function_exists('imagecopyresampled'))
		{
			imagecopyresampled($destimg,$srcimg,0,0,0,0,$new_width,$new_height,ImageSX($srcimg),ImageSY($srcimg))
or die('Erro ao redimensionar: x001');
}else{
			Imagecopyresized($destimg,$srcimg,0,0,0,0,$new_width,$new_height,ImageSX($srcimg),ImageSY($srcimg))
or die('Erro ao redimensionar: x002');
}
ImageJPEG($destimg,$prod_img_thumb,90)
or die('Erro ao salvar');
imagedestroy($destimg);
	
	}

Onde:
$prod_img; //têm o endereço completo da imagem - no caso /../../../home/upado/imagem.jpg
$prod_img_thumb //mesma coisa mas pro thumbnail
$userfile_name //nome da imagem - imagem.jpg

Parece funcionar sem problemas.

Edição feita por: Forgotten Canha, 30/01/2007, 16:08.





1 user(s) are reading this topic

0 membro(s), 1 visitante(s) e 0 membros anônimo(s)

IPB Skin By Virteq