《PHP编程最快明白》第七讲:php图片验证码与缩略图


Posted in PHP onNovember 01, 2010

实例22 图片验证的核心代码

<?php 
//header("content-type:image/png"); 
$num ='1234'; 
$imagewidth=60; 
$imageheight=18; $numimage = imagecreate($imagewidth,$imageheight); 
imagecolorallocate($numimage,240,240,240); 
for($i=0;$i<strlen($num);$i++){ 
$x = mt_rand(1,8)+$imagewidth*$i/4; 
$y = mt_rand(1,$imageheight/4); 
$color=imagecolorallocate($numimage,mt_rand(0,150),mt_rand(0,150),mt_rand(0,150)); 
imagestring($numimage,5,$x,$y,$num[$i],$color); 
} 
for($i=0;$i<200;$i++){ 
$randcolor=imagecolorallocate($numimage,rand(200,255),rand(200,255),rand(200,255)); 
imagesetpixel($numimage,rand()%70,rand()%20,$randcolor); 
} 
imagepng($numimage); 
imagedestroy($numimage); 
?>

这个是输出4个验证码的例子,对于汉字,需要font文件和imagettftext函数,用到的时候大家再网上搜索吧。你要产生随机数,那有mt_rand函数;你还要用到session保存这个随机数;如果需要转成utf-8,需要iconv函数。

实例23 缩略图

<?php 
class SimpleImage { 
var $image; 
var $image_type; 
function load($filename) { 
$image_info = getimagesize($filename); 
$this->image_type = $image_info[2]; 
if( $this->image_type == IMAGETYPE_JPEG ) { 
$this->image = imagecreatefromjpeg($filename); 
} elseif( $this->image_type == IMAGETYPE_GIF ) { 
$this->image = imagecreatefromgif($filename); 
} elseif( $this->image_type == IMAGETYPE_PNG ) { 
$this->image = imagecreatefrompng($filename); 
} 
} 
function save($filename, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null) { 
if( $image_type == IMAGETYPE_JPEG ) { 
imagejpeg($this->image,$filename,$compression); 
} elseif( $image_type == IMAGETYPE_GIF ) { 
imagegif($this->image,$filename); 
} elseif( $image_type == IMAGETYPE_PNG ) { 
imagepng($this->image,$filename); 
} 
if( $permissions != null) { 
chmod($filename,$permissions); 
} 
} 
function output($image_type=IMAGETYPE_JPEG) { 
if( $image_type == IMAGETYPE_JPEG ) { 
imagejpeg($this->image); 
} elseif( $image_type == IMAGETYPE_GIF ) { 
imagegif($this->image); 
} elseif( $image_type == IMAGETYPE_PNG ) { 
imagepng($this->image); 
} 
} 
function getWidth() { 
return imagesx($this->image); 
} 
function getHeight() { 
return imagesy($this->image); 
} 
function resizeToHeight($height) { 
$ratio = $height / $this->getHeight(); 
$width = $this->getWidth() * $ratio; 
$this->resize($width,$height); 
} 
function resizeToWidth($width) { 
$ratio = $width / $this->getWidth(); 
$height = $this->getheight() * $ratio; 
$this->resize($width,$height); 
} 
function scale($scale) { 
$width = $this->getWidth() * $scale/100; 
$height = $this->getheight() * $scale/100; 
$this->resize($width,$height); 
} 
function resize($width,$height) { 
$new_image = imagecreatetruecolor($width, $height); 
imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight()); 
$this->image = $new_image; 
} 
} $newfile = UPLOAD_DIR."/icons/".md5($_SESSION['USER']->email).".jpg";//上传文件保存的目录 
$image = new SimpleImage(); 
$image->load($_FILES['icons']['tmp_name']);//上传的临时文件名 
$image->resizeToWidth(80);设置宽度 
$image->save($newfile); 
?>
PHP 相关文章推荐
php下实现在指定目录搜索指定类型文件的函数
Oct 03 PHP
抓取YAHOO股票报价的类
May 15 PHP
用Simple Excel导出xls实现方法
Dec 06 PHP
php不用正则验证真假身份证
Nov 06 PHP
采用ThinkPHP中F方法实现快速缓存实例
Jun 13 PHP
PHP查询快递信息的方法
Mar 07 PHP
54个提高PHP程序运行效率的方法
Jul 19 PHP
PHP如何通过AJAX方式实现登录功能
Nov 23 PHP
thinkPHP交易详情查询功能详解
Dec 02 PHP
详解yii2使用多个数据库的案例
Jun 16 PHP
php多进程模拟并发事务产生的问题小结
Dec 07 PHP
在laravel中实现ORM模型使用第二个数据库设置
Oct 24 PHP
《PHP编程最快明白》第六讲:Mysql数据库操作
Nov 01 #PHP
《PHP编程最快明白》第五讲:php目录、文件操作
Nov 01 #PHP
《PHP编程最快明白》第四讲:日期、表单接收、session、cookie
Nov 01 #PHP
《PHP编程最快明白》第三讲:php数组
Nov 01 #PHP
《PHP编程最快明白》第二讲 数字、浮点、布尔型、字符串和数组
Nov 01 #PHP
一篇有意思的技术文章php介绍篇
Oct 26 #PHP
理解php原理的opcodes(操作码)
Oct 26 #PHP
You might like
PHP 存取 MySQL 数据库的一个例子
2006/10/09 PHP
PHP中的正规表达式(一)
2006/10/09 PHP
php 数组处理函数extract详解及实例代码
2016/11/23 PHP
PHP 搜索查询功能实现
2016/11/29 PHP
php实现留言板功能
2017/03/05 PHP
创建无限极分类树型结构的简单方法
2017/06/20 PHP
在第一个input框内输入内容.textarea自动得到第一个文件框的值的javascript代码
2007/04/20 Javascript
文本框的字数限制功能jquery插件
2009/11/24 Javascript
单击复制文字兼容各浏览器的完美解决方案
2013/07/04 Javascript
JavaScript的模块化:封装(闭包),继承(原型) 介绍
2013/07/22 Javascript
js拖动div 当鼠标移动时整个div也相应的移动
2013/11/21 Javascript
JS延迟加载加快页面打开速度示例代码
2013/12/30 Javascript
JavaScript对象的property属性详解
2014/04/01 Javascript
js控制网页背景音乐播放与停止的方法
2015/02/06 Javascript
JS实现方向键切换输入框焦点的方法
2015/08/19 Javascript
谈谈js中的prototype及prototype属性解释和常用方法
2015/11/25 Javascript
微信小程序 简单DEMO布局,逻辑,样式的练习
2016/11/30 Javascript
如何清除IE10+ input X 文本框的叉叉和密码输入框的眼睛图标
2016/12/21 Javascript
js中数组的常用方法小结
2016/12/30 Javascript
js实现点击切换checkbox背景图片的简单实例
2017/05/08 Javascript
让微信小程序支持ES6中Promise特性的方法详解
2017/06/13 Javascript
jQuery Datatable 多个查询条件自定义提交事件(推荐)
2017/08/24 jQuery
Vue.js项目模板搭建图文教程
2017/09/20 Javascript
详解webpack import()动态加载模块踩坑
2018/07/17 Javascript
新手必须知的Node.js 4个JavaScript基本概念
2018/09/16 Javascript
Python中的下划线详解
2015/06/24 Python
Python使用三种方法实现PCA算法
2017/12/12 Python
python实现替换word中的关键文字(使用通配符)
2020/02/13 Python
结合CSS3的布局新特征谈谈常见布局方法
2016/01/22 HTML / CSS
Pandora西班牙官方商店:PandoraShop.es
2020/10/05 全球购物
社区八一活动方案
2014/02/03 职场文书
优秀电子工程系毕业生求职信
2014/05/24 职场文书
收入证明怎么写
2015/06/12 职场文书
2016优秀班主任个人先进事迹材料
2016/02/26 职场文书
Python 数据科学 Matplotlib图库详解
2021/07/07 Python
教你如何让spark sql写mysql的时候支持update操作
2022/02/15 MySQL