php打印文字到图片,【PHP】打开一个图片然后加上水印(图片上生成文字)

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);

?>

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值