注意字符串必须是utf-8格式的。
<?php
header("Content-type: image/jpeg");
$str = "a一二三四五六七八九十 一二三四五六七八九十一二三四五六七八九a十一二三四五六七八九十";
$len = strlen($str);
$nextpos = 0;
$nextlen = 27;
$lines = intval($len/$nextlen)+1;
$fontSize = 10;
$width = 120;
$height = $lines*($fontSize+3);
$x = 1;
$y = $fontSize+2;
$im = imagecreate($width,$height);
$white = imagecolorallocate($im, 255,255,255);
$black = imagecolorallocate($im, 0,0,0);
$gray = ImageColorAllocate($im, 200,200,200);
imagefill($im,0,0,$gray);
/*split the str to lines array*/
$output = new outputstr(&$str,$nextlen);
$output->mainfunc();
for($i=0;$i<count($output->splitstr);$i++)//echo $output->splitstr[$i];die;
imagettftext($im, $fontSize, 0, $x, $y*($i+1), $black, "C:\WINDOWS\Fonts\SIMSUN.TTC", $output->splitstr[$i]);
imagewbmp($im,'ts.wbmp');
imagejpeg($im);
imagedestroy($im);
/*class start*/
/**<class outputstr>
* this class is to split the string which was fixed with unicode character and ascii character
* as lines array by defined line length for other function to use.
*
* run request:the original str which will be splited must be encoded with utf8,if not ,nothing can be get.
*
* $this->splitstr is the array to store lines we want at last.
* @auther :bailing wu <gucdai@yahoo.com.cn>
*/
class outputstr{
/**
* @param str ,store the whole str which must be output as lines.
*/
var $str = "";
/**
* @param len,store the whole str length.
*/
var $len = "";
/**
* @param lines,output lines number.
*/
var $lines = "";
/**
* @param nextpos,next line start position.
*/ //echo "行数:$lines \n";
var $nextpos = 0;
/**
* @param nextlen,use the character number to limit the width of every line
*/
var $nextlen = 27;
/**
* @param splitstr,this param is to store the lines array of the str.
*/
var $splitstr = array();
/*<outputstr>*/
function outputstr(&$str,$nextlen){
$this->nextlen = $nextlen;
$this->str = $str;
$this->len = strlen($this->str);
$this->nextpos = 0;
$this->lines = intval($this->len/$this->nextlen)+1;
}
/*</outputstr>*/
/*<mainfunc>*/
function mainfunc(){
/*<for>*/
/*output lines number.*/
for($i=1;$i<=$this->lines;$i++){
//echo "nextpos:$this->nextpos = nextlen: $this->nextlen \n";
$currentpos = $this->nextpos;
$this->nextpos = $this->getPos($currentpos);
$currentlen = $this->nextpos - $currentpos;
$substr = substr($this->str,$currentpos,$currentlen);
$this->splitstr[] = $substr;
//eval($func);
//echo "$substr \n";
}/*</for>*/
}
/*</mainfunc>*/
/**
* this function get next position for substr.
* @param str ,the whole initial string which must be output by lines.
* @param currentpos,to get current operately string.
* @param nextlen,limit length of the current operately string.
* @return nextpos.
*/
function getPos($currentpos){
$tmp = substr($this->str,$currentpos,$this->nextlen);
$rawtextarray = preg_split("//",$tmp,-1, PREG_SPLIT_NO_EMPTY);
$rawtext = array();
//echo "\n tmp:$tmp \n";die;
for($i=0;$i<count($rawtextarray);$i++)$rawtext[] = ord($rawtextarray[$i]); //ord(char)
$rawtextlen = $this->nextlen;
//<for***********>
for($i=0;$i<$rawtextlen;$i++){
//<if>
if ($rawtext[$i] < 0x80) { // One byte
$asciibytes++; // Ignore ASCII, can throw off count
}//</if>
}//</for*********>
//echo "ascii字节数:$asciibytes \n";
if($asciibytes > 0){
$offset =3-($asciibytes%3);
$nextpos = $currentpos + $this->nextlen - $offset ;
}
else{
$nextpos = $currentpos + $this->nextlen;
}
return($nextpos);
}
}
/*</class outputstr>*/
/*
<example>
echo "<pre>";
$str = "一二三四五w六七八九十一二三aa四五六七八九十一r二三 四五六七八九十一二三四五六七八九十";
$nextlen = 27;
$output = new outputstr(&$str,$nextlen);
$output->mainfunc();
print_r($output->splitstr);
</example>
*/
?>
header("Content-type: image/jpeg");
$str = "a一二三四五六七八九十 一二三四五六七八九十一二三四五六七八九a十一二三四五六七八九十";
$len = strlen($str);
$nextpos = 0;
$nextlen = 27;
$lines = intval($len/$nextlen)+1;
$fontSize = 10;
$width = 120;
$height = $lines*($fontSize+3);
$x = 1;
$y = $fontSize+2;
$im = imagecreate($width,$height);
$white = imagecolorallocate($im, 255,255,255);
$black = imagecolorallocate($im, 0,0,0);
$gray = ImageColorAllocate($im, 200,200,200);
imagefill($im,0,0,$gray);
/*split the str to lines array*/
$output = new outputstr(&$str,$nextlen);
$output->mainfunc();
for($i=0;$i<count($output->splitstr);$i++)//echo $output->splitstr[$i];die;
imagettftext($im, $fontSize, 0, $x, $y*($i+1), $black, "C:\WINDOWS\Fonts\SIMSUN.TTC", $output->splitstr[$i]);
imagewbmp($im,'ts.wbmp');
imagejpeg($im);
imagedestroy($im);
/*class start*/
/**<class outputstr>
* this class is to split the string which was fixed with unicode character and ascii character
* as lines array by defined line length for other function to use.
*
* run request:the original str which will be splited must be encoded with utf8,if not ,nothing can be get.
*
* $this->splitstr is the array to store lines we want at last.
* @auther :bailing wu <gucdai@yahoo.com.cn>
*/
class outputstr{
/**
* @param str ,store the whole str which must be output as lines.
*/
var $str = "";
/**
* @param len,store the whole str length.
*/
var $len = "";
/**
* @param lines,output lines number.
*/
var $lines = "";
/**
* @param nextpos,next line start position.
*/ //echo "行数:$lines \n";
var $nextpos = 0;
/**
* @param nextlen,use the character number to limit the width of every line
*/
var $nextlen = 27;
/**
* @param splitstr,this param is to store the lines array of the str.
*/
var $splitstr = array();
/*<outputstr>*/
function outputstr(&$str,$nextlen){
$this->nextlen = $nextlen;
$this->str = $str;
$this->len = strlen($this->str);
$this->nextpos = 0;
$this->lines = intval($this->len/$this->nextlen)+1;
}
/*</outputstr>*/
/*<mainfunc>*/
function mainfunc(){
/*<for>*/
/*output lines number.*/
for($i=1;$i<=$this->lines;$i++){
//echo "nextpos:$this->nextpos = nextlen: $this->nextlen \n";
$currentpos = $this->nextpos;
$this->nextpos = $this->getPos($currentpos);
$currentlen = $this->nextpos - $currentpos;
$substr = substr($this->str,$currentpos,$currentlen);
$this->splitstr[] = $substr;
//eval($func);
//echo "$substr \n";
}/*</for>*/
}
/*</mainfunc>*/
/**
* this function get next position for substr.
* @param str ,the whole initial string which must be output by lines.
* @param currentpos,to get current operately string.
* @param nextlen,limit length of the current operately string.
* @return nextpos.
*/
function getPos($currentpos){
$tmp = substr($this->str,$currentpos,$this->nextlen);
$rawtextarray = preg_split("//",$tmp,-1, PREG_SPLIT_NO_EMPTY);
$rawtext = array();
//echo "\n tmp:$tmp \n";die;
for($i=0;$i<count($rawtextarray);$i++)$rawtext[] = ord($rawtextarray[$i]); //ord(char)
$rawtextlen = $this->nextlen;
//<for***********>
for($i=0;$i<$rawtextlen;$i++){
//<if>
if ($rawtext[$i] < 0x80) { // One byte
$asciibytes++; // Ignore ASCII, can throw off count
}//</if>
}//</for*********>
//echo "ascii字节数:$asciibytes \n";
if($asciibytes > 0){
$offset =3-($asciibytes%3);
$nextpos = $currentpos + $this->nextlen - $offset ;
}
else{
$nextpos = $currentpos + $this->nextlen;
}
return($nextpos);
}
}
/*</class outputstr>*/
/*
<example>
echo "<pre>";
$str = "一二三四五w六七八九十一二三aa四五六七八九十一r二三 四五六七八九十一二三四五六七八九十";
$nextlen = 27;
$output = new outputstr(&$str,$nextlen);
$output->mainfunc();
print_r($output->splitstr);
</example>
*/
?>