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

实例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);
?>

(0)

相关推荐

  • php 生成随机验证码图片代码

    复制代码 代码如下: <?php /** 默认首页 **/ class DefaultController extends AppController { public function index() { $len = 5; $str = "ABCDEFGHIJKLNMPQRSTUVWXYZ123456789"; $im = imagecreatetruecolor ( 70, 20 ); $bgc = imagecolorallocate($im, 255, 255, 255

  • 一漂亮的PHP图片验证码实例

    一.显示效果二.代码如下 复制代码 代码如下: /* *  @Author fy */ $imgwidth =100; //图片宽度$imgheight =40; //图片高度$codelen =4; //验证码长度$fontsize =20; //字体大小$charset = 'abcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ23456789';$font = 'Fonts/segoesc.ttf'; $im=imagecreatetruecolor($im

  • php图片验证码代码

    复制代码 代码如下: <?php     //文件头...     header("Content-type: image/png");     //创建真彩色白纸     $im = @imagecreatetruecolor(50, 20) or die("建立图像失败");     //获取背景颜色     $background_color = imagecolorallocate($im, 255, 255, 255);     //填充背景颜色(这

  • PHP生成图片验证码、点击切换实例

    这里来看下效果: 现在让我们来看下 PHP 代码 复制代码 代码如下: <?php   session_start(); function random($len) {     $srcstr = "1a2s3d4f5g6hj8k9qwertyupzxcvbnm";     mt_srand();     $strs = "";     for ($i = 0; $i < $len; $i++) {         $strs .= $srcstr[mt

  • PHP 绘制网站登录首页图片验证码

    几乎所有的网站登录页都会有验证码,验证码是一种安全保护机制,在注册时要求必须有人工操作进行验证,用于防止垃圾注册机大量注册用户账号占用服务器内存从而使服务器瘫痪. 图片验证码的实现十分简单.首先从指定字符集合中随机抽取固定数目的字符,以一种不规则的方法画在画布上,再适当添加一些干扰点和干扰元素,最后将图片输出,一张崭新的验证码就完成了. 先给大家展示下生成的验证码: 点击刷新: 如果大家对实现效果非常满意,请继续往下看. 前端代码如下: <!DOCTYPE html> <html>

  • PHP生成Gif图片验证码

    先看效果图  字体及字体文件的路径需要在类中$FontFilePath及$FontFileName中设置.如: 复制代码 代码如下: private static $FontFilePath = "static/font/"; //相对地本代码文件的位置private static $FontFileName = array("3.ttf");// array("1.ttf", "2.ttf", "3.ttf&quo

  • php生成图片验证码-附五种验证码

    以前输出验证码的时候用过一个方法,在前台用JS生成验证码字符串,再传递到后台用PHP输出验证码图像.这样在验证时就不需要使用$_SESSION传递验证码的值,直接用JS比较生成的字符串和输入的字符串是否相等即可. 本文以实例演示5种验证码,并介绍生成验证码的函数.PHP生成验证码的原理:通过GD库,生成一张带验证码的图片,并将验证码保存在Session中. 1.HTML 5中验证码HTML代码如下: <div class="demo"> <h3>1.数字验证码&

  • php5 图片验证码实现代码

    GD库的函数 1,imagecreatetruecolor -----创建一个真彩色的图像 imagecreatetruecolor(int x_size,int y_size) //x表示宽,y表示高 2,imagecolorallocate 为一幅图像分配颜色(调色板) imagecolorallocate(resource image,int red,int green,int blue)//red,green,blue----三原色 3,imagestring 绘图函数 iamgestr

  • PHP图片验证码制作实现分享(全)

    就如今天遇到随即函数rand();脑海中想到用它做点啥好呢,最后想起了验证码,数字验证码,字母验证码,中文验证码,可是自己不会呀,咋办呢,上网搜,看别人的代码,开不懂,看视频,听老师讲,将其中所遇到的函数,值得注意的地方都拿笔记下,平常看到一般网页上的随机验证码都是以一定的方框包围起来,貌似就是以图片为背景的.经过边看,自己边敲,虽然遇到很多不会的问题,但是我相信只要自己脚踏实地,一定学会的.现在想做一下总结,自己可能写的很乱,可我相信有一天会实现的.1.产生数字的随机数 -->创建图片-->

  • 如何用php生成扭曲及旋转的验证码图片

    复制代码 代码如下: <?php function make_rand($length="32"){//验证码文字生成函数         $str="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";     $result="";     for($i=0;$i<$length;$i++){         $num[$i]=rand(0,61);   

随机推荐