PHP 生成水印图片
这段时间因工作需要,学习了下用PHP来给背景图上添加公司logo,宣传语之类的图片合并功能。话不多说,直接上代码。
<?php public function getImage()
{
$data_set = array(
"text" => "这是测试文字"
); // 项目根目录
$path = __DIR__."/路径/"; // 背景图
$tem_bg = __DIR__."/项目存放背景图得路径/背景图.jpg"; // 最终合成图片得路径和名字
$final_path = "/img/.../...之类得路径/用户ID-bg.jpg"; $bg = $path.$final_path;
copy($tem_bg, $bg); // 复制图片
list($bgWidth, $bgHeight) = getimagesize($bg); // 获取图片宽高 // 字体文件路径
$path_src = __DIR__."/字体文件路径"; // 如果是需要生成文字图片,则调用下面方法,生成文字图片,再合成
$text_image = $this->text_image_png($path, $path_src, $data_set); // 获取文字图片得宽高
list($textWidth, $textHeight) = getimagesize($text_image); // 将背景图和文字图合成
$this->synthetic($bg, 'jpg', $bgWidth, $bgHeight, $text_image, $textHeight, $textWidth, "bottom-center", 0, 210);
unlink($text_image); // 删除临时合成得文字图片 // 如果不需要生成文字图片,直接使用现有的图片,则执行下面的,这2种方式可共存
$image = __DIR__."/图片路径/xxx.png";
list($logoWidth, $logoHeight) = getimagesize($image);
// 将背景图和文字图合成
$this->synthetic($bg, 'jpg', $bgWidth, $bgHeight, $image, $logoHeight, $logoWidth, "bottom-center", 0, 210, 0, 10); // 如果需要合成多个图片,那就执行多次$this->synthetic()方法 return array("width" => $bgWidth, "height" => $bgHeight, "path" => $bg_name); // path就是合成后图片的路径
} /**
* 生成文字图片
* @param $path 项目根目录
* @param $font_path 字体文件路径
* @param $data_set 需要生成文字图片的文字
* @return string
*/
public function text_image_png($path, $font_path, $data_set)
{
$bgwidth = 640; // 文字图片的宽
$bgheight = 500; // 文字图片的高
$block = imagecreatetruecolor($bgwidth, $bgheight);//建立一个画板
$bg = imagecolorallocatealpha($block , 255 , 255 , 255 , 127);// 拾取一个完全透明的颜色,不要用imagecolorallocate拾色
$color = imagecolorallocate($block,137,54,20); //字体拾色
imagealphablending($block , false);//关闭混合模式,以便透明颜色能覆盖原画板
imagefill($block , 0 , 0 , $bg);//填充
$font_file = $font_path.'/fonts/simhei.ttf'; // 字体文件 $color = imagecolorallocate($block,255,228,116); //字体拾色 // 文字
$text = $data_set["text"];
imagettftext($block,50,0,260,150,$color,$font_file,$text); // 向图像写入文本 imagesavealpha($block , true);//设置保存png时保留透明通道信息
$image_path = $path."/临时文件路径/text.png";
imagepng($block, $image_path);//生成图片
imagedestroy($block); // 销毁画板 return $image_path;
} /**
* 合并图片
*
* @param $image // 背景图
* @param $mimeType // 合成后的图片类型
* @param $imgWidth // 背景的宽
* @param $imgHeight // 背景的高
* @param $watermark // 需要合成的图
* @param $watermarkHeight // 需要合成的高
* @param $watermarkWidth // 需要合成的宽
* @param string $position // 居中类型
* @param int $mt // 设置margin-top值
* @param int $mb // 设置margin-bottom值
* @param int $ml // 设置margin-left值
* @param int $mr // 设置margin-right值
* @param string $watermakMimeType // 合成后图片的类型
* @throws \Exception
*/
public function synthetic($image, $mimeType, $imgWidth, $imgHeight, $watermark, $watermarkHeight, $watermarkWidth, $position = "center", $mt = 0, $mb = 0, $ml = 0, $mr = 0, $watermakMimeType = "png")
{
// Calculate the watermark position
switch ($position) {
case "center":
$marginBottom = round($imgHeight / 2);
$marginRight = round($imgWidth / 2) - round($watermarkWidth / 2);
break; case "top-left":
$marginBottom = round($imgHeight - $watermarkHeight);
$marginRight = round($imgWidth - $watermarkWidth);
break; case "bottom-left":
$marginBottom = 5;
$marginRight = round($imgWidth - $watermarkWidth);
break; case "top-right":
$marginBottom = round($imgHeight - $watermarkHeight);
$marginRight = 5;
break; case "bottom-center":
$marginBottom = $mb;
$marginRight = round($imgWidth / 2) - round($watermarkWidth / 2) + $mr;
break; default:
$marginBottom = 2;
$marginRight = 2;
break;
} if($watermakMimeType == "png") {
$watermark = imagecreatefrompng($watermark); // 生成png图片
} else {
$watermark = imagecreatefromjpeg($watermark); // 生成jpg图片
} switch ($mimeType) {
case "jpeg":
case "jpg":
$createImage = imagecreatefromjpeg($image);
break; case "png":
$createImage = imagecreatefrompng($image);
break; case "gif":
$createImage = imagecreatefromgif($image);
break; default:
$createImage = imagecreatefromjpeg($image);
break;
} $sx = imagesx($watermark);
$sy = imagesy($watermark);
imagecopy(
$createImage,
$watermark,
imagesx($createImage) - $sx - $marginRight,
imagesy($createImage) - $sy - $marginBottom,
0,
0,
imagesx($watermark),
imagesy($watermark)
); // 拷贝图像的一部分 switch ($mimeType) {
case "jpeg":
case "jpg":
imagejpeg($createImage, $image); // 以 jpeg 格式将图像输出到浏览器或文件
break; case "png":
imagepng($createImage, $image); // 以 PNG 格式将图像输出到浏览器或文件
break; case "gif":
imagegif($createImage, $image); // 以 gif 格式将图像输出到浏览器或文件
break; default:
throw new \Exception("A watermark can only be applied to: jpeg, jpg, gif, png images ");
break;
}
}
以上就是将多个图片合成一张图片的过程。
PHP 生成水印图片的更多相关文章
- PHP生成随机水印图片
基于PHP的GD图形库,自己生成一张图片.仅限初识GD库,实例学习. 一.需求 网站的布局用到了类似慕课网课程列表的风格,每一个课程是一个banner图,图下面是标题加简介.因为课程的数量较大没有为所 ...
- C#(.net)水印图片的生成
/* * * 使用说明: * 建议先定义一个WaterImage实例 * 然后利用实例的属性,去匹配需要进行操作的参数 * 然后定义一个WaterImageManage实例 * 利用WaterI ...
- PHP 上传图片,生成水印,支持文字, gif, png
//admin_upfile.php <html> <meta http-equiv="Content-Type" content="text/html ...
- 前端水印图片及文字js教程
前端水印图片文字教程如下,复制代码修改图片地址即可看到效果,工作中遇到总结的,喜欢就关注下哦: <!DOCTYPE html><html> <head> <m ...
- PHP验证码生成及图片处理(GD库)
GD库是php处理图形的扩展库,GD库提供了一系列用来处理图片的API,使用GD库可以处理图片,或者生成图片,也可以给图片加水印. 本章实现了生成图片并绘画各种形状.图片的压缩.中文字符水印及图片水印 ...
- php生成文字图片效果
php生成文字图片效果最近看到php的GD功能,试着做了一个基本生成文字图片效果的代码: 显示文字图片页面:demo.php<?php$str = $_REQUEST['str'] ? $_RE ...
- [转载]java操作word生成水印
应用场景 为了保护版权或辨别文件的真伪,有时需要在生成的Word文件中动态添加水印,PageOffice组件的WaterMark类就封装了给在线编辑的Word文件添加水印这一功能,调用接口非常简单. ...
- [原创]java操作word生成水印
应用场景 为了保护版权或辨别文件的真伪,有时需要在生成的Word文件中动态添加水印,PageOffice组件的WaterMark类就封装了给在线编辑的Word文件添加水印这一功能,调用接口非常简单. ...
- php课程 8-28 php如何绘制生成显示图片
php课程 8-28 php如何绘制生成显示图片 一.总结 一句话总结:gd库轻松解决 1.php图片操作生成的图的两种去向是什么? 一种在页面直接输出,一种存进本地磁盘 2.php操作图片的库有哪些 ...
随机推荐
- python之路--小数据池,再谈编码,is和 == 的区别
一 . 小数据池 # 小数据池针对的是: int, str, bool 在py文件中几乎所有的字符串都会缓存. # id() 查看变量的内存地址 s = 'attila' print(id(s)) 二 ...
- css多列居中
https://jingyan.baidu.com/article/36d6ed1f67d58f1bcf488393.html
- 转载 -- jquery easyui datagrid 动态表头 + 嵌套对象属性展示
代码功能: 1.datagrid 的表头由后台生成,可以配置在数据库 2.datagrid 的列绑定数据 支撑嵌套对象 $(function() { var columns = new Array() ...
- Python——FTP上传和下载
一.FTP对象方法说明 login(user='anonymous',passwd='', acct='') 登录 FTP 服务器,所有参数都是可选的 pwd() 获得当前工作目录 cwd(path) ...
- Error:Failed to resolve: com.android.support:appcompat-v7
repositories { maven { url "https://maven.google.com" } } implementation 'com.android.supp ...
- vhdl——type
TYPE 数据类型名 IS 数据类型定义 OF 基本数据类型 TYPE 数据类型名 IS 数据类型定义 常用的用户自定义的数据类型有枚举型,数组型,记录型.其中枚举型的在状态机的描述中经常使用到 ,数 ...
- 本科理工男如何学习Linux
我是一个本科学电子的理工男,但是一直对计算机感兴趣,所以平时自己在课下喜欢学一些与计算机有关的东西.由于对计算机感兴趣,所以后来我参加了学校的计算机社团,在那里接受一些培训和指导.当时在社团里看到师兄 ...
- github-share报错无法读取远程仓库
报错:github Could not read from remote repository 1.github创建仓库成功,而push报告此错误 2.考虑远程仓库名与本地项目名/文件夹名不匹配 3. ...
- 洛谷P1402 酒店之王
传送门:>Here< 题意:有N个人去酒店,酒店共有P个房间,Q道菜.已知每个人喜欢特定的几个房间和几道菜,一个人是满意的当且仅当住了喜欢的房间,吃了喜欢的菜(一个人只能选一个房间一道菜) ...
- SCOI2016幸运数字(树剖/倍增/点分治+线性基)
题目链接 loj luogu 题意 求树上路径最大点权异或和 自然想到(维护树上路径)+ (维护最大异或和) 那么有三种方法可以选择 1.树剖+线性基 2.倍增+线性基 3.点分治+线性基 至于线性基 ...