Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/260.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/2/sharepoint/4.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
Php 检查图像是否为白色;如果是这样,就要透明_Php_Transparency_Gd - Fatal编程技术网

Php 检查图像是否为白色;如果是这样,就要透明

Php 检查图像是否为白色;如果是这样,就要透明,php,transparency,gd,Php,Transparency,Gd,我有一个图像,我想让它透明,如果它是完全白色的。我已经为GD提供了以下代码,用于获取图像的一部分: $srcp = imagecreatefrompng("../images/".$_GET['b'].".png"); $destp = imagecreate(150, 150); imagecopyresampled($destp, $srcp, 0, 0, 40, 8, 150, 150, 8, 8); header('Content-type: image/png'); imagepng(

我有一个图像,我想让它透明,如果它是完全白色的。我已经为GD提供了以下代码,用于获取图像的一部分:

$srcp = imagecreatefrompng("../images/".$_GET['b'].".png");
$destp = imagecreate(150, 150);
imagecopyresampled($destp, $srcp, 0, 0, 40, 8, 150, 150, 8, 8);
header('Content-type: image/png');
imagepng($destp);

但是如何让它首先检查图像是否完全是白色的,如果是,将其更改为透明,并将其应用于
$destp

一个简单的解决方案是使用并迭代png的所有像素,如果所有像素都是白色,则将其更改为透明


希望这能有所帮助。

在快速浏览GD手册页面后,我认为以下内容应该适合您(不管怎样,它在我的测试服务器上也适用):

编辑:

根据重新阅读问题和下面的讨论,我相信这就是您想要的:

$width = 150;
$height = 150;

$srcp = imagecreatefrompng("../images/".$_GET['b'].".png");
$destp = imagecreatetruecolor(150, 150);

$white = 0;

for ($y = 0; $y < $height; $y++)
{
    for ($x = 0; $x < $width; $x++)
    {
        $currentColor = imagecolorat($srcp, $x, $y);
        $colorParts = imagecolorsforindex($srcp, $currentColor);

        if (($colorParts['red'] == 255 &&
            $colorParts['green'] == 255 &&
            $colorParts['blue'] == 255))
        {
            $white++;
        }
    }
}

if ($white == ($width * $height))
{
    imagecopyresampled($destp, $srcp, 0, 0, 40, 8, 150, 150, 8, 8);
}
else
{
    imagesavealpha($destp, true);
    imagefill($destp, 0, 0, imagecolorallocatealpha($destp, 0, 0, 0, 127));
}

header('Content-type: image/png');
imagepng($destp);
$width=150;
$height=150;
$srcp=imagecreatefrompng(“../images/”$\u GET['b']..png”);
$destp=imagecreatetruecolor(150150);
$white=0;
对于($y=0;$y<$height;$y++)
{
对于($x=0;$x<$width;$x++)
{
$currentColor=imagecolorat($srcp,$x,$y);
$colorParts=imagecolorsforindex($srcp,$currentColor);
如果($colorParts['red']==255&&
$colorParts['green']==255&&
$colorParts['blue']==255))
{
$white++;
}
}
}
如果($white==($width*$height))
{
imagecopyresampled($destp,$srcp,0,0,40,8150150,8,8);
}
其他的
{
imagesavealpha($destp,true);
imagefill($destp,0,0,imagecolorallocatealpha($destp,0,0,0127));
}
标题('Content-type:image/png');
imagepng($destp);
如果原始图像的8x8切片全部为白色,则会生成空白图像,否则会将8x8切片重新采样为150x150


原件:

我以前从未使用过PHP GD,我认为这将是一个有趣的挑战。以下是我最终如何做到这一点的:

$filename = 'test.png'; // input image
$image = imagecreatefrompng($filename);

// grab the input image's size
$size = getimagesize($filename);
$width = $size[0];
$height = $size[1];

$newImage = imagecreatetruecolor($width, $height);

// make sure that the image will retain alpha when saved
imagesavealpha($newImage, true);
// fill with transparent pixels first or else you'll
// get black instead of transparent
imagefill($newImage, 0, 0, imagecolorallocatealpha($newImage, 0, 0, 0, 127));

