2017-05-17 10 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); 
関連する問題