PHP图片水印

来源:互联网 发布:键盘宏定义软件 编辑:程序博客网 时间:2024/05/18 03:23

利用PHP中的GD库:

一.字体水印

<?php//打开图片//1.配置图片路径$src = './images/jing2.jpg';//2.获取图片信息(通过GD库提供的方法,得到你想要处理的图片的基本信息)$info = getimagesize($src);//3.通过图像的编号来获取图像的类型$type = image_type_to_extension($info[2],false);//4.在内存中创建一个和我们图像类型一样的图像$fun = "imagecreatefrom{$type}";//5.把图片复制到我们的内存中$image = $fun($src);//操作图片//1.设置字体的路径$font = './images/STCAIYUN.TTF';//2.填写我们的水印内容$content = "OUR 200 DAYS HAPPY!";//3.设置字体的颜色rgb和透明度,参数1内存中的图片$color = imagecolorallocatealpha($image, 0, 0, 0,80);//4.写入文字imagettftext($image, 200, -65, 150, 150, $color, $font, $content);//输出图片//浏览器输出header('Content-type:'.$info['mime']);$func = "image{$type}";$func($image);//保存图片$func($image,'fontimage'.rand().".".$type);//销毁图片,从内存中把图片销毁imagedestroy($image);

二.图片水印

<?php//打开图片//1.配置图片路径$src = './images/jing1.jpg';//2.获取图片信息(通过GD库提供的方法,得到你想要处理的图片的基本信息)$info = getimagesize($src);//3.通过图像的编号来获取图像的类型$type = image_type_to_extension($info[2],false);//4.在内存中创建一个和我们图像类型一样的图像$fun = "imagecreatefrom{$type}";//5.把图片复制到我们的内存中$image = $fun($src);//操作图片//1.设置水印的路径$src2 = './images/z.png';//2.填写我们的水印基本信息$info2 = getimagesize($src2);//3.通过水印图像的编号来获取水印的图片类型$type2 = image_type_to_extension($info2[2],false);//4.在内存中创建一个和我们图像类型一样的图像$fun2 = "imagecreatefrom{$type2}";//5.把图片复制到我们的内存中$water = $fun2($src2);//6.合并图片imagecopymerge($image, $water, 100, 150, 0, 0, $info2[0], $info2[1],30);//7.销毁水印图片imagedestroy($water);//输出图片//浏览器输出header('Content-type:'.$info['mime']);$func = "image{$type}";$func($image);//保存图片$func($image,'imagewater'.rand().".".$type);//销毁图片,从内存中把图片销毁imagedestroy($image);


三.图片缩略图

<?php//打开图片//1.配置图片路径$src = './images/jing1.jpg';//2.获取图片信息(通过GD库提供的方法,得到你想要处理的图片的基本信息)$info = getimagesize($src);//3.通过图像的变好来获取图像的类型$type = image_type_to_extension($info[2],false);//4.在内存中创建一个和我们图像类型一样的图像$fun = "imagecreatefrom{$type}";//5.把图片复制到我们的内存中$image = $fun($src);//操作图片//1.在内存中建立一个真色彩的图片300*200$image_thumb = imagecreatetruecolor(300, 200);//2.核心步骤将原图的复制到新建的真色彩图片上,按照一定的比例压缩imagecopyresampled($image_thumb, $image, 0, 0, 0, 0, 300, 200, $info[0], $info[1]);//3.销毁原图imagedestroy($image);//输出图片//浏览器输出header('Content-type:'.$info['mime']);$func = "image{$type}";$func($image_thumb);//保存图片$func($image_thumb,'newimage'.rand().".".$type);//销毁图片,从内存中把图片销毁imagedestroy($image_thumb);

原创粉丝点击