// loop through all the pixels
for ($y = 0; $y < $height; $y++)
{
    for ($x = 0; $x < $width; $x++)
    {
        // get the current pixel's colour
        $currentColor = imagecolorat($image, $x, $y);

        // then break it into easily parsed bits
        $colorParts = imagecolorsforindex($image, $currentColor);

        // if it's NOT white
        if (!($colorParts['red'] == 255 &&
            $colorParts['green'] == 255 &&
            $colorParts['blue'] == 255))
        {
            // then keep the same colour
            imagesetpixel($newImage, $x, $y, $currentColor);
        }
    }
}

// final output, the second arg is the filename
imagepng($newImage, 'newImage.png');
$filename='test.png';//输入图像
$image=imagecreatefrompng($filename);
//获取输入图像的大小
$size=getimagesize($filename);
$width=$size[0];
$height=$size[1];
$newImage=ImageCreateTureColor($width,$height);
//确保保存时图像将保留alpha
imagesavealpha($newImage,true);
//首先用透明像素填充,否则将
//变为黑色而不是透明
imagefill($newImage,0,0,imagecolorallocatealpha($newImage,0,0,127));
//循环遍历所有像素
对于($y=0;$y<$height;$y++)
{
对于($x=0;$x<$width;$x++)
{
//获取当前像素的颜色
$currentColor=imagecolorat($image,$x,$y);
//然后将其分解为易于解析的位
$colorParts=imagecolorsforindex($image,$currentColor);
//如果不是白色的
如果(!($colorParts['red']==255&&
$colorParts['green']==255&&
$colorParts['blue']==255))
{
//然后保持相同的颜色
imagesetpixel($newImage,$x,$y,$currentColor);
}
}
}
//最后输出,第二个参数是文件名
imagepng($newImage,'newImage.png');
它让我可以把这个:

进入此区域(此处很难看到alpha,但您可以打开它查看):


你得到了三个答案,其中两个比我的答案好得多,但仍然有效,你为什么不接受一个呢?你能实现我在问题中看到的代码吗?我不知道该怎么做。它会将图像的某一部分放大。但是你能把它包括进来吗?哦,对不起——是的,我把源参数和目标参数弄混了。不过,我不确定你到底想做什么。您希望放大的位(从8x8px到150x150px)只有在原始8x8px图像全为白色时才透明吗?如果不是全白的,是否希望任何部分透明?您希望放大的位(从8x8px到150x150px)仅在原始8x8px图像全白时透明没有。只有我从原始图像中拍摄的部分。好的。让我试着用另一种方式问这个问题。如果原始图像的8x8部分全部为白色,则应使其透明,对吗?(即,基本上不应将其添加到其他图像中)。如果不是全白的呢?白色位应该仍然是透明的,还是应该按原样添加到图像中?例如,如果白色背景上有一个红点,那么背景在图像上出现后是否仍然是白色的?
$filename = 'test.png'; // input image
$image = imagecreatefrompng($filename);

// grab the input image's size
$size = getimagesize($filename);
$width = $size[0];
$height = $size[1];

$newImage = imagecreatetruecolor($width, $height);

// make sure that the image will retain alpha when saved
imagesavealpha($newImage, true);
// fill with transparent pixels first or else you'll
// get black instead of transparent
imagefill($newImage, 0, 0, imagecolorallocatealpha($newImage, 0, 0, 0, 127));

// loop through all the pixels
for ($y = 0; $y < $height; $y++)
{
    for ($x = 0; $x < $width; $x++)
    {
        // get the current pixel's colour
        $currentColor = imagecolorat($image, $x, $y);

        // then break it into easily parsed bits
        $colorParts = imagecolorsforindex($image, $currentColor);

        // if it's NOT white
        if (!($colorParts['red'] == 255 &&
            $colorParts['green'] == 255 &&
            $colorParts['blue'] == 255))
        {
            // then keep the same colour
            imagesetpixel($newImage, $x, $y, $currentColor);
        }
    }
}

// final output, the second arg is the filename
imagepng($newImage, 'newImage.png');