十四节 图片转ASCII码图

最后更新于:2022-04-02 01:47:26

>[success] # 图片转ASCII码图 * [案例图](http://www.degraeve.com/img2txt.php) * [手把手教你图片转ASCII码图](https://www.cnblogs.com/jelly7723/p/5657891.html) * [图片转字符](https://segmentfault.com/a/1190000007692741) ![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/b577e6d90fa106b736db964948927712_896x428.png) ![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/bd855ff2eadf838f37becc2a62f9d23a_919x525.jpg) ~~~php = $len) { $index = $len - 1; } return $char[(int)$index]; } function tosmallim($const = 100, $width, $height, $image) { if ($width > $const) { $times = floor($width / $const); $smwidth = $const; $smheight = floor($height / $times); $im = imagecreatetruecolor($smwidth, $smheight); imagecopyresampled($im, $image, 0, 0, 0, 0, $smwidth, $smheight, $width, $height); return [$im, $smwidth, $smheight]; } return [$image, $width, $height]; } $imname = 'http://images2015.cnblogs.com/blog/763758/201606/763758-20160612082543871-1901077809.png'; //返回一图像标识符,代表了从给定的文件名取得的图像 $image = ImageCreateFromPng($imname); //$im = ImageCreateFromJpeg($imname); $size = getimagesize($imname); $width = $size[0]; $height = $size[1]; list($image, $width, $height) = tosmallim(100, $width, $height, $image); $arr = []; for ($i = 0; $i < $height; $i++) { for ($j = 0; $j < $width; $j++) { $rgb = ImageColorat($image, $j, $i); $r = ($rgb >> 16) & 0xFF; $g = ($rgb >> 8) & 0xFF; $b = $rgb & 0xFF; $arr[] = floor(($r + $g + $b) / 3); } } $num = count(array_unique($arr)); $str = ''; for ($i = 0; $i < $height; $i++) { for ($j = 0; $j < $width; $j++) { $rgb = ImageColorat($image, $j, $i); $r = ($rgb >> 16) & 0xFF; $g = ($rgb >> 8) & 0xFF; $b = $rgb & 0xFF; $str .= tochars($r, $g, $b, $num); } $str .= '
'; } echo $str . '
'; ~~~
';