查看: 5559|回复: 0
打印 上一主题 下一主题

PHP实现在图片中添加中文文字

[复制链接]
跳转到指定楼层
1#
发表于 2007-9-27 22:48:02 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
台州网址导航
<?php
/*
注意:需要gd库支持,需要iconv支持(php5已经包含不用加载)
*version 1.0
*@author sanshi(叁石)
*MSN: sanshi0815@tom.com
*Create 2005/10/20
在图片中添加中文文字
*/
/*
param $image 图象资源
param size 字体大小
param angle 字体输出角度
param showX 输出位置x坐标
param showY 输出位置y坐标
param font 字体文件位置
param content 要在图片里显示的内容
*/
class showChinaText
{
var $text='你好';
var $font='fs.ttf';
var $angle=0;
var $size=50;
var $showX=100;
var $showY=100;
function showChinaText($showText='')
{
$this->text=!isset($showText)?$showText:$this->text;
//exit();
$this->show();
}
function createText($instring)
{
$outstring="";
$max=strlen($instring);
for($i=0;$i<$max;$i++)
{
$h=ord($instring[$i]);
if($h>=160 && $i<$max-1)
{
$outstring.="&#".base_convert(bin2hex(iconv("gb2312","ucs-2",substr ($instring,$i,2))),16,10).";";
$i++;
}
else
{
$outstring.=$instring[$i];
}
}
return $outstring;
}
function createJpeg()
{}
function show()
{
//输出头内容
Header( "Content-type: image/png");
//建立图象
$image = imagecreate(400,300);
//定义颜色
$red = ImageColorAllocate($image,255,0,0);
$white = ImageColorAllocate($image,255,255,255);
$black=ImageColorAllocate($image,0,0,0);
//填充颜色
ImageFilledRectangle($image,0,0,200,200,$red);
//显示文字
$txt=$this->createText($this->text);
//写入文字
imagettftext($image,$this->size, $this->angle, $this->showX, $this->showY,$white,$this->font,$txt);
//ImageString($image,5,50,10,$txt,$white);
//显示图形
imagejpeg($image);
ImageDestroy($image);
}
}
//本类,并没有经过很好的考虑,只是简单的进行了封装,以后有机会,可能跟原来的图片类整合
?>
<?php
//使用示例
$s = new showChinaText();
?>
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 转播转播 分享分享 分享淘帖
台州维博网络(www.tzweb.com)专门运用PHP+MYSQL/ASP.NET+MSSQL技术开发网站门户平台系统等。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

网站推广
关于我们
  • 台州朗动科技(Tzweb.com)拥有多年开发网站平台系统门户手机客户端等业务的成功经验。主要从事:政企网站,系统平台,微信公众号,各类小程序,手机APP客户端,浙里办微应用,浙政钉微应用、主机域名、虚拟空间、后期维护等服务,满足不同企业公司的需求,是台州地区领先的网络技术服务商!

Hi,扫描关注我

Copyright © 2005-2026 站长论坛 All rights reserved

Powered by 站长论坛 with TZWEB Update Techonolgy Support

快速回复 返回顶部 返回列表