《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音乐采集(部分代码)
Feb 14 PHP
NOD32 v2.70.32 简体中文封装版 提供下载了
Feb 27 PHP
PHP生成月历代码
Jun 14 PHP
php 生成文字png图片的代码
Apr 17 PHP
shopex主机报错误请求解决方案(No such file or directory)
Dec 27 PHP
PHP编程函数安全篇
Jan 08 PHP
PHP中对缓冲区的控制实现代码
Sep 29 PHP
PHP调用wsdl文件类型的接口代码分享
Nov 19 PHP
php实现按指定大小等比缩放生成上传图片缩略图的方法
Dec 15 PHP
php日期操作技巧小结
Jun 25 PHP
php基于环形链表解决约瑟夫环问题示例
Nov 07 PHP
详解json在php中的应用
Sep 30 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
Smarty的配置与高级缓存技术分享
2012/06/05 PHP
dedecms函数分享之获取某一栏目所有子栏目
2014/05/19 PHP
PHP中$_SERVER使用说明
2015/07/05 PHP
PHP实现linux命令tail -f
2016/02/22 PHP
PHP高并发和大流量解决方案整理
2019/12/24 PHP
js form 验证函数 当前比较流行的错误提示
2009/06/23 Javascript
JavaScript与DOM组合动态创建表格实例
2012/12/23 Javascript
JS/FLASH实现复制代码到剪贴板(兼容所有浏览器)
2013/05/27 Javascript
详细分析JavaScript变量类型
2015/07/08 Javascript
JavaScript操作URL的相关内容集锦
2015/10/29 Javascript
使用jQuery.form.js/springmvc框架实现文件上传功能
2016/05/12 Javascript
jquery实现超简单的瀑布流布局【推荐】
2017/03/08 Javascript
VUE 使用中踩过的坑
2018/02/08 Javascript
PM2自动部署代码步骤流程总结
2018/12/10 Javascript
微信小程序 image组件遇到的问题
2019/05/28 Javascript
vue实现商城秒杀倒计时功能
2019/12/12 Javascript
微信小程序自定义菜单切换栏tabbar组件代码实例
2019/12/30 Javascript
解决uWSGI的编码问题详解
2017/03/24 Python
对Python字符串中的换行符和制表符介绍
2018/05/03 Python
Python面向对象类的继承实例详解
2018/06/27 Python
pandas 使用均值填充缺失值列的小技巧分享
2019/07/04 Python
使用PyTorch训练一个图像分类器实例
2020/01/08 Python
用python解压分析jar包实例
2020/01/16 Python
Python中的 ansible 动态Inventory 脚本
2020/01/19 Python
python GUI库图形界面开发之PyQt5拖放控件实例详解
2020/02/25 Python
Python实现从N个数中找到最大的K个数
2020/04/02 Python
tensorflow模型文件(ckpt)转pb文件的方法(不知道输出节点名)
2020/04/22 Python
丝芙兰法国官网:SEPHORA法国
2016/09/01 全球购物
日本PLST在线商店:日本时尚杂志刊载的人气服装
2016/12/10 全球购物
德国大型箱包和皮具商店:Koffer
2019/10/01 全球购物
应届大学生自荐信
2013/12/05 职场文书
两只小狮子教学反思
2014/02/05 职场文书
申请吧主发表的感言
2015/08/03 职场文书
php 防护xss,PHP的防御XSS注入的终极解决方案
2021/04/01 PHP
CSS3 Tab动画实例之背景切换动态效果
2021/08/23 HTML / CSS
MySQL远程无法连接的一些常见原因总结
2022/09/23 MySQL