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 ...
随机推荐
- Sentinel 是如何做限流的
限流是保障服务高可用的方式之一,尤其是在微服务架构中,对接口或资源进行限流可以有效地保障服务的可用性和稳定性. 之前的项目中使用的限流措施主要是Guava的RateLimiter.RateLimite ...
- spring中的核心类有那些,各有什么作用?
BeanFactory:产生一个新的实例,可以实现单例模式BeanWrapper:提供统一的get及set方法ApplicationContext:提供框架的实现,包括BeanFactory的所有功能 ...
- VUE里使用iframe在更改了src之后对应的网页并未刷新解决方案
在vue 里使用iframe,在更新src后页面并未刷新, 在更改iframe src属性值之前加上这一句即可 document.getElementById(iframe的id).contentWi ...
- canvas验证码 uni-app/小程序
1 <template> 2 <view class="logo-wrapper"> 3 <view class="logo-img&quo ...
- 01-module/分频器/激励写法
1.module module有出入接口,输出接口 module有时钟和复位 // input clock; rest_n; // n表示低电平复位 //output o_data; module m ...
- Java中有哪些方式能实现锁某个变量
有的时候博客内容会有变动,首发博客是最新的,其他博客地址可能会未同步,认准https://blog.zysicyj.top 首发博客地址 系列文章地址 在Java中,有几种方式可以实现对某个变量的锁定 ...
- [转帖]Mnesia reports that this RabbitMQ cluster has experienced a network partition.
一 问题描述 双节点RabbitMQ集群发生了脑裂,节点日志报错: [error] <0.6318.0> Mnesia(rabbit@pc2): ** ERROR ** mnesia_ev ...
- [转帖]为什么不推荐使用/etc/fstab
https://www.jianshu.com/p/af49a5d0553f 对于工作中使用服务器的公司来讲,每到节假日来临时,总免不了对服务器进行下电.而收假回来的早上,则会有一个早上的时间会花费在 ...
- [转帖]如何利用wrarp测试oss性能?
https://zhuanlan.zhihu.com/p/529735003 前言 我们利用mino与ceph rgw搭建好的oss经过多层网络转发,传输速度必定有所折损,这个时候我们使用wrap ...
- [转帖]Linux shell 单引号和双引号
https://www.cnblogs.com/airoot/p/15324883.html 在编写shell脚本的时候经常会用到引号,有些时候却老是忘记单引号和双引号之间的区别,所以就整理一下供以后 ...