dqknycyt92288 2012-08-16 13:37
浏览 163

imagecolortransparent()在PHP中将所有黑色像素变为透明

Right, so I set out to build a function in PHP that could merge two images together whilst preserving the transparent background of the PNG files - which I successfully accomplished - using the code below,

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

This managed to merge the two images and keep the transparent background, the only problem was that any black coloured pixels in the merged images turned transparent and the result of this merge is shown here > merged image

the top image is the jellyfish image, the bottom is a QR code, which can only be seen when the image is placed on any background other than white. So what I am pretty sure is happening, is that imagecolortransparent($imageOut, $colourBlack); turns every black pixel in the newly created merged_file.png to transparent. I tested the theory by altering imageCreateTransparent($x, $y) slightly to what is shown below,

function imageCreateTransparent($x, $y) {

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

imagecolortransparent($imageOut, $colourBlack);

return $imageOut;

}

so in this function I am filling the entire image with the colour (55, 55, 55) then setting this colour to transparent in my imagecolortransparent() function. This does the trick, and my QR code is shown as it should be. The only problem is that I see this as a quick and dirty hack and if anyone has the colour (55, 55, 55) in an uploaded image it will turn transparent ? So I am curious to know what another solution would be? Thanks.

  • 写回答

1条回答 默认 最新

  • douweiluo0600 2014-08-25 02:29
    关注

    Have you tried setting the black pixels to transparent only on the top image and then doing the copy?

    The PHP manual states: "Transparency is copied only with imagecopymerge() and true color images, not with imagecopy() or pallete images."

    So it might be that this is an alternative solution to applying the transparency on the final image before copying the two others in.

    Another thing that might be worth looking at is: imagealphablending. It has been mentioned in comments on the php site that having this incorrectly set can affect layering of images that have alphas applied.

    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法