PHP给图片加文字(水印)

准备工作:

 

 

 

代码:

 

<?php
header("Content-type: image/jpeg");    //浏览器输出,如不需要可去掉此行
$im = @imagecreatefromjpeg('bg.jpg');    //从图片建立文件,此处以jpg文件格式为例
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
$text = '16';  //要写到图上的文字
$font = 'arial.ttf';  //写的文字用到的字体。
$srcw=imagesx($im);
imagettftext($im, 40, 0, 20, 80, $grey, $font, $text);
imagettftext($im, 40, 0, 20, 80, $black, $font, $text);
imagettftext($im, 40, 0, 20, 80, $white, $font, $text);
imagepng($im);
imagedestroy($im);
?>

 

【效果】

 

 

如果要将图片保存到某个文件夹,只需将 :

imagepng($im);改为:

imagepng($im,"路径/output.png");

 

 

 

 

 

 

 

 

 

posted @ 2016-10-28 18:07  码农骆驼  阅读(578)  评论(0编辑  收藏  举报