PHP生成图片水印和文字水印

开发 后端
之前很多读者发邮件问我如何使用PHP生成水印,今天我就来给大家讲解一下。本篇PHP教程使用了两个函数来生成水印:watermark_text()和watermark_image()。

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

文本水印

我们使用函数watermark_text()来生成文本水印,你必须先指定字体源文件、字体大小和字体文本,具体代码如下:

  1. $font_path = "GILSANUB.TTF"// Font file  
  2. $font_size = 30; // in pixcels  
  3. $water_mark_text_2 = "phpfuns"// Watermark Text  
  4. function watermark_text($oldimage_name$new_image_name)  
  5. {  
  6. global $font_path$font_size$water_mark_text_2;  
  7. list($owidth,$oheight) = getimagesize($oldimage_name);  
  8. $width = $height = 300;  
  9. $image = imagecreatetruecolor($width$height);  
  10. $image_src = imagecreatefromjpeg($oldimage_name);  
  11. imagecopyresampled($image$image_src, 0, 0, 0, 0, $width$height$owidth$oheight);  
  12. $blue = imagecolorallocate($image, 79, 166, 185);  
  13. imagettftext($image$font_size, 0, 68, 190, $blue$font_path$water_mark_text_2);  
  14. imagejpeg($image$new_image_name, 100);  
  15. imagedestroy($image);  
  16. unlink($oldimage_name);  
  17. return true;  
  18. }  

可以在这里查看demo

图片水印

我们使用函数watermark_image()来生成图片水印,你必须先水银图片的源文件。具体代码如下:

  1. $image_path = "phpfuns.png";  
  2. function watermark_image($oldimage_name$new_image_name)  
  3. /{  
  4. global $image_path;  
  5. list($owidth,$oheight) = getimagesize($oldimage_name);  
  6. $width = $height = 300;  
  7. $im = imagecreatetruecolor($width$height);  
  8. $img_src = imagecreatefromjpeg($oldimage_name);  
  9. imagecopyresampled($im$img_src, 0, 0, 0, 0, $width$height$owidth$oheight);  
  10. $watermark = imagecreatefrompng($image_path);  
  11. list($w_width$w_height) = getimagesize($image_path);  
  12. $pos_x = $width - $w_width;  
  13. $pos_y = $height - $w_height;  
  14. imagecopy($im$watermark$pos_x$pos_y, 0, 0, $w_width$w_height);  
  15. imagejpeg($im$new_image_name, 100);  
  16. imagedestroy($im);  
  17. unlink($oldimage_name);  
  18. return true;  
  19. }  

你可以在这里查看demo

上传图片表单

我们使用下面的表单来上传图片:

  1. <?php  
  2. $demo_image"";  
  3. if(isset($_POST['createmark']) and $_POST['createmark'] == "Submit")  
  4. {  
  5. $path = "uploads/";  
  6. $valid_formats = array("jpg""bmp","jpeg");  
  7. $name = $_FILES['imgfile']['name'];  
  8. if(strlen($name))  
  9. {  
  10. list($txt$ext) = explode("."$name);  
  11. if(in_array($ext,$valid_formats) && $_FILES['imgfile']['size'] <= 256*1024)  
  12. {  
  13. $upload_status = move_uploaded_file($_FILES['imgfile']['tmp_name'], $path.$_FILES['imgfile']['name']);  
  14. if($upload_status){  
  15. $new_name = $path.time().".jpg";  
  16. // Here you have to user functins watermark_text or watermark_image  
  17. if(watermark_text($path.$_FILES['imgfile']['name'], $new_name))  
  18. $demo_image = $new_name;  
  19. }  
  20. }  
  21. else 
  22. $msg="File size Max 256 KB or Invalid file format.";  
  23. }  
  24. }  
  25. ?>  
  26. // HTML Code  
  27. <form name="imageUpload" method="post" enctype="multipart/form-data" >  
  28. Upload Image  
  29. Image :<input type="file" name="imgfile" /><br />  
  30. <input type="submit" name="createmark" value="Submit" />  
  31. <?php  
  32. if(!emptyempty($demo_image))  
  33. echo '<img src="'.$demo_image.'" />';  
  34. ?>  
  35. </form>  

实际上就是PHP的gd库,一切就是这么简单!

PHP图片水印和文字水印示例此处下载

原文链接:http://www.phpfuns.com/php/using-php-generate-watermark.shtml

【编辑推荐】

责任编辑:张伟 来源: phpFuns
相关推荐

2016-09-06 21:09:35

Phpgd库图片水印

2009-12-07 15:41:51

PHP图片加水印

2009-07-29 16:06:42

图片增加水印

2009-11-24 15:43:17

PHP给图片加水印

2009-10-27 10:00:18

VB.NET水印类

2009-08-12 17:19:51

ASP.NET图片加水

2021-06-28 11:29:50

生成水印插件

2023-09-27 12:11:08

Python水印Pillow

2009-12-08 10:50:12

PHP GD库实现中文

2022-03-14 09:39:06

PythonPDF图片

2021-07-01 15:25:32

前端水印代码

2023-08-30 13:24:00

AI工具

2015-07-17 10:07:33

JAVA数字水印

2018-09-17 14:06:46

UCloudUGC

2018-09-17 14:02:28

UCloudGUC

2018-09-17 13:52:39

UCloudUGC

2024-01-09 16:02:44

Python开源

2023-10-07 06:50:03

2022-05-04 18:26:25

PDF水印Python

2011-05-04 17:25:24

打印机
点赞
收藏

51CTO技术栈公众号