图片加文字水印

今天完成的是给图片加文字水印,需求是这个样子的,页面上有一个搜索,搜索出来的信息和一张模板图结合成一张新的图片出现在页面上面,这张图片要能让用户自己保存下来,这个其实就是一个加水印的过程,要是TP写的话,手册里面给出方法的,然后我这边是原生过程化写的,所以用的就是GD库加水印。

1.打开GD库,不一定每个人的GD库都是开着的。


这边打开GD库,之后重启wamp

2.添加文字水印


代码:

// 加载要加水印的图像
    $im = imagecreatefromjpeg('D:\wwwroot\aikesijia\wwwroot\logo.jpg');
    //定义字体颜色
    $color=imagecolorallocate($im,0,0,0);
    $color4=imagecolorallocate($im,255,0,0);
    //获取水印文字
    $t1 = mb_convert_encoding($data[0]['name'], "html-entities","utf-8" );
    $t2 = mb_convert_encoding($data[0]['wechat'], "html-entities","utf-8" );
    $t3 = mb_convert_encoding($data[0]['level'], "html-entities","utf-8" );
    $t4 = mb_convert_encoding($data[0]['join_time'], "html-entities","utf-8" );
    //获取文字字体
    $font = './simhei.ttf';
    //设置文字大
    $fontSize = 40;
    $fontSize2 = 50;
    $fontSize4 = 30;
    //获取图片A的宽高
    list($width, $height, $type, $attr) = getimagesize('D:\wwwroot\aikesijia\wwwroot\logo.jpg');
    //定义文字X
    $x=385;
    $x2=455;
    $x4=865;
    //定义文字Y
    $fontY = 265;
    imagettftext($im,$fontSize,0,$x,715,$color,$font,$t1);
    imagettftext($im,$fontSize2,0,$x2,1055,$color,$font,$t2);
    imagettftext($im,$fontSize2,0,$x2,1155,$color,$font,$t3);
    imagettftext($im,$fontSize4,0,$x4,1355,$color4,$font,$t4);
    $dest=imagecreatetruecolor($width,$height);
    imagecopy($dest,$im,0,0,0,0,$width,$height);
    ImageJPEG($im,"D:\wwwroot\aikesijia\wwwroot\uploads\{$data_wechat}.jpeg");
    imagedestroy($im) ;


这里可以自己控制文字的位置大小,还有文字的字体,这个只要加载字体文件就可以。

加文字水印应该就这样。

          更多源码,请访问 http://www.erdangjiade.com/source


          网页特效下载:http://www.erdangjiade.com/js


 更多PHP/Mysql功能:http://www.erdangjiade.com/php


          更多原创模板,尽在 http://www.erdangjiade.com/templates  

     
   PHP网站开发求职QQ群 368848856

Java可以使用Java2D API来实现图片文字水印。下面是一个简单的示例代码: ```java import java.awt.Color; import java.awt.Font; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; public class ImageWatermark { public static void main(String[] args) throws IOException { // 读取原始图片 BufferedImage image = ImageIO.read(new File("original.jpg")); // 创建一个空白的图片,大小和原始图片一样 BufferedImage result = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB); // 获取图片的Graphics2D对象 Graphics2D g = result.createGraphics(); // 将原始图片绘制到空白图片上 g.drawImage(image, 0, 0, null); // 设置水印文字的相关属性 String text = "Hello, world!"; Font font = new Font("Arial", Font.BOLD, 36); Color color = Color.WHITE; // 绘制水印文字 g.setFont(font); g.setColor(color); int x = (image.getWidth() - g.getFontMetrics().stringWidth(text)) / 2; int y = image.getHeight() / 2; g.drawString(text, x, y); // 保存水印图片 ImageIO.write(result, "jpg", new File("watermark.jpg")); } } ``` 在这个示例代码中,我们首先读取原始图片,然后创建一个空白的图片,大小和原始图片一样。接着,我们获取空白图片的Graphics2D对象,将原始图片绘制到空白图片上。然后,我们设置水印文字的相关属性,包括文字内容、字体和颜色,并在空白图片上绘制水印文字。最后,我们将水印图片保存到文件中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值