额外补充
图片转ASCII码图


php
function tochars($r, $g, $b, $px = 256, $char = "#ABCDEFGHIJKLMNOPQRSTUVWXYZ@$%??__ff--++~~'' ::.. `` ")
{
if ($px == 0)
return '';
$len = mb_strlen($char);
//灰度值
$gray = floor(($r + $g + $b) / 3);
//将256个像素平均分配给字符
$unit = ceil($px / $len);
//获取当前像素对应的字符
$index = floor($gray / $unit);
if ($index >= $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://s.siushin.com/siushin/upload/php/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 = '<span style="font-size: 8pt;
letter-spacing: 4px;
line-height: 8pt;
font-weight: bold;display: block;
font-family: monospace;
white-space: pre;
margin: 1em 0;">';
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 .= '<br>';
}
echo $str . '</span>';生成指定数量的数字、字符串
php
// 生成随机数
function randNumber($length = 4): string
{
return join('', array_rand(range(0, 9), $length));
}
// 生成随机字符串
function randString($length = 4): string
{
$stringList = array_flip(array_merge(range('a', 'z'), range('A', 'Z')));
return join('', array_rand($stringList, $length));
}
// 生成随机数字+字符串
function randNumberAndString($length = 4): string
{
$stringList = array_flip(array_merge(range(0, 9), range('a', 'z'), range('A', 'Z')));
return join('', array_rand($stringList, $length));
}单引号、双引号的区别
双引号:解析所有转义符
单引号:只解析\\,\'这两个转义符
定界符(heredoc)、nowdoc
- 定界符
php
$name = 'Hello World';
$eof = <<<EOF
$name
I'm a "true" \$man
EOF;
echo $eof;
// Hello World I'm a "true" $man- heredoc - 相当于双引号
php
$name = 'Hello World';
$heredoc = <<<"EOF"
$name
I'm a "true" \$man
EOF;
echo $heredoc;
// Hello World I'm a "true" $man- nowdoc - 不会解析任何转义符
php
$name = 'Hello World';
$nowdoc = <<<'EOF'
$name
I'm a "true" \$man
a\\b\\'c
EOF;
echo $nowdoc;
// $name I'm a "true" \$man a\\b\\'c数值与字符串运算
php
// 取合法数字,如果不是以合法数字开始,转换成0
echo 1 + '3abc'; // 3
// 8.1版本警告:Warning: A non-numeric value encountered in XXX on line 4
echo 1.2 + '4abc'; // 5.2
// 8.1版本警告:Warning: A non-numeric value encountered in XXX on line 6
echo 3 + '2e2'; // 203
echo 2 + 'true'; // 0(8.1版本报错,没有输出)
// 8.1版本报错:Fatal error: Uncaught TypeError: Unsupported operand types: int + string in XXX:12 Stack trace: #0 {main} thrown in