Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
imagecolortransparent()在PHP中使所有黑色像素透明_Php_Image_Image Processing_Php Gd - Fatal编程技术网

imagecolortransparent()在PHP中使所有黑色像素透明

imagecolortransparent()在PHP中使所有黑色像素透明,php,image,image-processing,php-gd,Php,Image,Image Processing,Php Gd,好的,所以我开始用PHP构建一个函数,可以将两个图像合并在一起,同时保留PNG文件的透明背景——我成功地实现了这一点——使用下面的代码 function imageCreateTransparent($x, $y) { $imageOut = imagecreatetruecolor($x, $y); $colourBlack = imagecolorallocate($imageOut, 0, 0, 0); imagecolortransparent($imageOu

好的,所以我开始用PHP构建一个函数,可以将两个图像合并在一起,同时保留PNG文件的透明背景——我成功地实现了这一点——使用下面的代码

function imageCreateTransparent($x, $y) {

    $imageOut = imagecreatetruecolor($x, $y);
    $colourBlack = imagecolorallocate($imageOut, 0, 0, 0);
    imagecolortransparent($imageOut, $colourBlack);
    return $imageOut;
}
    


function mergePreregWthQR($preRegDir, $qrDir){

$top_file = $preRegDir;
$bottom_file = $qrDir;


$top    = imagecreatefrompng($top_file);
$bottom = imagecreatefrompng($bottom_file);

// get current width/height
list($top_width, $top_height) = getimagesize($top_file);
list($bottom_width, $bottom_height) = getimagesize($bottom_file);

// compute new width/height
$new_width = ($top_width > $bottom_width) ? $top_width : $bottom_width;
$new_height = $top_height + $bottom_height;

// create new image and merge
$new = imageCreateTransparent($new_width,$new_height);
imagecopy($new, $bottom, 0, $top_height+1, 0, 0, $bottom_width, $bottom_height);
imagecopy($new, $top, 0, 0, 0, 0, $top_width, $top_height);

$filename = "merged_file.png";

// save to file
imagepng($new, $filename);

}

mergePreregWthQR("file.png", "qr.png");
这成功地合并了两个图像并保持了透明背景,唯一的问题是合并图像中的任何黑色像素都变为透明,并且此合并的结果显示在此处>

顶部图像是水母图像,底部是二维码,只有将图像放置在除白色以外的任何背景上时才能看到二维码。所以我非常确定的是,imagecolortransparent($imageOut,$ColorBlack)将新创建的合并文件.png中的每个黑色像素变为透明。我通过将imageCreateTransparent($x,$y)稍微更改为下图所示来测试该理论

function imageCreateTransparent($x, $y) {

$imageOut = imagecreatetruecolor($x, $y);
$colourBlack = imagecolorallocate($imageOut, 55, 55, 55);
imagefill ( $imageOut, 0, 0, $colourBlack );

imagecolortransparent($imageOut, $colourBlack);

return $imageOut;
}


因此,在这个函数中,我用颜色(55,55,55)填充整个图像,然后在imagecolortransparent()函数中将此颜色设置为透明。这样做的技巧,我的二维码显示它应该是。唯一的问题是,我认为这是一个快速和肮脏的黑客,如果有人在上传的图像颜色(55,55,55),它会变成透明?所以我很想知道另一个解决方案是什么?谢谢。

您是否尝试过将顶部图像上的黑色像素设置为仅透明,然后进行复制

仅使用imagecopymerge()和真彩色图像复制透明度,而不使用imagecopy()或pallete图像

因此,这可能是在复制其他两个图像之前,在最终图像上应用透明度的另一种解决方案


另一件可能值得一看的事情是:。php网站的评论中提到,如果设置不正确,可能会影响应用Alpha的图像分层。

您解决过这个问题吗?我也有同样的问题。。。