php 图片汉字命名_php 输出汉字png GD显示中文

php输出图片可以适用GD组件来进行。

输出英文字母使用 imagestring — 水平地画一行字符串。

// 建立一幅 100X30 的图像

$im = imagecreate(100, 30);

// 白色背景和蓝色文本

$bg = imagecolorallocate($im, 255, 255, 255);

$textcolor = imagecolorallocate($im, 0, 0, 255);

// 把字符串写在图像左上角

imagestring($im, 5, 0, 0, "Hello world! ", $textcolor);

// 输出图像

header("Content-type: image/png");

imagepng($im);

?>

输出中文的时候就不行了,下面的代码就只能输出英文。

// 建立一幅 100X30 的图像

$im = imagecreate(100, 30);

// 白色背景和蓝色文本

$bg = imagecolorallocate($im, 255, 255, 255);

$textcolor = imagecolorallocate($im, 0, 0, 255);

// 把字符串写在图像左上角

imagestring($im, 5, 0, 0, "Hello world! 你好", $textcolor);

// 输出图像

header("Content-type: image/png");

imagepng($im);

?>

翻了下PHP手册,有时候手册是很强大的,就好比闲来无事翻字典,你会多认识好多字一样。

imagettftext — 用 TrueType 字体向图像写入文本,可以先导入中文字体在输出。

// Set the content-type

header("Content-type: image/png");

// Create the image

$im = imagecreatetruecolor(400, 30);

// Create some colors

$white = imagecolorallocate($im, 255, 255, 255);

$grey = imagecolorallocate($im, 128, 128, 128);

$black = imagecolorallocate($im, 0, 0, 0);

imagefilledrectangle($im, 0, 0, 399, 29, $white);

// The text to draw

$text = 'Testing...测试';

// Replace path by your own font path

$font = 'yh.ttf';

// Add some shadow to the text

imagettftext($im, 20, 0, 11, 21, $grey, $font, $text);

// Add the text

imagettftext($im, 20, 0, 10, 20, $black, $font, $text);

// Using imagepng() results in clearer text compared with imagejpeg()

imagepng($im);

imagedestroy($im);

?>

效果

QQ%E6%88%AA%E5%9B%BE20111126101231.jpg

但要保证php文件是以utf8格式保存的,不然会输出如下乱码,

本函数同时需要 GD 库和  FreeType 库。

QQ%E6%88%AA%E5%9B%BE20111126101452.jpg

原因应该是内部进行了utf8和gbk转码工作,等有机会我再细分析下。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值