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 ...
随机推荐
- Educational Codeforces Round 110 (Rated for Div. 2) (AB签到,C题双指针,D题DP好题)
补题链接:Here 1535A. Fair Playoff 四名选手参加了季后赛.比赛按以下方案进行:第一名选手与第二名选手比赛,第三名选手与第四名选手比赛,然后两人中的获胜者进入决赛. 众所周知,在 ...
- Educational Codeforces Round 6 620E. New Year Tree(DFS序+线段树)
题目链接:点击打开链接 题意:给你一棵树,编号1~n,告诉你根结点是1. 每次有两个操作: 1,将以v为根的子树的结点全部染成颜色c 2,问以v为根的紫书的结点的颜色种类. 思路:如果这是一条线段的话 ...
- C#开源跨平台的多功能Steam工具箱
前言 作为一名程序员你是否会经常会遇到GitHub无法访问(如下无法访问图片),或者是访问和下载源码时十分缓慢就像乌龟爬行一般.今天分享一款C#开源的.跨平台的多功能Steam工具箱和GitHub加速 ...
- VuePress搭建博客部署Gitee Pages
使用技术 VuePress + vuepress-theme-reco VuePress简介 VuePress文档地址 简洁至上:以 Markdown 为中心的项目结构,以最少的配置帮助你专注于写作. ...
- 分享这位大神的WPF界面设计系列视频
本文结构: 前言 视频详情 搬运详情 总结 4.1 国内推荐WPF资源 4.2 B站是学习的天堂 4.3 去外面看看 4.4 个人给C/S同学建议 1. 前言 今天介绍油管上一个大佬发的WPF设计系列 ...
- 【SHELL】命令补全
# 指定文件 dodo_path=/home/skull/work/scripts/dodo echo "hello skull" ## COMP_WORDS 是一个 bash 内 ...
- restful-接口风格
- IL合集二
引言 在第一篇关于IL的文章中,我们写了一些IL的相加,创建对象,循环以及实现TryCatch的一些功能,接下来,为大家带上后续关于IL的更新,其中包括,类型转换,以及条件判断,还有定义字段,定义属性 ...
- [转帖]oracle ZHS16GBK的数据库导入到字符集为AL32UTF8的数据库(转载+自己经验总结)
字符集子集向其超集转换是可行的,如此例 ZHS16GBK转换为AL32UTF8. 导出使用的字符集将会记录在导出文件中,当文件导入时,将会检查导出时使用的字符集设置,如果这个字符集不同于导入客户端的N ...
- [转帖]TIDB_HOT_REGIONS
https://docs.pingcap.com/zh/tidb/stable/information-schema-tidb-hot-regions TIDB_HOT_REGIONS 表提供了关于当 ...