[普通]取图片色系并生成反差的字体

作者(passion) 阅读(1084次) 评论(0) 分类( html)

取平均颜色:

function imgColor($imgUrl)
{
    $imageInfo = getimagesize($imgUrl);
    $imgType   = strtolower(substr(image_type_to_extension($imageInfo[2]), 1));
    $imageFun  = 'imagecreatefrom' . ($imgType == 'jpg' ? 'jpeg' : $imgType);
    $i         = $imageFun($imgUrl);
    $rColorNum = $gColorNum = $bColorNum = $total = 0;
    for ($x = 0; $x < imagesx($i); $x++) {
        for ($y = 0; $y < imagesy($i); $y++) {
            $rgb = imagecolorat($i, $x, $y);
            $r   = ($rgb >> 16) & 0xFF;
            $g   = ($rgb >> 8) & 0xFF;
            $b   = $rgb & 0xFF;
            $rColorNum += $r;
            $gColorNum += $g;
            $bColorNum += $b;
            $total++;
        }
    }
    $rgb      = array();
    $rgb['r'] = round($rColorNum / $total);
    $rgb['g'] = round($gColorNum / $total);
    $rgb['b'] = round($bColorNum / $total);
    return $rgb;
}
 
$rgb = imgColor("图片地址");
print_r($rgb); //将打印出一个数组



根据函数我们可以理解到,此函数运行时会依次遍历所有的像素点。然后取出现最多的像素点。

改进一下函数:


function imgColor($imgUrl)
{
    $imageInfo = getimagesize($imgUrl);
    $imgType   = strtolower(substr(image_type_to_extension($imageInfo[2]), 1));
    $imageFun  = 'imagecreatefrom' . ($imgType == 'jpg' ? 'jpeg' : $imgType);
    $i         = $imageFun($imgUrl);
    $rColorNum = $gColorNum = $bColorNum = $total = 0;
    for ($x = 50; $x < imagesx($i) - 50; $x+=20) {
        for ($y = 50; $y < imagesy($i) - 50; $y+=20) {
            $rgb = imagecolorat($i, $x, $y);
            $r   = ($rgb >> 16) & 0xFF;
            $g   = ($rgb >> 8) & 0xFF;
            $b   = $rgb & 0xFF;
            $rColorNum += $r;
            $gColorNum += $g;
            $bColorNum += $b;
            $total++;
        }
    }
    $rgb      = array();
    $rgb['r'] = round($rColorNum / $total);
    $rgb['g'] = round($gColorNum / $total);
    $rgb['b'] = round($bColorNum / $total);
    return $rgb;
}
$rgb = imgColor("图片地址");
print_r($rgb); //将打印出一个数组 
 
/**
* x=50 
* imagesx($i) - 50
* y=50
* imagesy($i) - 50
* 相当于 margin 属性 
**/

生成背景色反差字体:

$gray      = '255,255,255';
$grayLevel = $img['r'] * 0.299 + $img['g'] * 0.587 + $img['b'] * 0.114;
if ($grayLevel >= 150) {
    $gray = '0,0,0';
}

希望能帮到需要的朋友。转载请注明出处。谢谢。

« 上一篇:wifi共享上网(至尊版wifi)
« 下一篇:ASP.NET附加数据库文件的方式,如何发布到IIS7而不导致SQLServer出错
在这里写下您精彩的评论
  • 微信

  • QQ

  • 支付宝

返回首页
返回首页 img
返回顶部~
返回顶部 img