2012-09-28 3 views
0

업로드 된 이미지를받는 PHP 스크립트가 있습니다. 업로드 된 이미지는 임시 폴더에 저장되고이 스크립트는 이미지를 다시 샘플링하여 올바른 폴더에 저장합니다. 사용자는 JPG, PNG 또는 GIF 파일을 업로드 할 수 있습니다. 이 스크립트는 JPG 파일 만 제공합니다.png 및 gif 용 이미지 리샘플링 스크립트

투명성을 잃지 않고 PNG 및 GIF의 크기를 조정하려면이 스크립트를 어떻게 수정해야합니까?

$targ_w = $targ_h = 150; 
$jpeg_quality = 90; 

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

$new_src = str_replace('/temp','',$_POST['n']); 

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

imagejpeg($dst_r,$new_src,$jpeg_quality); 

답변

1

JPEG 이미지는 투명한 배경을 가질 수 없습니다.

대신 당신은 imagesavealpha()에 따라 이미지를 만들 수 있습니다 : 그것은 투명한 배경을 PNG 및 GIF (모두에서 PNG를 만들 것입니다

$targ_w = $targ_h = 150; 
$newImage = imagecreatetruecolor($targ_w, $targ_h); 
imagealphablending($newImage, false); 
imagesavealpha($newImage, true); 
$transparent = imagecolorallocatealpha($newImage, 255, 255, 255, 127); 
imagefilledrectangle($newImage, 0, 0, $targ_w, $targ_h, $transparent); 

$src = $_POST['n']; 
$img_r = imagecreatefromstring(file_get_contents($src)); 
$img_r_size = getimagesize($src); 

$width_r = $img_r_size[0]; 
$height_r = $img_r_size[1]; 
if($width_r > $height_r){ 
    $width_ratio = $targ_w/$width_r; 
    $new_width = $targ_w; 
    $new_height = $height_r * $width_ratio; 
} else { 
    $height_ratio = $targ_h/$height_r; 
    $new_width = $width_r * $height_ratio; 
    $new_height = $targ_h; 
} 

imagecopyresampled($newImage, $img_r, 0, 0, 0, 0, $new_width, $new_height, $width_r, $height_r); 

$new_src = str_replace('/temp','',$_POST['n']); 
imagepng($newImage, $new_src); 

을하고, 150 × 150으로 크기를 조정

이것은 단지이다. . 예를 들어,이 비율을 제한하지 않는

+0

나는 그들이 투명한 배경을 가지고 있지 않다는 것을 알고있다. 아마도 제 질문은 다소 모호합니다. 내가 게시 한 스크립트는 업로드 된 이미지를 입력으로 가져옵니다. 나는 그 질문을 갱신 할 것이다. – Albert

+0

내가 게시 한 내용은 투명성을 잃지 않고 이미지의 크기를 조정합니다. 시도 해봐. –

+0

파일의 원래 확장자가 있어야합니다. – Albert

0

나는 몇 달 전에이 문제를 가지고 아래의 코드를 사용하여 그것을 해결 :

관련 문제