PHP给图片添加文字或图片水印的实现代码

简介:

有时上传图片时,需要给图片添加水印,水印一般为文字或图片logo水印,下面就来看看两种添加方法。

一、文字水印

文字水印就是在图片上加上文字,主要使用gd库的imagefttext方法,并且需要字体文件。效果图如下:

 
  1. $dst_path = 'dst.jpg'
  2. //创建图片的实例 
  3. $dst = imagecreatefromstring(file_get_contents($dst_path)); 
  4.  
  5. //打上文字 
  6. $font = './simsun.ttc';//字体路径 
  7.  
  8. $black = imagecolorallocate($dst, 0x00, 0x00, 0x00);//字体颜色 
  9. imagefttext($dst, 13, 0, 20, 20, $black, $font, '快乐编程'); 
  10. //输出图片 
  11. list($dst_w, $dst_h, $dst_type) = getimagesize($dst_path); 
  12. switch ($dst_type) { 
  13.     case 1://GIF 
  14.         header('Content-Type: image/gif'); 
  15.         imagegif($dst); 
  16.         break; 
  17.     case 2://JPG 
  18.         header('Content-Type: image/jpeg'); 
  19.         imagejpeg($dst); 
  20.         break; 
  21.     case 3://PNG 
  22.         header('Content-Type: image/png'); 
  23.         imagepng($dst); 
  24.         break; 
  25.     default
  26.         break; 
  27. imagedestroy($dst);  

新创建一张图片,然后打印文字水印:

 
  1. // imagecreatefromstring 
  2. // imageCreateFromPng  Create a new image from file or URL   创建图片对象 
  3.  
  4. // Create a 300x100 image,新创建一张图片 
  5. $im = imagecreatetruecolor(500, 300); 
  6.  
  7. // set color 
  8. $red = imagecolorallocate($im, 0xFF, 0x00, 0x00); 
  9. $black = imagecolorallocate($im, 0x00, 0x00, 0x00); 
  10.  
  11. // Make the background red 
  12. // function imagefilledrectangle ($image, $x1, $y1, $x2, $y2, $color) {} 
  13. imagefilledrectangle($im, 0, 0, 300, 100, $red); 
  14.  
  15. // Path to our ttf font file 
  16. $font_file = './font/Arial.ttf'
  17.  
  18.  
  19. // imagefttext ($image, $size, $angle, $x, $y, $color, $fontfile, $text, $extrainfo = null ) 
  20. // Draw the text 'PHP Manual' using font size 13 
  21. imagefttext($im, 13, 0, 150, 50, $black, $font_file, 'PHP Manual'); 
  22.  
  23. // Output image to the browser 
  24. header('Content-Type: image/png'); 
  25.  
  26. imagepng($im); 
  27. imagedestroy($im); 

二、图片水印

图片水印就是将一张图片加在另外一张图片上,主要使用gd库的imagecopy和imagecopymerge。

 
  1. $dst_path = 'myimage.jpg'
  2. $src_path = 'http://www.logodashi.com/FileUpLoad/inspiration/636003768803214440.jpg'
  3. //创建图片的实例 
  4. $dst = imagecreatefromstring(file_get_contents($dst_path)); 
  5. $src = imagecreatefromstring(file_get_contents($src_path)); 
  6. //获取水印图片的宽高 
  7. list($src_w, $src_h) = getimagesize($src_path); 
  8. //将水印图片复制到目标图片上,最后个参数50是设置透明度,这里实现半透明效果 
  9. imagecopymerge($dst, $src, 10, 10, 0, 0, $src_w, $src_h, 30); 
  10.  
  11. //如果水印图片本身带透明色,则使用imagecopy方法 
  12. // imagecopy($dst, $src, 10, 10, 0, 0, $src_w, $src_h); 
  13.  
  14. //输出图片 
  15. list($dst_w, $dst_h, $dst_type) = getimagesize($dst_path); 
  16. switch ($dst_type) { 
  17.     case 1://GIF 
  18.         header('Content-Type: image/gif'); 
  19.         imagegif($dst); 
  20.         break; 
  21.     case 2://JPG 
  22.         header('Content-Type: image/jpeg'); 
  23.         imagejpeg($dst); 
  24.         break; 
  25.     case 3://PNG 
  26.         header('Content-Type: image/png'); 
  27.         imagepng($dst); 
  28.         break; 
  29.     default
  30.         break; 
  31. imagedestroy($dst); 
  32. imagedestroy($src);  

效果图:

三、其他有关图像处理的函数

 
  1. /* 
  2.  *返回图像的大小及图像类型 
  3. // Get the size of an image 
  4. $size = getimagesize("http://image18-c.poco.cn/mypoco/myphoto/20160901/20/17857099520160901203311082.jpg?750x956_120"); 
  5.  
  6. print_r($size); 
  7.  
  8.  
  9. // 打印结果 
  10. Array 
  11.     [0] => 750 
  12.     [1] => 956 
  13.     [2] => 2 
  14.     [3] => width="750" height="956" 
  15.     [bits] => 8 
  16.     [channels] => 3 
  17.     [mime] => image/jpeg 
  18.  
  19. */ 
  20.  
  21.  
  22. /** 
  23.  * imagecopy — 拷贝图像的一部分 
  24.  */ 
  25.  
  26. // bool imagecopy ( resource $dst_im , resource $src_im , int $dst_x , int $dst_y , int $src_x , int $src_y , int $src_w , int $src_h ) 
  27. // 将 src_im 图像中坐标从 src_x,src_y 开始,宽度为 src_w,高度为 src_h 的一部分拷贝到 dst_im 图像中坐标为 dst_x 和 dst_y 的位置上。 
  28.  
  29.  
  30.  
  31. /* 
  32.  * 
  33.  * http://php.net/manual/zh/function.imagecopymerge.php 
  34.  *imagecopymerge — 拷贝并合并图像的一部分 
  35.  *bool imagecopymerge ( resource $dst_im , resource $src_im , int $dst_x , int $dst_y , int $src_x , int $src_y , int $src_w , int $src_h , int $pct ) 
  36.  * 
  37.  * 
  38.  */  



作者:Corwien

来源:51CTO

相关文章
|
1月前
|
PHP 开发者
PHP中的命名空间:优雅解决代码冲突与组织结构
在PHP开发中,命名空间是一种重要的工具,可以帮助开发者避免代码冲突、提高代码组织结构和可读性。本文将深入探讨PHP中命名空间的概念、使用方法以及实际应用场景,帮助读者更好地理解和运用命名空间来优化他们的PHP代码。
拿php写个原生增删改查案例出来(提供全部代码+sql)
拿php写个原生增删改查案例出来(提供全部代码+sql)
拿php写个原生增删改查案例出来(提供全部代码+sql)
|
7天前
|
PHP 数据库
DIY私人图床:使用CFimagehost源码自建无需数据库支持的PHP图片托管服务-2
DIY私人图床:使用CFimagehost源码自建无需数据库支持的PHP图片托管服务
|
7天前
|
存储 PHP Apache
DIY私人图床:使用CFimagehost源码自建无需数据库支持的PHP图片托管服务-1
DIY私人图床:使用CFimagehost源码自建无需数据库支持的PHP图片托管服务
|
12天前
|
数据采集 机器学习/深度学习 存储
图片大搜罗:PHP下载器带你畅游Twitter图像海洋
构建Twitter图像下载器,使用PHP模拟请求抓取图像,通过代理IP规避限制。示例代码展示如何设置代理、用户代理和Cookie,解析HTML提取图像链接并下载。结合机器学习与元数据分析,可洞察用户行为和社会趋势。代理服务器信息及Twitter URL需自行替换。
图片大搜罗:PHP下载器带你畅游Twitter图像海洋
|
28天前
|
安全 PHP 开发工具
php代码加密 php-screw-plus
php代码加密 php-screw-plus
21 0
|
29天前
|
算法 PHP 数据安全/隐私保护
【实战】PHP代码逆向工具,轻松还原goto加密语句的神器!
`goto解密工具`是一款针对PHP的在线神器,能有效解密和还原goto加密代码,提升代码可读性和可维护性。支持单文件及50M压缩包一键解密,提供全效解决方案。通过实际案例展示了解密报错和理解复杂代码的能力,是PHP开发者解决goto难题的得力助手。立即体验:[在线PHP解密大师](https://copy.kaidala.com/dala/goto/index.html)。
22 1
|
29天前
|
算法 PHP 数据安全/隐私保护
【必备工具】解密PHP超强在线工具,一键goto代码解密
goto解密工具,php开发者的福音,能有效解密复杂的goto加密代码,提升代码可读性和可维护性。支持单文件及项目目录一键解密,最大处理50M压缩文件。通过具体案例展示其在解决报错和理解复杂开源项目中的强大功能。立即体验:https://copy.kaidala.com/dala/goto/index.html
32 0
|
2月前
|
PHP 开发者 UED
PHP 中的异常处理:提高代码健壮性的关键
【2月更文挑战第28天】在 PHP 开发中,异常处理是确保应用程序稳定性和可靠性的重要环节。本文将深入探讨 PHP 异常的概念、类型及其处理机制,并通过实例演示如何有效地捕获和处理异常,以增强代码的健壮性和用户体验。