PHP如何实现图片转点阵和二维码转点阵的详细步骤?

2026-04-05 15:3010阅读0评论SEO问题
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计719个文字,预计阅读时间需要3分钟。

PHP如何实现图片转点阵和二维码转点阵的详细步骤?

本篇文章为大家介绍PHP扫描图像转换点阵和二维码转换点阵,具有一定的参考价值。需要的朋友可以参考一下,希望能对大家有所帮助。

图片转点阵(黑白)

php* @param string $imgPath* @return array*/function imageToPixelArray($imgPath) { // 省略图片读取和转换代码}

二维码转点阵

php* @param string $imgPath* @return array*/function qrCodeToPixelArray($imgPath) { // 省略二维码读取和转换代码}

本篇文章给大家介绍PHP扫描图片转点阵 二维码转点阵,有一定的参考价值,需要的朋友可以参考一下,希望对大家有所帮助。

/** * 图片转点阵(黑白) * @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]; }

注解:

$rgbArr = imagecolorsforindex($im, $rgb);

这里返回一个RGB数组,跟$white数组一样,我因为二维码只有黑白,所以这里只做了黑白判断,黑就是1,白就是0,如果你的图片支持三种级以上,这里可以做判断,拼接其他数字

PHP如何实现图片转点阵和二维码转点阵的详细步骤?

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

打印出来效果:

转成HTML:

参考代码:
$this->image就上面PHP生成的数据,替换成自己的就行了

<?phpnamespace Lattice\LatticePck;/** * 点阵输出类 * Class LatticeOutput */class LatticeOutput extends Lattice{ /** * 二进制输出方法 HTML 把当前视图输出 * * @return string */ function getBinaryOutHtml(): string { $str="<html><body><p class='k'>"; foreach ($this->image as $item) { $str .= $this->getBinaryOutRow($item); } $str .= "</p></body> </html>"; return $str; } /** * 二进制输出方法 HTML 输出一行视图 * @param string $string * @return string */ function getBinaryOutRow(string $string): string { $strLen = mb_strlen($string); $html = [ "<p></p>", "<p class='h'></p>", ]; $str="<p class='g'>"; for($i=0;$i<$strLen;$i++) { $str .= $html[$string[$i]]; } $str.="</p>"; return $str; } public function getHtml() { echo "<!DOCTYPE\"> <html xmlns=\"www.w3.org/1999/xhtml\"> <head> <meta name=\"viewport\" content=\"width=device-width,user-scalabl e=no\"> <title>测试</title> <style> .k { margin: 0px; padding: 0px; width: 296px; height:128px; background-color: #F8F8F8; float: none; border: 2px solid #999999; } .g { margin: 0px; padding: 0px; width: 100%; float: none; height: 1px; } .h{ background-color: #000000; } .g p{ margin: 0px; padding: 0px; width: 1px; float: left; height: 1px; position: relative; } .g p.h{ background-color: #000000; } </style> </head> <body>"; } public function getFoot() { echo '</body> </html>'; }}$lattice = new LatticeOutput();$lattice->getHtml();echo $lattice->getBinaryOutHtml();$lattice->getFoot();

本文共计719个文字,预计阅读时间需要3分钟。

PHP如何实现图片转点阵和二维码转点阵的详细步骤?

本篇文章为大家介绍PHP扫描图像转换点阵和二维码转换点阵,具有一定的参考价值。需要的朋友可以参考一下,希望能对大家有所帮助。

图片转点阵(黑白)

php* @param string $imgPath* @return array*/function imageToPixelArray($imgPath) { // 省略图片读取和转换代码}

二维码转点阵

php* @param string $imgPath* @return array*/function qrCodeToPixelArray($imgPath) { // 省略二维码读取和转换代码}

本篇文章给大家介绍PHP扫描图片转点阵 二维码转点阵,有一定的参考价值,需要的朋友可以参考一下,希望对大家有所帮助。

/** * 图片转点阵(黑白) * @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]; }

注解:

$rgbArr = imagecolorsforindex($im, $rgb);

这里返回一个RGB数组,跟$white数组一样,我因为二维码只有黑白,所以这里只做了黑白判断,黑就是1,白就是0,如果你的图片支持三种级以上,这里可以做判断,拼接其他数字

PHP如何实现图片转点阵和二维码转点阵的详细步骤?

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

打印出来效果:

转成HTML:

参考代码:
$this->image就上面PHP生成的数据,替换成自己的就行了

<?phpnamespace Lattice\LatticePck;/** * 点阵输出类 * Class LatticeOutput */class LatticeOutput extends Lattice{ /** * 二进制输出方法 HTML 把当前视图输出 * * @return string */ function getBinaryOutHtml(): string { $str="<html><body><p class='k'>"; foreach ($this->image as $item) { $str .= $this->getBinaryOutRow($item); } $str .= "</p></body> </html>"; return $str; } /** * 二进制输出方法 HTML 输出一行视图 * @param string $string * @return string */ function getBinaryOutRow(string $string): string { $strLen = mb_strlen($string); $html = [ "<p></p>", "<p class='h'></p>", ]; $str="<p class='g'>"; for($i=0;$i<$strLen;$i++) { $str .= $html[$string[$i]]; } $str.="</p>"; return $str; } public function getHtml() { echo "<!DOCTYPE\"> <html xmlns=\"www.w3.org/1999/xhtml\"> <head> <meta name=\"viewport\" content=\"width=device-width,user-scalabl e=no\"> <title>测试</title> <style> .k { margin: 0px; padding: 0px; width: 296px; height:128px; background-color: #F8F8F8; float: none; border: 2px solid #999999; } .g { margin: 0px; padding: 0px; width: 100%; float: none; height: 1px; } .h{ background-color: #000000; } .g p{ margin: 0px; padding: 0px; width: 1px; float: left; height: 1px; position: relative; } .g p.h{ background-color: #000000; } </style> </head> <body>"; } public function getFoot() { echo '</body> </html>'; }}$lattice = new LatticeOutput();$lattice->getHtml();echo $lattice->getBinaryOutHtml();$lattice->getFoot();