2016-11-07 89 views
0

当我在Jcrop容器中选择具有透明背景的PNG文件时,它会显示黑色背景。当我裁剪并保存它时,它将被保存为.png文件,但是在作物容器中显示为黑色背景。PHP&JS - Jcrop - 选择的PNG图像具有黑色背景

JS:

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

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'); 

任何想法我可能会错过? bgColor正如我在多个答案中看到的,因为解决方案没有任何效果,并且没有解决问题。

+1

请务必**复制和粘贴**你的代码以避免错别字。您的JS缺少一个报价,因此无效。 – Chris

+0

你的代码应该是这样的:https://github.com/tapmodo/Jcrop/issues/13 – DOZ

+0

哦,对不起,idk如何被错误删除,反正这不是问题。 @DOZ'透明'不是不幸的工作 –

回答

1

这可能是因为imagecreatetruecolor

引用例如第一用户给予贡献手册页注:

如果你想创建一个透明 PNG图像,其中背景 是完全透明,并且所有绘制操作发生在此之上,然后执行以下操作:

“Richard Davey”

当你creatre你的“主持人”的形象,以裁剪图像复制到
<?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); 
?> 

意义,你说有一个透明背景