2017-05-17 8 views
0

정사각형 이미지로 변환하지만 이미지 전체가 보이도록 가로 방향 PNG 이미지가 있습니다 (CSS 용어로 포함).imagecopyresampled png 이미지가 정사각형으로 표시됩니다.

나는 그것을 관리했고 이미지 뒤에있는 투명도가 보존되지만 이미지 위아래는 검정색으로 유지됩니다. 어떻게 투명하게 처리 할 수 ​​있습니까?

   move_uploaded_file($uploadedFile,$targetFile); 
       $image = ($fileExtension == 'png') ? imagecreatefrompng($targetFile) : imagecreatefromjpeg($targetFile); 
       unlink($targetFile); 
       $filename = $targetFile;   
       $width = imagesx($image); 
       $height = imagesy($image); 
       $thumb_width = 400; 
       $thumb_height = 400; 

       $thumb = imagecreatetruecolor($thumb_width, $thumb_height); 
       $dst_image = $thumb; 
       $src_image = $image; 
       $dst_w = $thumb_width; 
       $dst_h = $thumb_height; 
       $y = $width > $height ? ($width-$height)/2 : 0; 
       $x = $width > $height ? 0 : ($height-$width)/2; 
       $size = $width > $height ? $width : $height; 
       $src_w = $size; 
       $src_h = $size; 
       $src_x = -$x; 
       $src_y = -$y; 

       imagealphablending($thumb, false); 
       imagesavealpha($thumb, true); 
       imagealphablending($image, false); 
       imagesavealpha($image, true);   

       imagecopyresampled($dst_image,$src_image,$dst_x,$dst_y,$src_x,$src_y,$dst_w,$dst_h,$src_w,$src_h); 
       $blobName = 'icon.png'; 
       putBlobImage($storageClient,$storageContainer,$thumb,$blobName,$fileExtension); 

enter image description here


UPDATE

나는 블랙 박스를 제거하기 위해 관리 않았지만, 이제 내 이미지에서 검은 사라 (그것의 대부분을하지만). 원본 사진에서 검정색을 어떻게 보존합니까? 아래 예제 및 코드를 참조하십시오.

enter image description here

    move_uploaded_file($uploadedFile,$targetFile); 
       $image = ($fileExtension == 'png') ? imagecreatefrompng($targetFile) : imagecreatefromjpeg($targetFile); 
       unlink($targetFile); 
       $filename = $targetFile;   
       $width = imagesx($image); 
       $height = imagesy($image); 
       $thumb_width = 400; 
       $thumb_height = 400; 

       $thumb = imagecreatetruecolor($thumb_width, $thumb_height); 

       $black = imagecolorallocate($thumb, 0, 0, 0); 
       imagecolortransparent($thumb,$black); 

       $dst_image = $thumb; 
       $src_image = $image; 
       $sc = $width/$thumb_width; 
       $dst_w = $width/$sc; 
       $dst_h = $height/$sc; 
       $y = $width > $height ? (400-$dst_h)/2 : 0; 
       $x = $width > $height ? 0 : (400-$dst_w)/2; 
       $size = $width > $height ? $width : $height; 
       $src_w = $width; 
       $src_h = $height; 
       $position = array(0,0,$x,$y); 
       list($src_x,$src_y,$dst_x,$dst_y) = $position; 


       //imagealphablending($src_image, false); 
       //imagesavealpha($src_image, true); 
       //imagealphablending($dst_image, true); 
       //imagesavealpha($dst_image, true);    


       imagecopyresampled($dst_image,$src_image,$dst_x,$dst_y,$src_x,$src_y,$dst_w,$dst_h,$src_w,$src_h); 
       $blobName = 'icon.png'; 
       putBlobImage($storageClient,$storageContainer,$thumb,$blobName,$fileExtension); 
       $data = $blobName.'@@'.$uploadedFileName.'@@'.$fileExtension; 
       $return .= $data; 

답변

0

나는 마침내 그것을 해결하기 위해 관리!

move_uploaded_file($uploadedFile,$targetFile); 
$image = ($fileExtension == 'png') ? imagecreatefrompng($targetFile) : imagecreatefromjpeg($targetFile); 
unlink($targetFile); 
$filename = $targetFile;   
$width = imagesx($image); 
$height = imagesy($image); 
$thumb_width = 400; 
$thumb_height = 400; 
$thumb = imagecreatetruecolor($thumb_width, $thumb_height); 

$color = imagecolorallocatealpha($thumb, 255, 0, 0, 127); 
imagefill($thumb, 0, 0, $color); 
// THE LINE ABOVE DID THE TRICK 


imagecolortransparent($thumb,$color); 

$dst_image = $thumb; 
$src_image = $image; 
$sc = $width/$thumb_width; 
$dst_w = $width/$sc; 
$dst_h = $height/$sc; 
$y = $width > $height ? (400-$dst_h)/2 : 0; 
$x = $width > $height ? 0 : (400-$dst_w)/2; 
$size = $width > $height ? $width : $height; 
$src_w = $width; 
$src_h = $height; 
$position = array(0,0,$x,$y); 
list($src_x,$src_y,$dst_x,$dst_y) = $position; 


//imagealphablending($src_image, false); 
//imagesavealpha($src_image, true); 
//imagealphablending($dst_image, false); 
//imagesavealpha($dst_image, true); 


imagecopyresampled($dst_image,$src_image,$dst_x,$dst_y,$src_x,$src_y,$dst_w,$dst_h,$src_w,$src_h); 
$blobName = 'icon.png'; 
putBlobImage($storageClient,$storageContainer,$thumb,$blobName,$fileExtension);