Home > Backend Development > PHP Tutorial > How to obtain the color value of an image in PHP_PHP Tutorial

How to obtain the color value of an image in PHP_PHP Tutorial

WBOY
Release: 2016-07-13 10:23:48
Original
879 people have browsed it

PHP 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 and detects the main color of the image by reading it through the imagecreatefromjpeg function. It takes the picture and recycles it to obtain each color value for calculation.

The specific code is as follows:

 $i=imagecreatefromjpeg("photo3.jpg");//Test picture, define one yourself, pay attention to the path

 for ($x=0;$x

for ($y=0;$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);

//Example:

echo $rAverage;

 ?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/834955.htmlTechArticleHow to obtain the color value of an image in PHP. This example describes how to obtain the color value of an image in PHP. PHP obtains an image. The main color of the color value detection picture is read through the imagecreatefromjpeg function...
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