我有一个横向PNG图像,我想将其转换为方形图像,但整个图像可见(用css-terms:contains)。
我已经设法了,图像背后的透明度得以保留,但图像上方和下方都是黑色。我如何设法使其透明化?
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);
更新
我确实设法移除了黑匣子,但现在我的图像中的黑色也消失了(尽管大部分都消失了)。如何保留原始图片中的黑色?请参阅下面的示例和代码。
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 :(得分: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);