1、自动生成图片,在图片上生成文字
Header("Content-type: image/gif"); //设置Header,如果输出图片不成功,请先暂时去掉这代码查看错误
$im = imagecreate(400,30); // 创建一个真彩的图片背景,参数是 X长
,Y宽。
$black = ImageColorAllocate($im,
143,0,0); //设置背景颜色,根据rgb
imagefill($im, 0, 0, $black); //填充背景颜色
$white = ImageColorAllocate($im, 255,255,255); //设置背景颜色,根据rgb
//这里用的都是TTF字体。懒得上网找就到 C:\WINDOWS\Fonts 下,Copy几个字体到程序目录下就好了
// 写入字体,如果写入中文时乱码请确认字体是否支持中文,并且把文件转换为“Encode in UTF-8 without
ROM”
//ImageTTFText设置字体的背景、大小、角度、坐标、字体和显示内容
ImageTTFText($im, 20, 0, 10, 20, $white, "css/FZYTK.ttf",
"中国人!!");
ImageGif($im); //创建图片
ImageDestroy($im); //结束图像,释放内存空间
?>
2、打开图片,在图片上打图片水印
// Create image instances
$src = imagecreatefromgif('images/btn1.gif');
//$dest = imagecreatetruecolor(80, 40);
$dest =imagecreatefromgif('images/12.gif');
// Copy
imagecopy($dest, $src, 50, 10, 0, 0, 67, 23);
// Output and free from memory
header('Content-Type: image/gif');
imagegif($dest);
imagedestroy($dest);
imagedestroy($src);
?>
3、加载图片,然后在图片上生成文字
$host = trim($_GET["host"]);
$type = intval($_GET["type"]);
$score = intval($_GET['score']);
$percent = intval(trim($_GET['percent']))."%";
$img_file = array( //加载不同推按,图片数组。
0 =>
dirname(dirname(dirname(__FILE__)))."/public/img/share/share0.png",
1 =>
dirname(dirname(dirname(__FILE__)))."/public/img/share/share1.png",
2 =>
dirname(dirname(dirname(__FILE__)))."/public/img/share/share2.png",
3 =>
dirname(dirname(dirname(__FILE__)))."/public/img/share/share3.png",
4 =>
dirname(dirname(dirname(__FILE__)))."/public/img/share/share4.png",
5 =>
dirname(dirname(dirname(__FILE__)))."/public/img/share/share5.png"
);
$font_file =
dirname(dirname(dirname(__FILE__)))."/public/img/share/arial.ttf";
//加载文字的字体
$img_bg = imagecreatefrompng($img_file[$type]); //打开图片,图片是数组
$bg_width = imagesx($img_bg);
$clr = imagecolorallocate($img_bg, 5, 164, 65);
$black = imagecolorallocate($img_bg, 10, 10, 10);
if(strlen($host) > 20){
$host = "...".substr($host,-20);
}
imagettftext($img_bg, 12, 0, 90, 50, $black, $font_file,
$host);
if($type != 0 && $score !=
0){
imagettftext($img_bg, 18, 0, 208, 86, $clr, $font_file,
strval($score));
imagettftext($img_bg, 16, 0, 145, 164, $clr, $font_file,
$percent);
}
header('Expires: Thu, 01 Jan 1970 00:00:00 GMT');
// HTTP/1.1
header('Cache-Control: private, no-store, no-cache,
must-revalidate');
header('Cache-Control: post-check=0, pre-check=0, max-age=0',
false);
// HTTP/1.0
header('Pragma: no-cache');
header('Content-type: image/png');
imagepng($img_bg);
imagedestroy($img_bg);
?>