用php删除图像背景并保存透明png

Har*_*ngh 6 php

我想删除在PHP平台上工作的网站上传的任何图像的白色背景.上传功能已完成但与此功能混淆.

这是我在这里找到的链接: 从图像中删除白色背景并使其透明

但这反过来了.我想删除彩色背景,并使其具有透明背景图像.

Mae*_*lyn 5

由于您只需要单色透明度,因此最简单的方法是使用定义白色imagecolortransparent()。这样的东西(未经测试的代码):

$img = imagecreatefromstring($your_image); //or whatever loading function you need
$white = imagecolorallocate($img, 255, 255, 255);
imagecolortransparent($img, $white);
imagepng($img, $output_file_name);
Run Code Online (Sandbox Code Playgroud)


小智 0

使用php图像处理和GD,逐像素读取图像,如果RGB分量都是255(像素为白色),则将alpha通道设置为255(透明)。您可能需要更改图像的文件类型,具体取决于上传的文件类型是否支持 Alpha 通道。