簡體   English   中英

PHP&JS-Jcrop-選擇的PNG圖像具有黑色背景

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

當我在Jcrop容器中選擇透明背景的PNG文件時,它顯示為黑色背景。 當我裁剪並保存它時,它保存為.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作為解決方案沒有任何效果,並且bgColor問題。

可能是因為imagecreatetruecolor

引用示例在手冊頁上為第一位用戶提供了注釋:

如果要創建透明的 PNG圖像,背景是完全透明的,並且所有繪制操作都在此之上進行,則請執行以下操作:

“理查德·戴維”

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

意思是當您創建“宿主”圖像以將裁剪的圖像復制到其中時,您說它具有透明背景

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM