PHP:如何表达“接近白色"作为颜色? [英] PHP: how to express "near white" as a color?

查看:47
本文介绍了PHP:如何表达“接近白色"作为颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有修剪图像周围白色区域的功能,其功能类似于

I have a function for trimming white areas around images that works something like

if(imagecolorat($img, $x, $top) != 0xFFFFFF) {
    //sets where the top part of the image is trimmed
}

问题是某些图像偶尔会出现杂散像素,其接近白色,因此不明显,但由于不是精确的0xFFFFFF而是0xFFFEFF或类似的东西,所以会增加裁剪速度.我该如何重写上面的语句,以使它对于接近白色的图像(例如,低至0xFDFDFD)的评估正确,显然无需测试任何可能的值.

The problem is some images have an occasional stray pixel that is so near white that it's unnoticeable but screws up the cropping because it's not exactly 0xFFFFFF but 0xFFFEFF or something. How could I rewrite the above statement so that it evaluates true for those near white images, say down to 0xFDFDFD, obviously without testing for ever possible value.

推荐答案

$color = imagecolorat($img, $x, $top);
$color = array(
    'red'   => ($color >> 16) & 0xFF,
    'green' => ($color >>  8) & 0xFF,
    'blue'  => ($color >>  0) & 0xFF,
);
if ($color['red']   >= 0xFD
 && $color['green'] >= 0xFD
 && $color['blue']  >= 0xFD) {
    //sets where the top part of the image is trimmed
}

有关所用运算符的说明,请阅读:

For a description of the operators used, please read:

这篇关于PHP:如何表达“接近白色"作为颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