PHP生成带中文的图片

imagettftext() 函数是 PHP 中的一个内置函数,用于使用 TrueType 字体将文本写入图像。

句法:

数组imagettftext(资源$image,float $size,float $angle,
          int $x,int $y,int $color,string $fontfile,string $text)
参数:此函数接受上述八个参数,如下所述:

$image:它指定要处理的图像。
$size:它指定要使用的字体大小,以磅为单位。
$angle:它以度为单位指定角度。
$x:指定 x 坐标。
$y:它指定 y 坐标。
$color:它指定文本所需颜色的索引。
$fontfile:它指定要使用的字体。
$text:它指定要写入的文本。
返回值:此函数在成功时返回一个数组。

下面的示例说明了PHP 中的imagettftext() 函数。

<?php
// set up image 
$im = ImageCreateTrueColor(200, 100);
$black = ImageColorAllocate ($im, 0, 0, 0);
$white = ImageColorAllocate ($im, 255, 255, 255);

$wenben = "阿酷tony简体中文标题";
ImageFill($im, 0, 0, $black);
//ImageLine($im, 0, 0, 200, 200, $white);
$font = "msyh.ttf";

imagettftext($im, 14, 0, 20, 40, $white , $font, $wenben);

// output image
Header ('Content-type: image/png');
ImagePng($im);
// clean up 
ImageDestroy($im);
?>


 

$bgbgimg='bg.png';
$bgObj=imagecreatefrompng($bgbgimg);
$bg_width=imagesx($bgObj);
$bg_height=imagesy($bgObj);
$bgObj_bg=imagecreatetruecolor($bg_width,$bg_height);
 
imagecopy($bgObj_bg,$bgObj,0,0,0,0,$bg_width,$bg_height);
 
$imgpath="test4.png";
$imgObj = imagecreatefrompng($imgpath);
$wh = getimagesize($imgpath);
$imgwidth=intval($wh[0]);
$imgheight=intval($wh[1]);
 
/**
 * $dst_image:目标图连接资源
 * $src_image:源图连接资源
 * $dst_x:目标图 X 坐标点。 以图片左上角为源点,源图要画在目标图的开始坐标x
 * $dst_y:目标图 Y 坐标点。 以图片左上角为源点,源图要画在目标图的开始坐标Y
 * $src_x:源图 X坐标点。 以图片左上角为源点,将源图从X位置开始的部分画在目标图上
 * $src_y:源图 y坐标点。 以图片左上角为源点,将源图从Y位置开始的部分画在目标图上
 * $dst_w:目标图宽度   在目标图上画的宽度
 * $dst_h:目标图高度   在目标图上画的高度
 * $src_w:源图宽度    要画在目标图上的宽度
 * $src_h:源图高度    要画在目标图上的高度
 * 例如:目标图为空白 400*400,源图为200*200,下面表示将源图从100*100的位置开始截取50*50的内容在目标图100*100的位置开始画,画的宽度是30*30
 * imagecopyresampled($bgObj_bg,$imgObj,100,100,100,100,30,30,50,50);
 */
imagecopyresampled($bgObj_bg,$imgObj,100,100,100,100,30,30,50,50);
//输出图片
imagepng($bgObj_bg,'img/real.jpg',9);

参考: 

PHP: imagettftext - Manual

猜你喜欢

转载自blog.csdn.net/ffffffff8/article/details/133303741