GD库 使用

常用步骤

创建画布 imagescreatetruecolor($width,$height) 返回资源类型

(创建画布有两种方式1是创建一张空白的画布2是打开一张图片作为画布)

创建各种颜料 imagecolorallocate()

绘画 imagesfill()

保存成图片 imagepng()

销毁犯罪现场,销毁画布 imagedestroy()

 

<?php

/*基本使用 创建一张有填充背景的图片*/


/*
1:造画布(多宽,多高)  imagecreatetruecolor()
 返回是资源类型
*/
$width = 300;
$height = 200;
$im = imagecreatetruecolor($width,$height);
/*
2:创建颜料 imagecolorallocate
imagecolorallocate(画布资源,红,绿,蓝)
*/
$blue = imagecolorallocate($im,0,0,255);
/*
3:画图
imagefill是用颜料填充画布
bool imagefill (画布资源 , 填充的起始点x值 , 填充的起始点y值 , 填充颜色)
*/
imagefill($im,0,0,$blue);
/*
4:保存!
imagepng
imagejpeg
imagegif
..
来保存成不同图片格式
*/
if(imagepng($im,'./01.png')) {
    echo '图片生成成功!';
} else {
    echo 'fail';
}
/*
5:销毁画布
*/
imagedestroy($im);

 

注意 :屏幕的坐标系 以左上角为原点,向下为正Y 向右为正X

 

-如果要在一个图片文件中打开 :

$file = './home.jpg';

$im = imagecreatefromjpeg($file);

 

-如果要直接输出到页面,则imagepng()第二个参数不要即可 ; 输出到浏览器必须声明

header('content-type: image/png');

 

-图片无非也是0101?只是看图软件解析了

 

-字符串验证码的制作

<?php
/**
 * 制作英文验证码
 */


    // 1 造画布
    $im = imagecreatetruecolor(50,25);
    // 2浅色背景
    $gray = imagecolorallocate($im,220,220,220);

    // 造写字的随机颜色
    $randcolor = imagecolorallocate($im,mt_rand(0,150),mt_rand(0,150),mt_rand(0,150));
    //造划线的随机颜色
    $linecolor1 = imagecolorallocate($im,mt_rand(100,150),mt_rand(100,150),mt_rand(100,150));
    $linecolor2 = imagecolorallocate($im,mt_rand(100,150),mt_rand(100,150),mt_rand(100,150));
    $linecolor3 = imagecolorallocate($im,mt_rand(100,150),mt_rand(100,150),mt_rand(100,150));


    // 填充背景
    imagefill($im,0,0,$gray);

    // 画干扰线,值越小,颜色越浅
    imageline($im,0,mt_rand(0,25),50,mt_rand(0,25),$linecolor1);
    imageline($im,0,mt_rand(0,25),50,mt_rand(0,25),$linecolor2);
    imageline($im,0,mt_rand(0,25),50,mt_rand(0,25),$linecolor3);

    /*
    3 写字
    imagestring — 水平地画一行字符串
    说明
    bool imagestring ( resource $image , int $font , int $x , int $y , string $s , int $col )
    参数分别代表: 画布资源,字体大小(1-5中选择), 字符最左上角的x坐标,y坐标 ,要写的字符串,颜色
    */
    $str = substr(str_shuffle('ABCDEFGHIJKMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz0123456789'),0,4);
    imagestring($im,5,8,5,$str,$randcolor);
    header('content-type: image/png');
    //把图片输出到调用的网页中
    imagepng($im);
/****
验证码的字符串还想扭曲该如何做.参考正弦曲线函数,弧度函数
****/

 

记 : 随机颜色划线(imageline) 写字imagestring

图点击刷新需要用js制作

function chv(vcode) {

    //var vcode = document.getElementsByTagName('img')[0];

    vcode.src = vcode.src+'?_s='+Math.random();

}

οnclick="chv(this);"

 

-随机中文字符验证码

<?php
/***
====笔记部分====
中文验证码

如何产生随机的中文字符串?
中文按其unicode编码,是有规律的,
位于0x4E00-0x9FA0
我们可以在uncode范围内随机选取,

但是 请注意,对于用户来说,能否认得?
因为有大量生僻字.

所以在实际项目中,只是抽取几百或上千个常用汉字,放数组里,随机选取.
***/

//举例汉字
$char = array('','','','','','','');
shuffle($char);
$code = implode('',array_slice($char,0,4));


// 画布
$im = imagecreatetruecolor(65,25);

// 颜料
$gray = imagecolorallocate($im,200,200,200);
$blue = imagecolorallocate($im,0,0,255);


// 填充
imagefill($im,0,0,$gray);

// 写字
imagettftext($im,12,0,2,20,$blue,'./msyh.ttf',$code);


// 输出
header('content-type: image/jpeg;');
imagejpeg($im);

//销毁
imagedestroy($im);

(也可以做水印)

随机中文需要根据中文编码的规律 生成汉字

一般是选取几百或上千的汉字

imagettftext()

 

-画正方形,椭圆,员,饼状图

imagefilledarc()

★bool getimagesize($path) 返回值是一个数组,,获取图片的信息

 

 

转载于:https://www.cnblogs.com/lzhlearn/p/5804960.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值