计应134 寿燕
一、对图片添加水印
字符串编码转换string iconv ( string $in_charset , string $out_charset , string $str );
<?php
$dst_path = '1.jpg';
$src_path = '2.jpg';
//创建图片的实例
$dst = imagecreatefromstring(file_get_contents($dst_path));
$src = imagecreatefromstring(file_get_contents($src_path));
//获取水印图片的宽高
list($src_w, $src_h) = getimagesize($src_path);
//将水印图片复制到目标图片上,最后个参数50是设置透明度,这里实现半透明效果
imagecopymerge($dst, $src, 10, 10, 0, 0, $src_w, $src_h, 50);
//如果水印图片本身带透明色,则使用imagecopy方法
//imagecopy($dst, $src, 10, 10, 0, 0, $src_w, $src_h);
//输出图片
list($dst_w, $dst_h, $dst_type) = getimagesize($dst_path);
switch ($dst_type) {
case 1://GIF
header('Content-Type: image/gif');
imagegif($dst);
break;
case 2://JPG
header('Content-Type: image/jpeg');
imagejpeg($dst);
break;
case 3://PNG
header('Content-Type: image/png');
imagepng($dst);
break;
default:
break;
}
imagedestroy($dst);
imagedestroy($src);
?>
这里面主要是添加水印选项,如果选中添加水印则将1.jpg作为水印图片和原来的图片合并在一起。
二、使用GD2函数在照片上添加文字
PHP中的GD必须要以UTF_8编码格式来进行传递。
列如:
<?php
header("content-type:image/jpeg"); //定义输出为图像类型
$im=imagecreatefromjpeg("images/photo.jpg"); //载入照片
$textcolor=imagecolorallocate($im,56,73,136);//设置字体颜色为蓝色,值为RGB颜色值
$fnt="c:/windows/fonts/simhei.ttf"; //定义字体
$motto=iconv("gb2312","utf-8","加油 努力"); //定义输出字体串
imageTTFText($im,220,0,480,340,$textcolor,$fnt,$motto); //写TTF文字到图中
imagejpeg($im); //建立JPEG图形
imagedestroy($im); //结束图形,释放内存空间
?>
在上面的代码中,主要使用imageTTFText()函数输出文字到照片中。通过这种方法也可以实现用户登陆界面 。