The PHP GD library parses a simple picture and outputs it


Here we will only show the pictures of 2 color values and briefly describe the following concepts.


GD library must be installed first. Otherwise, the following code will not work.

$size = getimagesize('2.png');  //Get Picture Size
 $res = imagecreatefrompng('2.png');  //Get the resource object of the specified picture
 for ($i = 0;   $i <   $size[1];  ++$i) {
 for ($j = 0;   $j <   $size[0];  ++$j) {
 $rgb = imagecolorat($res, $i, $j);  //Get Coordinate Index
 $rgbarray = imagecolorsforindex($res, $rgb);  //Get rgb color of each coordinate
 $sum = $rgbarray['red'] + $rgbarray['green'] + $rgbarray['blue'];  // rgb color values are added to distinguish
 * The demo picture consists of pure black rgb(0,0,0) colors and other colors
 if ($sum == 0) {
 $data[$i][$j] = 1;
 } else {
 $data[$i][$j] = 2;

The above code has already generated a color block for each pixel of the whole picture.

echo "<div style='border:1px solid #ccc;  width: {$size[1]}px;  height: {$size[0]}px;'  >";
 for ($i = 0;   $i < count ($data);  $i++) {
 if (array_sum ($data[$i]) !  = 200) {
 for ($j = 0;   $j < count ($data[$i]);  $j++) {
 if ($data[$i][$j] == 1) {
 echo '<div style="width:1px;  height:1px;  background: #cccccc;  float:left;"  ></div>';
 } else {
 echo '<div style="width:1px;  height:1px;  background: red;  float:left;"  ></div>';
 echo "</div>";

Through the above code, a picture piled up by pixel blocks like the specified picture can be generated.

The similarity _ text function can judge the similarity between the two values. I’ll consider whether I can use the binary code of picture a to compare with picture b. Judging the similarity?
It is only a concept, and we will continue to study it, so that we can realize the function of character recognition.