《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 json_encode奇怪问题说明
Sep 27 PHP
php地址引用(php地址引用的效率问题)
Mar 23 PHP
PHP 验证码不显示只有一个小红叉的解决方法
Sep 30 PHP
PHP数据库链接类(PDO+Access)实例分享
Dec 05 PHP
PHP中的use关键字概述
Jul 23 PHP
PHP使用静态方法的几个注意事项
Sep 16 PHP
php实现根据字符串生成对应数组的方法
Sep 22 PHP
php返回当前日期或者指定日期是周几
May 21 PHP
php session的锁和并发
Jan 22 PHP
Laravel 5.3 学习笔记之 错误&amp;日志
Aug 28 PHP
PHP检查网站是否宕机的方法示例
Jul 24 PHP
PHP实现类似于C语言的文件读取及解析功能
Sep 01 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检测图片木马多进制编程实践
2013/04/11 PHP
解析dedeCMS验证码的实现代码
2013/06/07 PHP
一个图片地址分解程序(用于PHP小偷程序)
2014/08/23 PHP
PHP levenshtein()函数用法讲解
2019/03/08 PHP
flexigrid 参数说明
2010/11/23 Javascript
使用JQuery快速实现Tab的AJAX动态载入(实例讲解)
2013/12/11 Javascript
利用JS解决ie6不支持max-width,max-height问题的方法
2014/01/02 Javascript
JavaScript排序算法之希尔排序的2个实例
2014/04/04 Javascript
用jQuery toggleClass 实现鼠标移上变色
2014/05/14 Javascript
JS长整型精度问题实例分析
2015/01/13 Javascript
JS实现自适应高度表单文本框的方法
2015/02/25 Javascript
jQuery实现悬浮在右上角的网页客服效果代码
2015/10/24 Javascript
Nodejs的express使用教程
2015/11/23 NodeJs
jquery树形菜单效果的简单实例
2016/06/06 Javascript
JS使用单链表统计英语单词出现次数
2016/06/16 Javascript
jQuery ajax中使用confirm,确认是否删除的简单实例
2016/06/17 Javascript
JS实现的幻灯片切换显示效果
2016/09/07 Javascript
js实现百度地图定位于地址逆解析,显示自己当前的地理位置
2016/12/08 Javascript
javascript和jQuery中的AJAX技术详解【包含AJAX各种跨域技术】
2016/12/15 Javascript
Move.js入门
2017/02/08 Javascript
Nodejs基于LRU算法实现的缓存处理操作示例
2017/03/17 NodeJs
Vue中的混入的使用(vue mixins)
2018/06/01 Javascript
Vue实现双向绑定的原理以及响应式数据的方法
2018/07/02 Javascript
微信小程序 image组件遇到的问题
2019/05/28 Javascript
[56:58]VP vs Optic 2018国际邀请赛小组赛BO2 第一场 8.16
2018/08/17 DOTA
[03:48]大碗DOTA
2019/07/25 DOTA
Python实现把回车符\r\n转换成\n
2015/04/23 Python
6行Python代码实现进度条效果(Progress、tqdm、alive-progress​​​​​​​和PySimpleGUI库)
2020/01/06 Python
基于python图书馆管理系统设计实例详解
2020/08/05 Python
Django执行源生mysql语句实现过程解析
2020/11/12 Python
如何利用find命令查找文件
2015/02/07 面试题
自动化职业生涯规划书范文
2014/01/03 职场文书
客服专员岗位职责
2015/02/10 职场文书
教师个人工作总结范文2015
2015/10/14 职场文书
关于公司年会的开幕词
2016/03/04 职场文书
总结高并发下Nginx性能如何优化
2021/11/01 Servers