美文网首页
php-获取图片主要色值,RGB HEX 转换

php-获取图片主要色值,RGB HEX 转换

作者: 若初_终点 | 来源:发表于2020-04-04 10:58 被阅读0次
    
    PHP获取图片主要色值,RGB HEX 转换
    
    /*
     *图片主要(三通道)颜色判断
     */
    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 TO HEX
     */
    function rgb2html($r, $g=-1, $b=-1)
    {
        if (is_array($r) && sizeof($r) == 3)
            list($r, $g, $b) = $r;
        $r = intval($r); $g = intval($g);
        $b = intval($b);
        $r = dechex($r<0?0:($r>255?255:$r));
        $g = dechex($g<0?0:($g>255?255:$g));
        $b = dechex($b<0?0:($b>255?255:$b));
        $color = (strlen($r) < 2?'0':'').$r;
        $color .= (strlen($g) < 2?'0':'').$g;
        $color .= (strlen($b) < 2?'0':'').$b;
        return '#'.$color;
    }
    /*
     *HEX TO RGB
     *author cuitengwei
     *2014/1/16
     */
    function html2rgb($color)
    {
        if ($color[0] == '#')
            $color = substr($color, 1);
        if (strlen($color) == 6)
            list($r, $g, $b) = array($color[0].$color[1],
                                     $color[2].$color[3],
                                     $color[4].$color[5]);
        elseif (strlen($color) == 3)
            list($r, $g, $b) = array($color[0].$color[0], $color[1].$color[1], $color[2].$color[2]);
        else
            return false;
        $r = hexdec($r); $g = hexdec($g); $b = hexdec($b);
        return array($r, $g, $b);
    }
    //使用示例
    $imgUrl = "D:/wamp/www/vtest/test.jpg";//图片地址
    var_dump(imgColor($imgUrl));
    var_dump(rgb2html(245,255,244));
    var_dump(html2rgb('#F08098'));
    
    
    
    $i=imagecreatefromjpeg("photo3.jpg");//测试图片
    for ($x=0;$x<imagesx($i);$x++) {
        for ($y=0;$y<imagesy($i);$y++) {
            $rgb = imagecolorat($i,$x,$y);
            $r=($rgb >>16) & 0xFF;
            $r=($rgb >>16) & 0xFF;
            $g=($rgb >> & 0xFF;
            $b=$rgb & 0xFF;
            $rTotal += $r;
            $gTotal += $g;
            $bTotal += $b;
            $total++;
        }
    }
    $rAverage = round($rTotal/$total);
    $gAverage = round($gTotal/$total);
    $bAverage = round($bTotal/$total);
    //示例:
    echo $rAverage;
    
    
    
    $average = new Imagick("xiaocai.jpg");
    $average->quantizeImage( 10, Imagick::COLORSPACE_RGB, 0, false, false );
    $average->uniqueImageColors();
    function GetImagesColor( Imagick $im ){
        $colorarr = array();
        $it = $im->getPixelIterator();
        $it->resetIterator();
        while( $row = $it->getNextIteratorRow() ){
            foreach ( $row as $pixel ){
                $colorarr[] = $pixel->getColor();
            }
        } // bbs.it-home.org
        return $colorarr;
    }
    $colorarr = GetImagesColor($average);
    foreach($colorarr as $val){
        echo "";
    }
    
    
    $average = new Imagick("./fruit/143511081321676_593.jpg");
    $average->quantizeImage( 10, Imagick::COLORSPACE_RGB, 0, false, false );
    $average->uniqueImageColors();
    function GetImagesColor( Imagick $im ){
        $colorarr = array();
        $it = $im->getPixelIterator();
        $it->resetIterator();
        while( $row = $it->getNextIteratorRow() ){
            foreach ( $row as $pixel ){
                $colorarr[] = $pixel->getColor();
            }
        }
        return $colorarr;
    }
    $colorarr = GetImagesColor($average);
    foreach($colorarr as $val){
        $r += $val['r'];
        $g += $val['g'];
        $b += $val['b'];
        echo "<div style='background-color: rgb({$val['r']},{$val['g']},{$val['b']});width:50px;height:50px;float:left;'></div>";
    }
    $r = round($r/10);
    $g = round($g/10);
    $b = round($b/10);
    echo "<br><div style='background-color: rgb({$r},{$g},{$b});width:100px;height:100px;float:left;'></div>";
    
    --------------------------------------------------------------------------------------
    

    相关文章

      网友评论

          本文标题:php-获取图片主要色值,RGB HEX 转换

          本文链接:https://www.haomeiwen.com/subject/glmdphtx.html