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 ...
随机推荐
- python:进程操作
一.多进程应用 import socket from multiprocessing import Process def talk(conn): conn.send(b'connected') re ...
- HDU 1077 Catching Fish(用单位圆尽可能围住多的点)
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1077 Catching Fish Time Limit: 10000/5000 MS (Java/Oth ...
- data-ng-hide指令
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...
- Extjs treePanel 的treestore重复加载问题解决
在Extjs 4.2.2 中构建一个treePanel 发现设置rootVisible后 ,treeStore中设置的autoLoad:false不启作用,在组件初始化的时候即加载数据源,造成数据重复 ...
- Alert Log删除
标题:Renaming or Deleting the Alert Log While an Oracle Instance is Up & Running (文档 ID 74966.1) Q ...
- oracle-02 用户管理
一.创建用户概述:在oracle中要创建一个新的用户使用create user语句,一般是具有dba(数据库管理员)的权限才能使用.create user 用户名 identified by 密码; ...
- Struts2后期(这框架目前正处于淘汰状态)
Struts2第三天 课程回顾:Struts2框架的第二天 1. Servlet的API * ActionContext对象 * ServletActionContext对象 2. 结构类型的跳转 * ...
- Windows无法安装到这个磁盘 选中的磁盘具有MBR分区表解决方法
在安装 win10的时候,会出现这种提示:Windows 无法安装到这个磁盘.选中的磁 盘具有 MBR 分区表.在 EFI 系统上, Windows 只能安装到 GPT 磁盘.出现这种 情况主要是因为 ...
- PTA 最多删除3个字符(DP) - 30分
给定一个全部由小写英文字母组成的字符串,允许你至多删掉其中 3 个字符,结果可能有多少种不同的字符串? 输入格式: 输入在一行中给出全部由小写英文字母组成的.长度在区间 [4, 1] 内的字符串. 输 ...
- leetcode笔记(五)809. Expressive Words
题目描述 Sometimes people repeat letters to represent extra feeling, such as "hello" -> &qu ...