Detailed explanation of converting PHP scanned images to bitmaps and QR codes to bitmaps

藏色散人
Release: 2023-04-09 21:06:01
forward
3392 people have browsed it

This article will introduce to you how to convert scanned images to bitmaps with PHP and convert QR codes to bitmaps. It has certain reference value. Friends who need it can refer to it. I hope it will be helpful to everyone.

/** * 图片转点阵(黑白) * @param string $imgPath * @return array */ function imgToLattice(string $imgPath): array { $size = getimagesize($imgPath);// 得到图片的信息 $im = imagecreatefrompng($imgPath);// 創建一張圖片 // 储存二进制数组 $lattice = []; $white = [ 'red' => 255, 'green' => 255, 'blue' => 255, 'alpha' => 0, ]; for ($i = 0; $i < $size[1]; ++ $i) { $lattice[$i] = ''; for ($j = 0; $j < $size[0]; ++$j) { $rgb = imagecolorat($im, $j, $i); //取得某像素的颜色索引值 $rgbArr = imagecolorsforindex($im, $rgb); if ($white === $rgbArr){ $lattice[$i] .= 0; }else{ $lattice[$i] .= 1; } } } return [$lattice, $size]; }
Copy after login

Note:

$rgbArr = imagecolorsforindex($im, $rgb);
Copy after login

This returns an RGB array, which is the same as the $white array. Because the QR code only has black and white, I only make a black and white judgment here. Black is 1 and white is 0. If your picture supports more than three levels, you can make a judgment here and splice other numbers

if ($white === $rgbArr){ $lattice[$i] .= 0;}else{ $lattice[$i] .= 1;}
Copy after login

to print out the effect:

Convert to HTML:

Reference code:
$this->image Just replace the data generated by PHP with your own

"; foreach ($this->image as $item) { $str .= $this->getBinaryOutRow($item); } $str .= "

"; return $str; } /** * 二进制输出方法 HTML 输出一行视图 * @param string $string * @return string */ function getBinaryOutRow(string $string): string { $strLen = mb_strlen($string); $html = [ "

", "

", ]; $str="

"; for($i=0;$i<$strLen;$i++) { $str .= $html[$string[$i]]; } $str.="

"; return $str; } public function getHtml() { echo " 测试
"; } public function getFoot() { echo ' '; }}$lattice = new LatticeOutput();$lattice->getHtml();echo $lattice->getBinaryOutHtml();$lattice->getFoot();
Copy after login

[Recommended learning:PHP video tutorial]

The above is the detailed content of Detailed explanation of converting PHP scanned images to bitmaps and QR codes to bitmaps. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:learnku.com
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
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!