之前很多读者发邮件问我如何使用PHP生成水印,今天我就来给大家讲解一下。本篇PHP教程使用了两个函数来生成水印:watermark_text()和watermark_image()。你可以将本篇教程的示例整合到你的WEB项目中,比如上传图片的版权水印。

文本水印
我们使用函数watermark_text()来生成文本水印,你必须先指定字体源文件、字体大小和字体文本,具体代码如下:
- $font_path = "GILSANUB.TTF"; // Font file
 - $font_size = 30; // in pixcels
 - $water_mark_text_2 = "phpfuns"; // Watermark Text
 - function watermark_text($oldimage_name, $new_image_name)
 - {
 - global $font_path, $font_size, $water_mark_text_2;
 - list($owidth,$oheight) = getimagesize($oldimage_name);
 - $width = $height = 300;
 - $image = imagecreatetruecolor($width, $height);
 - $image_src = imagecreatefromjpeg($oldimage_name);
 - imagecopyresampled($image, $image_src, 0, 0, 0, 0, $width, $height, $owidth, $oheight);
 - $blue = imagecolorallocate($image, 79, 166, 185);
 - imagettftext($image, $font_size, 0, 68, 190, $blue, $font_path, $water_mark_text_2);
 - imagejpeg($image, $new_image_name, 100);
 - imagedestroy($image);
 - unlink($oldimage_name);
 - return true;
 - }
 
可以在这里查看demo。
图片水印
我们使用函数watermark_image()来生成图片水印,你必须先水银图片的源文件。具体代码如下:
- $image_path = "phpfuns.png";
 - function watermark_image($oldimage_name, $new_image_name)
 - /{
 - global $image_path;
 - list($owidth,$oheight) = getimagesize($oldimage_name);
 - $width = $height = 300;
 - $im = imagecreatetruecolor($width, $height);
 - $img_src = imagecreatefromjpeg($oldimage_name);
 - imagecopyresampled($im, $img_src, 0, 0, 0, 0, $width, $height, $owidth, $oheight);
 - $watermark = imagecreatefrompng($image_path);
 - list($w_width, $w_height) = getimagesize($image_path);
 - $pos_x = $width - $w_width;
 - $pos_y = $height - $w_height;
 - imagecopy($im, $watermark, $pos_x, $pos_y, 0, 0, $w_width, $w_height);
 - imagejpeg($im, $new_image_name, 100);
 - imagedestroy($im);
 - unlink($oldimage_name);
 - return true;
 - }
 
你可以在这里查看demo。
上传图片表单
我们使用下面的表单来上传图片:
- $demo_image= "";
 - if(isset($_POST['createmark']) and $_POST['createmark'] == "Submit")
 - {
 - $path = "uploads/";
 - $valid_formats = array("jpg", "bmp","jpeg");
 - $name = $_FILES['imgfile']['name'];
 - if(strlen($name))
 - {
 - list($txt, $ext) = explode(".", $name);
 - if(in_array($ext,$valid_formats) && $_FILES['imgfile']['size'] <= 256*1024)
 - {
 - $upload_status = move_uploaded_file($_FILES['imgfile']['tmp_name'], $path.$_FILES['imgfile']['name']);
 - if($upload_status){
 - $new_name = $path.time().".jpg";
 - // Here you have to user functins watermark_text or watermark_image
 - if(watermark_text($path.$_FILES['imgfile']['name'], $new_name))
 - $demo_image = $new_name;
 - }
 - }
 - else
 - $msg="File size Max 256 KB or Invalid file format.";
 - }
 - }
 - ?>
 - // HTML Code
 - Upload Image
 - Image :
 
- if(!emptyempty($demo_image))
 - echo '
 ';
- ?>
 
实际上就是PHP的gd库,一切就是这么简单!
此处下载
                文章名称:PHP生成图片水印和文字水印
                
                链接URL:http://www.csdahua.cn/qtweb/news13/332813.html
            
网站建设、网络推广公司-快上网,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 快上网