[英]imagecopyresampled png contain image in square
I have a landscape PNG image which I want to convert to a square image but with the whole image visable (in css-terms: contain). 我有一个风景PNG图像,我想将其转换为正方形图像,但整个图像可见(在CSS术语中:contain)。
I have managed that, and the transparency behind the image is preserved, but above and below the image it is black. 我已经做到了,并且保留了图像后面的透明度,但是在图像的上方和下方是黑色。 How do I manage to get that transparent too?
我也要如何做到透明?
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);
UPDATE UPDATE
I did manage to remove the black boxes, but now also the black from my image disappeared (most of it though). 我确实设法删除了黑匣子,但是现在我图像中的黑色也消失了(尽管大部分)。 How do I preserve the black from the original picture?
如何保留原始图片中的黑色? See example and code below.
请参见下面的示例和代码。
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;
I finally managed to fix it! 我终于设法解决了!
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.