GD库常用实例
GD库常用实例
一、图片水印
1、实现步骤
- 打开原图(也叫操作的目标图片)
- 打开水印图(也叫水印来源图片)
- 使用 imagecopymerge 将小图合并至大图的指定位置
- 输出图片
- 销毁资源
2、实例代码
$dst_path = 'image/meinv.jpg';
$dst = imagecreatefromstring(file_get_contents($dst_path));
$logo_path = 'image/jack.jpg';
$logo = imagecreatefromstring(file_get_contents($logo_path));
list($dst_width,$dst_height) = getimagesize($dst_path);
list($logo_width,$logo_height) = getimagesize($logo_path);
$dst_x = $dst_width - $logo_width;
$dst_y = $dst_height - $logo_height;
//要将图片加在右下脚
imagecopymerge($dst, $logo, $dst_x, $dst_y, 0, 0, $logo_width, $logo_height, 50);
header('Content-type:image/png');
imagepng($dst);
imagedestroy($dst);
二、文字水印
/**
* imagecolorallocatealpha() 的行为和 imagecolorallocate() 相同,
* 但多了一个额外的透明度参数 alpha,其值从 0 到 127。0 表示完全不透明,127 表示完全透明。
*/
$filename = 'image/meinv.jpg';
$fileinfo = getimagesize($filename);
list( $width, $height ) = $fileinfo;
$mime = $fileinfo['mime'];
$createFrom = str_replace('/','createfrom',$mime);
$outFun = str_replace('/','',$mime);
$image = $createFrom($filename);
//设置字体
$font = 'C:\Windows\Fonts\msyh.ttc';//建议使用绝对路径
imagettftext($image, 30, 0, 0,ceil(($height-30)/2), $color, $font, 'Jack喜欢的妞');
header('content-type:'.$mime);
$outFun($image);
imagedestroy($image);
三、验证码
function createCheckCode( $codeNum ){ $code="23456789abcdefghijkmnpqrstuvwxyzABCDEFGHIJKMNPQRSTUVWXYZ";
$string='';
//从字符串中取出随机的字符
for($i=0; $i < $codeNum; $i++){
$char=$code{mt_rand(0, strlen($code)-1)};
$string.=$char;
}
//返回字符内容
return $string;
}
$width = 100;
$height = 30;
# 准备画布
$image = imagecreatetruecolor($width, $height);
# 准备颜色
$color_dan = imagecolorallocate($image, 200, 200, 200);
$color_shen = imagecolorallocate($image, 2, 2, 2);
# 填充背景颜色
imagefilledrectangle($image, 0, 0, $width, $height, $color_dan);
# 将字符写入画布
$str = createCheckCode(4);
for ($i = 0; $i < 4; $i++) {
$pj = 100/4;
$startx = $i*$pj + 10;
$starty = rand(0, 30-15);
imagechar($image, 7, $startx, $starty, $str{$i}, $color_shen);
}
for ($i = 0; $i < 100; $i++) {
imagesetpixel($image, mt_rand(0, 100), mt_rand(0, 30), $color_shen);
}
# 告诉浏览器你弄了一个啥
header('Content-Type:image/png');
# 以 PNG 格式将图像输出到浏览器或文件
imagepng($image);
# 销毁 节省内存
imagedestroy($image);
四、图片缩放
- 打开来源图片
- 设置图片缩放百分比(缩放)
- 获得来源图片,按比调整大小
- 新建一个指定大小的图片为目标图
- 将来源图调整后的大小放到目标中
- 销毁资源
$src_path = 'image/meinv.jpg';
$src_image = imagecreatefromstring(file_get_contents($src_path));
list( $src_w, $src_h ) = getimagesize($src_path);
$p = 0.3;
$new_width = $src_w * $p;
$new_height = $src_h * $p;
$new_image = imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($new_image, $src_image, 0, 0, 0, 0, $new_width, $new_height, $src_w, $src_h);
header('content-type:image/jpeg');
imagejpeg($new_image);
五、验证码类
$char=array_merge(range('a','z'),range('A','Z'),range(1,9));
<?php
//验证码类
class ValidateCode {
private $charset = 'abcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ23456789';//随机因子
private $code;//验证码
private $codelen ;//验证码长度
private $width;//宽度
private $height;//高度
private $img;//图形资源句柄
private $font;//指定的字体格式
private $fontsize;//指定字体大小
private $fontcolor;//指定字体颜色
//构造方法初始化
public function __construct($codelen=4,$width=130,$height=50,$fontsize=20) {
//dirname()返回路径中的目录部分,__FILE__ 文件的完整路径和文件名
$this->codelen=$codelen;
$this->width=$width;
$this->height=$height;
$this->fontsize=$fontsize;
$this->font = 'C:\Windows\Fonts\msyh.ttc';//注意字体路径要写对,否则显示不了图片
}
//生成随机码
private function createCode() {
$_len = strlen($this->charset)-1;
for ($i=0;$i<$this->codelen;$i++) {
$this->code .= $this->charset[mt_rand(0,$_len)];
}
}
//生成背景
private function createBg() {
//新建一个真彩色图像
$this->img = imagecreatetruecolor($this->width, $this->height);
//为一幅图像分配颜色
$color = imagecolorallocate($this->img, mt_rand(157,255), mt_rand(157,255), mt_rand(157,255));
//画一矩形并填充
imagefilledrectangle($this->img,0,$this->height,$this->width,0,$color);
}
//生成文字
private function createFont() {
$_x = $this->width / $this->codelen;
for ($i=0;$i<$this->codelen;$i++) {
$this->fontcolor = imagecolorallocate($this->img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));
imagettftext($this->img,$this->fontsize,mt_rand(-20,20),$_x*$i+mt_rand(10,20),mt_rand($this->fontsize+5,$this->height-5),$this->fontcolor,$this->font,$this->code[$i]);
}
}
//生成线条、雪花
private function createLine() {
//线条
for ($i=0;$i<6;$i++) {
$color = imagecolorallocate($this->img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));
imageline($this->img,mt_rand(0,$this->width),mt_rand(0,$this->height),mt_rand(0,$this->width),mt_rand(0,$this->height),$color);
}
//雪花
for ($i=0;$i<100;$i++) {
$color = imagecolorallocate($this->img,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
imagestring($this->img,mt_rand(1,5),mt_rand(0,$this->width),mt_rand(0,$this->height),'*',$color);
}
}
//输出
private function outPut() {
header('Content-type:image/png');
imagepng($this->img);
imagedestroy($this->img);
}
//对外生成
public function doimg() {
$this->createBg();
$this->createCode();
$this->createLine();
$this->createFont();
$this->outPut();
}
//获取验证码
public function getCode() {
return strtolower($this->code);
}
}
//用法
//$p=new ValidateCode(5,250,60,30);
//$p->doimg();
?>
GD库常用实例的更多相关文章
- GD库常用函数
创建句柄 imagecreate($width, $height) //新建图像 imagecreat ...
- PHP的GD库
GD库 PHP通过GD库,可以对JPG.PNG.GIF.SWF等图片进行处理.GD库常用在图片加水印,验证码生成等方面. 绘制线条 要对图形进行操作,首先要新建一个画布,通过imagecreatetr ...
- php 安装扩展插件实例-gd库
今天给php 安装一个扩展插件 gd库 一.gd库是什么 gd库是一个开源的图像处理库,它通过提供一系列用来处理图片的API,使其可以用来创建图表.图形.缩略图以及其他图像的处理操作. gd库支持 ...
- 使用SharePoint Designer定制开发专家库系统实例!
将近大半年都没有更新博客了,趁这段时间不忙,后续会继续分享一些技术和实际应用.对于Sharepoint的定制开发有很多种方式,对于一般的应用系统,可以使用Sharepoint本身自带的功能,如列表作为 ...
- 安装GD库解决ThinkPHP 验证码Call to undefined function Think\imagecreate()出错
在php中imagecreate函数是一个图形处理函数,主要用于新建一个基于调色板的图像了,然后在这个基础上我们可以创建一些图形数字字符之类的,但这个函数需要GD库支持,如果没有开启GD库使用时会提示 ...
- GD库使用小结---1
因为一开始,“大家”都说一般任务中,用php操作图片不常见,像我们这种基本业务型的,就更用不到了,所以先别看,偶就没有看.现在有机会了自然要来玩一把. 以前学过C#的GDI+,交了课程设计后忘得一干二 ...
- PHP5 GD库生成图形验证码(汉字)
PHP5 GD库生成图形验证码且带有汉字的实例分享. 1,利用GD库函数生成图片,并在图片上写指定字符imagecreatetruecolor 新建一个真彩色图像imagecolorallocate ...
- GD库知识点
GD库:PHP的一个扩展库,主要用于绘制动态图,根据数据动态响应的图片 如统计图 验证码 其他的用途如:处理已有图像 图片的缩放 裁剪 图片水印 文字水印 1.安装GD库 2.画图步骤:创建背景图像( ...
- PHP 使用GD 库绘制图像,无法显示的问题
根据官方GD 库绘制图像文档样式 原基本样式 $width = 120; $height = 50; $img = @imagecreatetruecolor($width, $height) or ...
- 【PHP】使用GD库实现 图像生成、缩放、logo水印和简单验证码
gd库是php最常用的图片处理库之一(另外一个是imagemagick),可以生成图片.验证码.水印.缩略图等等.要使用gd库首先需要开启gd库扩展, windows系统下需要在php.ini中将ex ...
随机推荐
- POJ - 1190 生日蛋糕(深搜+神奇的剪枝)
链接:https://ac.nowcoder.com/acm/contest/1015/B 题目描述 7月17日是Mr.W的生日,ACM-THU为此要制作一个体积为Nπ的M层生日蛋糕,每层都是一个圆柱 ...
- 深度学习(六)——神经网络的基本骨架:nn.Module的使用
一.torch.nn简介 官网地址: torch.nn - PyTorch 2.0 documentation 1. torch.nn中的函数简介 Containers:神经网络的骨架 Convolu ...
- 基于AHB_BUS Clac slave详解
基于AHB-APB BUS slave详解 1.目录 高内聚:让模块的功能更集中,更单一. AMBA总线例子,需要有一个模块和AMBA进行交互,就可以单独将与AHB总线进行交互的部分作为一个模块.经常 ...
- Listener refused the connection with the following error: ORA-12514
1.问题 在使用Oracle SQL Developer时,遇到以下问题: 状态: 失败 -测试失败: Listener refused the connection with the followi ...
- Razor 语法@Html.DropDownList,根据List集合或者枚举生成Select标签
1.根据List集合生成Select标签,根据数据库数据换成SelectListItem集合 Action 方法(也可使用下方的List集合的扩展方法): 1 var selectList = DBL ...
- MySQL高可用九种方案
有的时候博客内容会有变动,首发博客是最新的,其他博客地址可能会未同步,认准https://blog.zysicyj.top 首发博客地址 参考视频 MMM 方案(单主) MySQL 高可用方案之 MM ...
- 查看正在运行容器的环境变量-寻找容器运行mysql的root密码
查看正在运行容器的环境变量-寻找容器运行mysql的root密码 背景 有一个服务器上面运行着一个长达两年的mysql数据库实例. 因为当时root密码是通过环境变量注入进去的. 现在我想重新连接一下 ...
- Intel 移动CPU天梯榜
Intel酷睿i9-13980HX 2023 2121 Intel酷睿i9-13900HX 2023 2051 Intel酷睿i9-13950HX 2023 2005 4 + Intel酷睿i9-12 ...
- [转帖]jmeter实现不写代码把测试结果存入execl
这里使用数据库作为中间件来实现不写代码就把测试结果存入execl,下面是步骤 1.新建一个setup线程组用来设置数据库连接信息和新建数据库,如下图所示,我们使用sqlite数据库来存储信息,因为不需 ...
- [转帖]【Jmeter】Jmeter压力测试工具安装及使用教程(redis测试)
摘自:https://www.cnblogs.com/monjeo/p/9330464.html 一.Jmeter下载 进入官网:http://jmeter.apache.org/ 1.第一步进入官网 ...