Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
一言签名档
<?php
// download hitokoto.json from https://github.com/kokororin/hitokoto
$hitokoto = json_decode(file_get_contents('./hitokoto.json'),true);
$h = $hitokoto[array_rand($hitokoto)];
$hh = $h['hitokoto'];
$count = mb_strlen($hh,'UTF-8');
$height = $count * 27;
$width = 30;
$line = 1;
//换行还是有点bug 不想思考了
if($count > 30){
$height = 800;
$line = ceil($count / 30);
$width = 30 * $line;
}
$image = imagecreate($height, $width);
$bg = imagecolorallocate($image, 255, 255, 255);
$font = './1.TTF';
$black = imagecolorallocate($image, 0, 0, 0);
imagecolortransparent($image,$bg);
header('Content-type: image/png');
for ($i=0; $i < $line ; $i++) {
$t = mb_substr($hh,0,30);
$hh = str_replace($t,'',$hh);
imagettftext($image, 20, 0, 1, $i * 28 + 25, $black, $font, $t);
}
imagepng($image);
imagedestroy($image);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment