PHP基于GD库的图像处理方法小结


Posted in PHP onSeptember 27, 2016

本文实例讲述了PHP基于GD库的图像处理方法。分享给大家供大家参考,具体如下:

gd图像处理技术

extension=php_gd2.dll

创建画布

画布,一种资源型数据,可操作的图像资源

创建画布(新建)

imageCreate(width,height) //创建基于调色板的画布

imageCreateTrueColor(width,height) //创建真彩色的画布

基于图片创建画布(打开)

imageCreateFromJPEG( url)
imageCreateFromPNG(url)
imageCreateFromGIF(url)

操作画布

分配颜色:如果需要在画布上使用某种颜色,应该先将颜色分配到画布上。

(颜色标识 )= imageColorAllocate(img,r,g,b)

填充画布

imageFill(img,x,y,颜色标识)

输出画布

1. 输出到图片文件

2. 直接输出,需要告知浏览器输出为图片信息(header("Content-type:image/png;")

imagePNG(img[,url])
imageJPEG()
imageGIF()

销毁画布资源

imageDestroy(img)

<?php
header('content-type:image/png');
$img = imagecreate(300,300);
$color = imagecolorallocate($img,223,22,44);
imagefill($img,3,3,$color);
imagepng($img);
imagedestroy();
?>

运行效果图如下:

PHP基于GD库的图像处理方法小结

验证码实现

<?php
  header('content-type:image/png');
  $code = '123456789abcdefghijklmnpqrstuvwxvz';
  $length = strlen($code);
  $print = '';
  for($i=0; $i<4; $i++){
    $print.=$code[mt_rand(0,$length-1)];
  }
//  echo $print;
  $img = imagecreatefrompng('./str.png');
  $color = mt_rand(0,1)==1?imagecolorallocate($img,0,0,0):imagecolorallocate($img,255,255,255);
  //图片大小
  $img_width = imagesx($img);
  $img_height = imagesy($img);
  //字体大小
  $font = 5;
  $font_width = imagefontwidth($font);
  $font_height = imagefontheight($font);
  $fin_w = ($img_width-$font_width*4)/2;
  $fin_h = ($img_height-$font_height)/2;
  imagestring($img,$font,$fin_w,$fin_h,$print,$color);
  imagepng($img);
  imagedestroy($img);
?>
<image src="gd_string.php" onclick="this.src='gd_string.php?ra='+Math.random()"></image>

运行效果图如下:

 PHP基于GD库的图像处理方法小结

<?php
session_start();
$im=imagecreatetruecolor(80,30);
$str="";
for ($i=0;$i<4;$i++){
  $str.=dechex(rand(0,15));
}
$_SESSION['code']=$str;
$white=imagecolorallocate($im,255,255,255);
imagestring($im,rand(2,5),rand(0,70),rand(0,10),$str,$white);
//imagettftext($im,rand(0,5),rand(0,180),rand(0,100),rand(0,10),$white,"simhei.ttf",$str);
for($i=0;$i<20;$i++){
$color=imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));
imageline($im,rand(0,90),rand(0,20),rand(0,100),rand(0,100),$color);
}
header("content-type:image/png");
imagepng($im);
imagedestroy($im);
?>

注意:图片输出前后不能有额外输出

希望本文所述对大家PHP程序设计有所帮助。

PHP 相关文章推荐
PHP4之真OO
Oct 09 PHP
php字符串截取中文截取2,单字节截取模式
Dec 10 PHP
PHP中全局变量global和$GLOBALS[]的区别分析
Aug 06 PHP
PHP实现多进程并行操作的详解(可做守护进程)
Jun 18 PHP
解析VS2010利用VS.PHP插件调试PHP的方法
Jul 19 PHP
php根据年月获取季度的方法
Mar 31 PHP
PHP递归调用数组值并用其执行指定函数的方法
Apr 01 PHP
php中实现获取随机数组列表的自定义函数
Apr 02 PHP
PHP实现的增强性mhash函数
May 27 PHP
PHP输入流php://input实例讲解
Dec 22 PHP
php和nginx交互实例讲解
Sep 24 PHP
php中yar框架实例用法讲解
Dec 27 PHP
PHP文件上传操作实例详解
Sep 27 #PHP
PHP目录操作实例总结
Sep 27 #PHP
PHP文件操作实例总结
Sep 27 #PHP
PHP命名空间namespace用法实例分析
Sep 27 #PHP
PHP7标量类型declare用法实例分析
Sep 26 #PHP
PHP7新增运算符用法实例分析
Sep 26 #PHP
PHP7匿名类用法分析
Sep 26 #PHP
You might like
《PHP边学边教》(02.Apache+PHP环境配置――上篇)
2006/12/13 PHP
PHP分多步骤填写发布信息的简单方法实例代码
2012/09/23 PHP
微信开发之获取JSAPI TICKET
2017/07/07 PHP
教您去掉ie网页加载进度条的方法
2010/12/09 Javascript
Javascript学习笔记之数组的构造函数
2014/11/23 Javascript
jQuery实现跨域iframe接口方法调用
2015/03/14 Javascript
JQuery控制radio选中和不选中方法总结
2015/04/15 Javascript
原生JS实现拖拽图片效果
2020/08/27 Javascript
JS中Eval解析JSON字符串的一个小问题
2016/02/21 Javascript
Jquery对新插入的节点 绑定Click事件失效的解决方法
2016/06/02 Javascript
AngularJS模块详解及示例代码
2016/08/17 Javascript
12个非常有用的JavaScript技巧
2017/05/17 Javascript
vue2.0s中eventBus实现兄弟组件通信的示例代码
2017/10/25 Javascript
React教程之封装一个Portal可复用组件的方法
2018/01/02 Javascript
使用JS获取页面上的所有标签
2018/10/18 Javascript
微信小程序module.exports模块化操作实例浅析
2018/12/20 Javascript
微信小程序实现简单表格
2019/02/14 Javascript
微信小程序image图片加载完成监听
2019/08/31 Javascript
vue实现div单选多选功能
2020/07/16 Javascript
[01:13]这,就是刀塔
2014/07/16 DOTA
基于python实现微信模板消息
2015/12/21 Python
用python实现的线程池实例代码
2018/01/06 Python
flask中过滤器的使用详解
2018/08/01 Python
使用Python 正则匹配两个特定字符之间的字符方法
2018/12/24 Python
django实现类似触发器的功能
2019/11/15 Python
python自动从arxiv下载paper的示例代码
2020/12/05 Python
中医专业职业生涯规划书范文
2014/01/04 职场文书
公务员政审个人鉴定
2014/02/25 职场文书
农林环境专业求职信
2014/03/13 职场文书
毕业生找工作求职信
2014/08/05 职场文书
好的促销活动方案
2014/08/21 职场文书
师德自我剖析材料范文
2014/10/06 职场文书
论文致谢词范文
2015/05/14 职场文书
企业廉洁教育心得体会
2016/01/20 职场文书
opencv深入浅出了解机器学习和深度学习
2022/03/17 Python
实现GO语言对数组切片去重
2022/04/20 Golang