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 ...
随机推荐
- PowerShell实现英汉互译_并保存
代码如下:(介于着色 代码在文末下载) 功能简介: 自动识别英汉输入 返回结果 对于词数小于20的会保存在当前目录下temp_table.txt词文件 大于20的会被识别为句子进行互译 不会存于词文件 ...
- 2017.9.1 Java中的程序方法
今日内容介绍 1.方法基础知识 2.方法高级内容 3.方法案例 01方法的概述 * A: 为什么要有方法 * 提高代码的复用性 * B: 什么是方法 * 完成特定功能的代码块. 02方法的定义格式 * ...
- 【洛谷P2607】[ZJOI2008]骑士
骑士 题目链接 这道题一看,似乎和舞会是一样的,然而它并没有保证是一棵树 但是,对于每个连通块,必有相同的点数和边数,这样的图一定是一棵树上加一条边 这条边一定回使图中形成一个环,这种图貌似叫“基环树 ...
- Progress
这个标签用来表示进度,常用来表示下载的进度. <progress value="22" max="100"></progress> ...
- IOS NSNotification 通知
一. 先看下官方对NSNotification通知的解释 1. NSNotification 通知 @interface NSNotification : NSObject <NSCopying ...
- 在iOS中如何正确的实现行间距与行高
最近准备给 VirtualView-iOS 的文本元素新增一个 lineHeight 属性,以便和 VirtualView-Android配合时能更精确的保证双平台的一致性.面向 Google 以及 ...
- js日期相减得到分钟数
const date1 = new Date(fieldsValue.examStartTime); const date2 = new Date(fieldsValue.examEndTime); ...
- LeetCode 中级 - 路径总和2(113)
给定一个二叉树和一个目标和,找到所有从根节点到叶子节点路径总和等于给定目标和的路径. 说明: 叶子节点是指没有子节点的节点. 示例:给定如下二叉树,以及目标和 sum = 22, 5 / \ 4 8 ...
- poj_1306_Combinations
Computing the exact number of ways that N things can be taken M at a time can be a great challenge w ...
- Windows登录密码明文获取器
软件原理:本软件根据开源工具mimikatz2.0 修改!软件能直接读取系统明文密码! 支持32位.64位系统 win xp/vista/7/8/8.1 本机win10专业版测试不能获取,虚拟机win ...