PHP implements the method of obtaining the color value of an image, _PHP tutorial

WBOY
Release: 2016-07-13 10:23:25
Original
885 people have browsed it

PHP implements the method of obtaining the color value of an image,

This example describes the method of obtaining the color value of an image in PHP. PHP obtains the color value of an image and detects the main color of the image by reading the image through the imagecreatefromjpeg function, and then looping to obtain each color value for calculation.

The specific code is as follows:

<&#63;php
$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;
    $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;
&#63;>
Copy after login

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/840630.htmlTechArticlePHP implements the method of obtaining the image color value. This example describes the PHP method of obtaining the image color value. PHP obtains The image color value detects the main color of the image through the imagecreatefromjpeg function...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!