Detect “overall average” color of the picture

前端 未结 7 990
走了就别回头了
走了就别回头了 2020-12-04 05:53

I have a jpg image.

I need to know \"overall average\" the color of the image. At first glance there can use the histogram of the image (channel RGB).

At wor

7条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-04 06:14

    Combining JKirchartz and Alexander Hugestrand answer:

     function getAverage($sourceURL){
    
        $image = imagecreatefromjpeg($sourceURL);
        $scaled = imagescale($image, 1, 1, IMG_BICUBIC); 
        $index = imagecolorat($scaled, 0, 0);
        $rgb = imagecolorsforindex($scaled, $index); 
        $red = round(round(($rgb['red'] / 0x33)) * 0x33); 
        $green = round(round(($rgb['green'] / 0x33)) * 0x33); 
        $blue = round(round(($rgb['blue'] / 0x33)) * 0x33); 
        return sprintf('#%02X%02X%02X', $red, $green, $blue); 
     }
    

    Tried and tested, returns hex string.

提交回复
热议问题