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 ...
随机推荐
- Codeforces Round #513
A. Phone Numbers 题意:给一些数字,每个电话号码以8开头,11位,求最多组成多少个号码,重复累加. #include <bits/stdc++.h> using names ...
- Uva 11468 AC自动机或运算
AC自动机 UVa 11468 题意:给一些字符和各自出现的概率,在其中随机选择L次,形成长度为L的字符串S,给定K个模板串,求S不包含任意一个串的概率. 首先介绍改良版的AC自动机: 传统的AC自动 ...
- [ZJOI2012]小蓝的好友
https://www.luogu.org/problemnew/show/P2611 题解 \(n\times m\)肯定过不去.. 我们把给定的点看做障碍点,考虑先补集转化为求全空矩阵. 然后我们 ...
- 2018.9.16 Redis 边学习边总结
Redis 是一个使用 C 语言写成的,开源的 key-value 数据库..和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合) ...
- 【洛谷P3834】(模板)可持久化线段树 1(主席树)
[模板]可持久化线段树 1(主席树) https://www.luogu.org/problemnew/show/P3834 主席树支持历史查询,空间复杂度为O(nlogn),需要动态开点 本题用一个 ...
- 【luogu P3390 矩阵快速幂】 模板
题目链接:https://www.luogu.org/problemnew/show/P3390 首先要明白矩阵乘法是什么 对于矩阵A m*p 与 B p*n 的矩阵 得到C m*n 的矩阵 矩阵 ...
- Mysql跨数据库(在同一IP地址中)复制表
数据库表间数据复制分类 在利用数据库开发时,常常会将一些表之间的数据互相导入.当然可以编写程序实现,但是,程序常常需要开发环境,不方便.最方便是利用sql语言直接导入.既方便而修改也简单.以下就是导入 ...
- HDU 1210 Eddy's 洗牌问题(找规律,数学)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1210 Eddy's 洗牌问题 Time Limit: 2000/1000 MS (Java/Other ...
- 使用RMAN对数据文件进行恢复
(1)备份数据库 在使用RMAN进行数据库恢复之前,先用RMAN进行全库备份 [oracle@redhat6 ~]$ rman target / Recovery Manager: Release : ...
- python使用sqlalchemy连接mysql数据库
环境:centos7+python2.7.5+sqlalchemy sqlalchemy是python当中比较出名的orm程序.在python中,使用sqlalchemy连接mysql数据库进行操作非 ...