利用php给图片添加文字水印--面向对象与面向过程俩种方法的实现
1: 面向过程的编写方法
//指定图片路径
$src = '001.png';
//获取图片信息
$info = getimagesize($src);
//获取图片扩展名
$type = image_type_to_extension($info[2],false);
//动态的把图片导入内存中
$fun = "imagecreatefrom{$type}";
$image = $fun('001.png');
//指定字体颜色
$col = imagecolorallocatealpha($image,255,255,255,50);
//指定字体内容
$content = 'helloworld';
//给图片添加文字
imagestring($image,5,20,30,$content,$col);
//指定输入类型
header('Content-type:'.$info['mime']);
//动态的输出图片到浏览器中
$func = "image{$type}";
$func($image);
//销毁图片
imagedestroy($image);
2:面向对象的实现方法
class Image_class {
private $image;
private $info;
/**
* @param $src:图片路径
* 加载图片到内存中
*/
function __construct($src){
$info = getimagesize($src);
$type = image_type_to_extension($info[2],false);
$this -> info =$info;
$this->info['type'] = $type;
$fun = "imagecreatefrom" .$type;
$this -> image = $fun($src);
}
/**
* @param $fontsize: 字体大小
* @param $x: 字体在图片中的x位置
* @param $y: 字体在图片中的y位置
* @param $color: 字体的颜色是一个包含rgba的数组
* @param $text: 想要添加的内容
* 操作内存中的图片,给图片添加文字水印
*/
public function fontMark($fontsize,$x,$y,$color,$text){
$col = imagecolorallocatealpha($this->image,$color[0],$color[1],$color[2],$color[3]);
imagestring($this->image,$fontsize,$x,$y,$text,$col);
}
/*
* 输出图片到浏览器中
*/
public function show(){
header('content-type:' . $this -> info['mime']);
$fun='image' . $this->info['type'];
$fun($this->image);
}
/**
* 销毁图片
*/
function __destruct(){
imagedestroy($this->image);
}
}
//对类的调用
$obj = new Image_class('001.png');
$obj->fontMark(20,20,30,array(255,255,255,60),'hello');
$obj->show();
利用php给图片添加文字水印--面向对象与面向过程俩种方法的实现的更多相关文章
- php给图片添加文字水印方法汇总
在php中要给图片加水印我们需要给php安装GD库了,这里我们不介绍GD库安装,只介绍怎么利用php给图片添加文字水印的4种方法的汇总.有需要的小伙伴可以参考下. 1: 面向过程的编写方法 1 2 3 ...
- php 图片添加文字水印 以及 图片合成(微信快码传播)
1.图片添加文字水印: $bigImgPath = 'backgroud.png'; $img = imagecreatefromstring(file_get_contents($bigImgPat ...
- Swift - 给图片添加文字水印(图片上写文字,并可设置位置和样式)
想要给图片添加文字水印或者注释,我们需要实现在UIImage上写字的功能. 1,效果图如下: (在图片左上角和右下角都添加了文字.) 2,为方便使用,我们通过扩展UIImage类来实现添加水印功能 ( ...
- C#图片添加文字水印
/// <summary> /// 给图片添加文字水印 /// </summary> /// <param name="img">图片</ ...
- php图片添加文字水印方法汇总
方法一: <?php header("content-type:text/html;charset=utf-8"); //指定图片路径 $src = "img/a. ...
- php使用GD库实现图片水印和缩略图——给图片添加文字水印
今天呢,就来学习一下在php中使用PD库来实现对图片水印的文字水印方法,不需要PS哦! 首先,准备素材 (1)准备一张图片 (2)准备一张水印(最好是透明的,即背景是白色底) (3)准备一中字体(在电 ...
- PHP给图片添加文字水印实例
PHP给图片添加文字水印实例,支持中文文字水印,是否覆盖原图,自定义设置水印背景色.文字颜色.字体等. 水印类water.class.php var $Path = "./"; / ...
- asp .net 为图片添加文字水印(内包含有加图片水印的方法) .
在项目中先创建一个Imag_writer 类库 在该类库下分别创建两个枚举类型WaterMarkType (水印的类型).WaterMarkPosition (水印的位置).代码如下: using S ...
- Android - 利用扩展函数为Bitmap添加文字水印
<异空间>项目技术分享系列--扩展函数为Bitmap添加文字水印 对图片Bitmap绘制文字水印还是比较常见的需求,毕竟版权意识都在增强(用户可以给自己图片加上用户名),还可以为用户提供更 ...
随机推荐
- Android 中HttpClient和HttpURLConnection选取
原文地址:http://android-developers.blogspot.com/2011/09/androids-http-clients.html 译文:http://yunfeng.sin ...
- [译]PyUnit—Python单元测试框架(1)
1. 原文及参考资料 原文链接:http://docs.python.org/2/library/unittest.html# 参考文档: http://pyunit.sourceforge.net/ ...
- sql server 修改表的默认值, 需要先删除约束条件
---------增加是否发布订单 if not exists(select 1 from syscolumns where name='iIsRelease' and id=OBJECT_ID('M ...
- 【CSS3】Advanced4:Advanced Colors
1.rgba(red,green,blue,alpha(不透明度0.0(完全透明)与 1.0(完全不透明)) 2.HSLa(hue(色调 0red 120green 240blue),saturati ...
- javascript对象几种创建方式
Javascript对象创建的几种方式 1.使用new运算符创建Object var box=new Object(); box.name='肖能武'; box.age=28; 2.ne ...
- 利用JDBC连接Eclipse和mySQL5.1.26数据库
初学JDBC,看了看书,自己动手的时候还是有很多地方有问题,最终终于解决了实现了数据库的连接.现将整个步骤描述如下: 环境:mySQL5.1.26(win 32bit), Eclipse JavaEE ...
- delphi 提取字符中的数字
Function Setstring(cString:string):string; {提取数字} VAr i:integer; str:string; begin str:='' ...
- 【Java基础】增强for循环要注意陷阱
什么是增强for循环 增强for循环是一种简单模式的for循环,为了方便数组和集合的遍历而存在. int[] arr = new int[]{1, 2, 3, 4, 5, 6}; for (int a ...
- ios get airplay name
tarting from iOS7 AudioToolbox API for currentRoute becomes deprecated: Apple instead made currentRo ...
- 两个栈实现一个队列,C语言实现,队列可伸缩,容纳任意数目的元素。
一.思路:1.创建两个空栈A和B:2.A栈作为队列的入口,B栈作为队列的出口:3.入队列操作:即是入栈A:4.出队列操作:若栈B为空,则将A栈内容出栈并压人B栈,再出 B栈:不为空就直接出栈: 二.代 ...