php 使用GD库压缩图片,添加文字图片水印
先上一个工具类,提供了压缩,添加文字、图片水印等方法:
image.class.php
<?php
class Image {
private $info;
private $image;
public function __construct($src) {
$info = getimagesize($src);
$this -> info = array(
"width" => $info[0],
"height" => $info[1],
"type" => image_type_to_extension($info[2], false),
"mime" => $info['mime']
);
$fun = "imagecreatefrom{$this->info['type']}";
$this -> image = $fun($src);
}
public function thumb($width, $height) {
$imageThumb = imagecreatetruecolor($width, $height);
imagecopyresampled(
$imageThumb,
$this -> image,
0,
0,
0,
0,
$width,
$height,
$this -> info["width"],
$this -> info["height"]);
imagedestroy($this -> image);
$this -> image = $imageThumb;
}
public function waterMarkText($text, $fontPath, $fontSize, $color, $x, $y, $angle) {
$color = imagecolorallocatealpha(
$this -> image,
$color[0],
$color[1],
$color[2],
$color[3]);
imagettftext(
$this -> image,
$fontSize,
$angle,
$x,
$y,
$color,
$fontPath,
$text);
}
public function waterMarkImage($source, $x, $y, $alpha) {
$markInfo = getimagesize($source);
//3、获取水印图片类型
$markType = image_type_to_extension($markInfo[2], false);
//4、在内存创建图像
$markCreateImageFunc = "imagecreatefrom{$markType}";
//5、把水印图片复制到内存中
$water = $markCreateImageFunc($source);
//特别处理,设置透明
$color=imagecolorallocate($water,255,255,255);
imagefill($water,0,0,$color);
imagecolortransparent($water,$color);
//6、合并图片
imagecopymerge($this -> image, $water, $x, $y, 0, 0, $markInfo[0], $markInfo[1], $alpha);
}
public function show() {
header("Content-type:" . $this -> info['mime']);
$outputfunc = "image{$this -> info['type']}";
$outputfunc($this -> image);
}
public function save($newname) {
$outputfunc = "image{$this -> info['type']}";
$outputfunc($this -> image, $newname . '.' . $this -> info['type']);
}
public function __destruct() {
imagedestroy($this -> image);
}
}
?>
调用:
<?php
require "image.class.php";
$src = "aeroplane.jpg";
$image = new Image($src);
$source = "logo.png";
$image -> waterMarkImage($source, 0, 0, 30);
$image -> thumb(500, 500);
$fontPath = "STXINGKA.ttf";
$text = "文字图片水印";
$image -> waterMarkText(
$text,
$fontPath,
60,
array(255, 255, 255, 20),
10,
240,
0);
$image -> show();
$image -> save("image_mark");
?>
上面用到的图片和字体都跟代码文件在同一个目录下。
效果:

php 使用GD库压缩图片,添加文字图片水印的更多相关文章
- ios图片添加文字或者水印
在项目中,我们会对图片做一些处理,但是我们要记住,一般在客户端做图片处理的数量不宜太多,因为受设备性能的限制,如果批量的处理图片,将会带来交互体验性上的一些问题.首先让我们来看看在图片上添加文字的方法 ...
- php 图片添加文字,水印
因为工作需求,用到这个,网上找了很多,也没有找到好的方式,最后找到这种感觉比较简单的方式,记录下来,以备后用. $im = imagecreatefrompng("img/yyk_bg. ...
- 用python给图片添加文字(水印)
题目来源于:Python 练习册,每天一个小程序 第0000题 代码如下: #-*- coding:utf-8 -*- import PIL from PIL import Image from PI ...
- php使用GD库实现图片水印和缩略图——给图片添加文字水印
今天呢,就来学习一下在php中使用PD库来实现对图片水印的文字水印方法,不需要PS哦! 首先,准备素材 (1)准备一张图片 (2)准备一张水印(最好是透明的,即背景是白色底) (3)准备一中字体(在电 ...
- php给图片添加文字水印方法汇总
在php中要给图片加水印我们需要给php安装GD库了,这里我们不介绍GD库安装,只介绍怎么利用php给图片添加文字水印的4种方法的汇总.有需要的小伙伴可以参考下. 1: 面向过程的编写方法 1 2 3 ...
- php图片添加文字水印方法汇总
方法一: <?php header("content-type:text/html;charset=utf-8"); //指定图片路径 $src = "img/a. ...
- php 图片添加文字水印 以及 图片合成(微信快码传播)
1.图片添加文字水印: $bigImgPath = 'backgroud.png'; $img = imagecreatefromstring(file_get_contents($bigImgPat ...
- R语言 如何为图片添加文字说明(转载)
转载:(中文翻译者)[http://blog.csdn.net/chen790646223/article/details/49766659] (原文链接)[http://datascienceplu ...
- 利用php给图片添加文字水印--面向对象与面向过程俩种方法的实现
1: 面向过程的编写方法 //指定图片路径 $src = '001.png'; //获取图片信息 $info = getimagesize($src); //获取图片扩展名 $type = image ...
随机推荐
- MySQL:数据库入门篇2
#移除主键时需要先解除递增,才能解除主键 alter table info modify id int null , drop PRIMARY key 一.用户权限 1.创建用户 create use ...
- HDU 6034 6038
6034:给每个字母26进制的贪心.例如一个字母 c = 7*26^89 + 6*26^50.... 这个字符串有10^5长度.普通的大整数会超时,这里要稀疏这个大数一个pair<int,int ...
- NEUACM1132: Renew MST Quickly 增量最小生成树
题目链接:http://acm.neu.edu.cn/hustoj/problem.php?id=1132 和UVa11354很类似 题意: 原先有一棵树,每次加一条边,看最小生成树大小: 这个和增量 ...
- 2018.10.5 hibernate导入约束,在Eclipse的xml文件实现自动提示
打开Java Resources/Libraries/hibernate-core-5.3.1.Final.jar/org.hibernate/hibernate-mapping-3.0.dtd(hi ...
- Spring多个版本源码地址分享
源码地址为:http://repo.spring.io/simple/libs-release-local/org/springframework/spring/,以供研究源码的朋友. 我看了好几本关 ...
- C# 利用HttpWebRequest进行HTTPS的post请求的示例
最近一个推送信息的目标接口从http格式换成https格式,原来的请求无法正常发送,所以修改了发送请求的方法.标红的代码是新加了,改了之后就可以正常访问(不检测证书的) public static s ...
- json sort
Array.sort()方法是用来对数组项进行排序的 ,默认情况下是进行升序排列.sort() 方法可以接受一个 方法为参数. sort()排序时每次比较两个数组项都回执行这个参数,并把两个比较的数组 ...
- Storm 中drpc调用
package storm.starter; import backtype.storm.Config; import backtype.storm.LocalCluster; import back ...
- 移动端flex自适应方案。(px to rem)
define(function (require, exports, module) { exports.mobileUtilMethod = function () { (function (e, ...
- 理解 ES6 Generator-next()方法
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...