简体   繁体   English

PHP&JS-Jcrop-选择的PNG图像具有黑色背景

[英]PHP & JS - Jcrop - chosen PNG image has black background

When I choose PNG file with transparent background in Jcrop container it is displayed with black background. 当我在Jcrop容器中选择透明背景的PNG文件时,它显示为黑色背景。 When I crop it and save it, it is saved as .png file, but with black background as it was displayed in crop container. 当我裁剪并保存它时,它保存为.png文件,但是在裁剪容器中显示为黑色背景。

JS: JS:

$('#image').Jcrop({
    bgColor: 'transparent',
    aspectRatio: 1,
    minSize: [180, 180],
    maxSize: [20000, 20000],
    onSelect: updateCoords,
    onChange: updateCoords,
    boxWidth: $('.modal-body', $imageUploadModal).width()
});

PHP for saving images: PHP用于保存图像:

$target_w = $target_h = 400;

$src = $request->request->get('src');
$x = $request->request->get('x');
$y = $request->request->get('y');
$w = $request->request->get('w');
$h = $request->request->get('h');

ini_set('memory_limit', -1);

$img_r = imagecreatefrompng($src);
imagealphablending($img_r, true);

$dst_r = ImageCreateTrueColor($target_w, $target_h);

imagecopyresampled($dst_r, $img_r, 0, 0, $x, $y, $target_w, $target_h, $w, $h);

$randomStringGenerator = new RandomStringGenerator();
$filename = '/profile-pictures/'.$randomStringGenerator->generate(50).'.png';

imagepng($dst_r, $filename);
imagedestroy($img_r);
imagedestroy($dst_r);

ini_restore('memory_limit');

Any ideas what I could be missing? 有什么想法我可能会错过吗? bgColor as I saw in multiple answers as solution does not have any effect and is not fixing the issue. 我在多个答案中看到的bgColor作为解决方案没有任何效果,并且bgColor问题。

It could be because of imagecreatetruecolor 可能是因为imagecreatetruecolor

Quoting example gave in first user contributed note on the manual page : 引用示例在手册页上为第一位用户提供了注释:

If you want to create a transparent PNG image, where the background is fully transparent, and all draw operations happen on-top of this, then do the following: 如果要创建透明的 PNG图像,背景是完全透明的,并且所有绘制操作都在此之上进行,则请执行以下操作:

"Richard Davey" “理查德·戴维”

<?php
    $png = imagecreatetruecolor(800, 600);
    imagesavealpha($png, true);

    $trans_colour = imagecolorallocatealpha($png, 0, 0, 0, 127);
    imagefill($png, 0, 0, $trans_colour);

    $red = imagecolorallocate($png, 255, 0, 0);
    imagefilledellipse($png, 400, 300, 400, 300, $red);

    header("Content-type: image/png");
    imagepng($png);
?>

Meaning when you creatre your "host" image to copy cropped image to, you say it has a transparent background 意思是当您创建“宿主”图像以将裁剪的图像复制到其中时,您说它具有透明背景

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM