《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下实现一个阿拉伯数字转中文数字的函数
Jul 10 PHP
PHP小程序自动提交到自助友情连接
Nov 24 PHP
php中session过期时间设置及session回收机制介绍
May 05 PHP
PHP实现的sqlite数据库连接类
Dec 12 PHP
PHP+apc+ajax实现的ajax_upload上传进度条代码
Jan 25 PHP
PHP的Yii框架中移除组件所绑定的行为的方法
Mar 18 PHP
Linux下编译redis和phpredis的方法
Apr 07 PHP
ThinkPHP中where()使用方法详解
Apr 19 PHP
Yii2数据库操作常用方法小结
May 04 PHP
PHP结合Vue实现滚动底部加载效果
Dec 17 PHP
浅析php如何实现爬取数据原理
Sep 27 PHP
php反射学习之不用new方法实例化类操作示例
Jun 14 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 var_dump遍历对象属性的函数与应用代码
2010/06/04 PHP
php指定函数参数默认值示例代码
2013/12/04 PHP
php实现将任意进制数转换成10进制的方法
2015/04/17 PHP
PHP基于工厂模式实现的计算器实例
2015/07/16 PHP
CodeIgniter控制器之业务逻辑实例分析
2016/01/20 PHP
Javascript中的数学函数集合
2007/05/08 Javascript
js 浮动层菜单收藏
2009/01/16 Javascript
javascript的onchange事件与jQuery的change()方法比较
2009/09/28 Javascript
JQuery中getJSON的使用方法
2010/12/13 Javascript
jquery 页面滚动到底部自动加载插件集合
2014/01/31 Javascript
js+CSS实现弹出居中背景半透明div层的方法
2015/02/26 Javascript
jquery实现Li滚动时滚动条自动添加样式的方法
2015/08/10 Javascript
jQuery中的ready函数与window.onload谁先执行
2016/06/21 Javascript
Vue.js 2.0 移动端拍照压缩图片上传预览功能
2017/03/06 Javascript
利用prop-types第三方库对组件的props中的变量进行类型检测
2017/05/02 Javascript
浅谈Vuejs Prop基本用法
2017/08/17 Javascript
使用jQuery给Table动态增加行、清空table的方法
2018/09/05 jQuery
js实现计时器秒表功能
2019/12/16 Javascript
Vue实现剪贴板复制功能
2019/12/31 Javascript
python获取糗百图片代码实例
2013/12/18 Python
跟老齐学Python之不要红头文件(2)
2014/09/28 Python
用Python的SimPy库简化复杂的编程模型的介绍
2015/04/13 Python
python实现网站的模拟登录
2016/01/04 Python
由浅入深讲解python中的yield与generator
2017/04/05 Python
python实现数据写入excel表格
2018/03/25 Python
Python实现iOS自动化打包详解步骤
2018/10/03 Python
详解使用PyInstaller将Pygame库编写的小游戏程序打包为exe文件
2019/08/23 Python
python 多线程爬取壁纸网站的示例
2021/02/20 Python
HTML5 绘制图像(上)之:关于canvas元素引领下一代web页面的问题
2013/04/24 HTML / CSS
瑞典手机壳品牌:Richmond & Finch
2018/04/28 全球购物
培训科主任岗位职责
2014/08/08 职场文书
高速铁道技术专业求职信
2014/08/09 职场文书
运动会广播稿50字-100字
2014/10/11 职场文书
幼儿园2014年度工作总结
2014/11/10 职场文书
教师个人自我评价
2015/03/04 职场文书
SQL SERVER中常用日期函数的具体使用
2021/04/08 SQL Server