PHP GD-透明PNG黑色背景

我正在使用PHP和GD裁剪并输出带有以下代码的图像.它工作正常,但是当我将透明PNG传递给它时,会生成黑色背景.我该如何阻止呢?

//setup
switch ($source_type) {
    case IMAGETYPE_JPEG:    $source = imagecreatefromjpeg($img_path);   break;
    case IMAGETYPE_PNG:     $source = imagecreatefrompng($img_path);    break;
}

// setup cropped destination
$cropped = imagecreatetruecolor($cropped_width, $cropped_height);

// create cropped image
$x = (($source_width / 100) * IMAGE_X) - ($cropped_width / 2);
$y = (($source_height / 100) * IMAGE_Y) - ($cropped_height / 2);
imagecopy(
    $cropped,
    $source,
    0, 0,
    $x, $y,
    $cropped_width, $cropped_height
);

// output inc header
header('Content-type: image/jpeg');
imagejpeg($cropped);

解决方法:

应该是以下内容

switch ($source_type)
{
 case IMAGETYPE_PNG:

    $background = imagecolorallocate($source, 0, 0, 0);
    // remove the black 
    imagecolortransparent($source, $background);

    // turn off alpha blending
    imagealphablending($source, false);


    imagesavealpha($source, true);

    break;
}

还有一个类似的问题here

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...