php生成图片与验证码图片生成原理

 更新时间:2016年11月25日 16:58  点击:1188
这款php生成图片与验证码图片生成原理代码,是由php gd库来支持,如果你的系统不能创建图片就把gd.dll前面的;去再,重起apache,如果是iis重起iis就OK了。

<?

 代码如下 复制代码
$w?$RESIZEWIDTH=$w:$RESIZEWIDTH=400;// 生成图片的宽度
$h?$RESIZEHEIGHT=$h:$RESIZEHEIGHT=400;// 生成图片的高度
function ResizeImage($im,$maxwidth,$maxheight,$name){
    $width = imagesx($im);
    $height = imagesy($im);
    if(($maxwidth && $width > $maxwidth) || ($maxheight && $height > $maxheight)){
        if($maxwidth && $width > $maxwidth){
            $widthratio = $maxwidth/$width;
            $RESIZEWIDTH=true;//www.111cn.net
        }
        if($maxheight && $height > $maxheight){
            $heightratio = $maxheight/$height;
            $RESIZEHEIGHT=true;
        }
        if($RESIZEWIDTH && $RESIZEHEIGHT){
            if($widthratio < $heightratio){
                $ratio = $widthratio;
            }else{
                $ratio = $heightratio;
            }
        }elseif($RESIZEWIDTH){
            $ratio = $widthratio;
        }elseif($RESIZEHEIGHT){
            $ratio = $heightratio;
        }
        $newwidth = $width * $ratio;
        $newheight = $height * $ratio;
        if(function_exists("imagecopyresampled")){
              $newim = imagecreatetruecolor($newwidth, $newheight);
              imagecopyresampled($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
        }else{
            $newim = imagecreate($newwidth, $newheight);
              imagecopyresized($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
        }
        ImageJpeg ($newim,$name);
        ImageDestroy ($newim);
    }else{
        ImageJpeg ($im,$name);
    }
}
 代码如下 复制代码
if($_FILES['uploadfile']['size']){
    if($_FILES['uploadfile']['type'] == "image/pjpeg"){
        $im = imagecreatefromjpeg($_FILES['uploadfile']['tmp_name']);
    }elseif($_FILES['uploadfile']['type'] == "image/x-png"){
        $im = imagecreatefrompng($_FILES['uploadfile']['tmp_name']);
    }elseif($_FILES['uploadfile']['type'] == "image/gif"){
        $im = imagecreatefromgif($_FILES['uploadfile']['tmp_name']);
    }
    if($im){
        if(file_exists('bbs.jpg')){
            unlink('www.111cn.net.jpg');
        }
        ResizeImage($im,$RESIZEWIDTH,$RESIZEHEIGHT,'bbs.jpg');
        ImageDestroy ($im);
  
    }
}
//$uploadfile="bbs.jpg";

?>

创建图片后记得用ImageDestroy 清空内存哦。

下面关于php 3D饼图类绘制类函数实现原理是根据//椭圆长半轴 等参数绘制一个3D饼图形的代码。
 代码如下 复制代码

class chart{

var $a; //椭圆长半轴
var $b; //椭圆短半轴
var $DataArray;  //每个扇形的数据
var $ColorArray; //每个扇形的颜色 要求按照十六进制书写但前面不加0x
//为边缘及阴影为黑色

function chart($pa=100,$pb=60,$sData="100,200,300,400,500,300", $sColor="ee00ff,dd0000,cccccc,ccff00,00ccff,ccff00")
{
    $this->a=$pa;
    $this->b=$pb;
    $this->DataArray=split(",",$sData);
    $this->ColorArray=split(",",$sColor);
}

function setA($v){
    $this->a=$v;
}

function getA(){
    return $this->a;
}

function setB($v){
    $this->b=$v; 
}

function getB(){
    return $this->b;
}

function setDataArray($v){
    $this->DataArray=split(",",$v);
}

function getDataArray($v){
    return $this->DataArray;
}

function setColorArray($v){
    $this->ColorArray=split(",",$v);
}

function getColorArray(){
    return  $this->ColorArray;
}

 
function  DrawPie(){
    $image=imagecreate($this->a*2+40,$this->b*2+40);
    $PieCenterX=$this->a+10;
    $PieCenterY=$this->b+10;
    $DoubleA=$this->a*2;
    $DoubleB=$this->b*2;
    list($R,$G,$B)=getRGB(0);
    $colorBorder=imagecolorallocate($image,$R,$G,$B);
    $DataNumber=count($this->DataArray);
    
    //$DataTotal
    for($i=0;$i<$DataNumber;$i++)      $DataTotal+=$this->DataArray[$i]; //算出数据和
    
    //填充背境
    imagefill($image, 0, 0, imagecolorallocate($image, 0xFF, 0xFF, 0xFF));

    /*
    ** 画每一个扇形
    */
    $Degrees = 0;
    for($i = 0; $i < $DataNumber; $i++){
        $StartDegrees = round($Degrees);
        $Degrees += (($this->DataArray[$i]/$DataTotal)*360);
        $EndDegrees = round($Degrees);
        $percent = number_format($this->DataArray[$i]/$DataTotal*100, 1); 
        list($R,$G,$B)=getRGB(hexdec($this->ColorArray[$i]));
        $CurrentColor=imagecolorallocate($image,$R,$G,$B);
        if ($R>60 and $R<256)            $R=$R-60;
        if ($G>60 and $G<256)            $G=$G-60;
        if ($B>60 and $B<256)            $B=$B-60;
        $CurrentDarkColor=imagecolorallocate($image,$R,$G,$B);
        //画扇形弧
        imagearc($image,$PieCenterX,$PieCenterY,$DoubleA,$DoubleB,$StartDegrees,$EndDegrees,$CurrentColor);
        //画直线
        list($ArcX, $ArcY) = pie_point($StartDegrees , $this->a , $this->b);
        imageline($image,$PieCenterX,$PieCenterY,floor($PieCenterX + $ArcX),floor($PieCenterY + $ArcY),$CurrentColor);
        //画直线
        list($ArcX, $ArcY) = pie_point($EndDegrees,$this->a , $this->b);
        imageline($image,$PieCenterX,$PieCenterY,ceil($PieCenterX + $ArcX),ceil($PieCenterY + $ArcY),$CurrentColor);
        //填充扇形
        $MidPoint = round((($EndDegrees - $StartDegrees)/2) + $StartDegrees);
        list($ArcX, $ArcY) = Pie_point($MidPoint, $this->a*3/4 , $this->b*3/4);
        
        imagefilltoborder($image,floor($PieCenterX + $ArcX),floor($PieCenterY + $ArcY), $CurrentColor,$CurrentColor);
        imagestring($image,2,floor($PieCenterX + $ArcX-5),floor($PieCenterY + $ArcY-5),$percent."%",$colorBorder);

        //画阴影
        if ($StartDegrees>=0 and $StartDegrees<=180){
           if($EndDegrees<=180){    
               for($k = 1; $k < 15; $k++)
                imagearc($image,$PieCenterX, $PieCenterY+$k,$DoubleA, $DoubleB, $StartDegrees, $EndDegrees, $CurrentDarkColor);
           }else{
               for($k = 1; $k < 15; $k++)
                imagearc($image,$PieCenterX, $PieCenterY+$k,$DoubleA, $DoubleB, $StartDegrees, 180, $CurrentDarkColor);
           }

        }
   }
        
    /*到此脚本已经生了一幅图像了
    **现在需要的是把它发到浏览器上,重要的一点是要将标头发给浏览器,让它知道是一个GIF文件。不然的话你只能看到一堆奇怪的乱码
    */ 
    //输出生成的图片    
    header("Content-type: image/gif");
    imagegif($image);
    imagedestroy($image);
}//End drawPie()
}//End class


//实现

$objp = new chart();
$objp->DrawPie();

 代码如下 复制代码

<?php教程
header("Content-type: image/jpeg");
// 载入图像
$imagen1 = imagecreatefromjpeg("imagen1.jpg");
$imagen2 = imagecreatefromjpeg("imagen2.jpg");

// 复制图像www.111cn.net
imagecopy($imagen1,$imagen2,0,0,0,0,200,150);

// 输出jpeg图像
imagejpeg($imagen1);

//释放内存
imagedestroy($imagen2);
imagedestroy($imagen1);

?>

获取图片属性代码

 代码如下 复制代码

<?php
$info = getimagesize("imagen2.jpg");
print_r($info);

?>

生成png图片的php代码

 代码如下 复制代码

<?php
//PNG格式图像处理函数
function Loadpng ($imgname) {
    $im = @ImageCreateFromPNG ($imgname);
    if (!$im) {    //载入图像失败                     
        $im = ImageCreate (400, 30);     
        $bgc = ImageColorAllocate ($im, 255, 255, 255);
        $tc  = ImageColorAllocate ($im, 0, 0, 0);
       ImageFilledRectangle ($im, 0, 0, 150, 30, $bgc);
       ImageString($im, 4, 5, 5, "Error loading: $imgname", $tc);
    }
    return $im;
 }
 $imgPng=Loadpng("./karte.png");
   /* 输出图像到浏览器 */
 header("Content-type: image/png");
 imagePng($imgPng);
 ?>

关于php生成图形验证码我们讲过很多,这是一个非常简单的适合于php入门者的教程,一个本的用php生成验证码的实例代码。

<?php
$height = 300;
$width = 300;
//创建背景图
$im = ImageCreateTrueColor($width, $height);
//分配颜色
$white = ImageColorAllocate ($im, 255, 255, 255);
$blue = ImageColorAllocate ($im, 0, 0, 64);
//绘制颜色至图像中
ImageFill($im, 0, 0, $blue);
//绘制字符串:Hello,PHP
ImageString($im, 10, 100, 120, 'Hello,PHP', $white);
//输出图像,定义头
Header ('Content-type: image/png');
//将图像发送至浏览器
ImagePng($im);
//清除资源
ImageDestroy($im);
?>

function image3($length=4,$type='png',$width=180,$height=60,$fontface='fpnf.ttf',$verifyName='verify') {
        $code = $this->rand_string($length,4);
        $width = ($length*25)>$width?$length*25:$width;
        $authCode = new Zend_Session_Namespace('Auth_Code');
        $authCode->imagecode = $randval;
       
        $im=imagecreatetruecolor($width,$height);
        $borderColor = imagecolorallocate($im, 100, 100, 100);                    //边框色
        $bkcolor=imagecolorallocate($im,250,250,250);
        imagefill($im,0,0,$bkcolor);
        @imagerectangle($im, 0, 0, $width-1, $height-1, $borderColor);
        // 干扰
        for($i=0;$i<15;$i++){
            $fontcolor=imagecolorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
            imagearc($im,mt_rand(-10,$width),mt_rand(-10,$height),mt_rand(30,300),mt_rand(20,200),55,44,$fontcolor);
        }
        for($i=0;$i<255;$i++){
            $fontcolor=imagecolorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
            imagesetpixel($im,mt_rand(0,$width),mt_rand(0,$height),$fontcolor);
        }
        if(!is_file($fontface)) {
            $fontface = dirname(__FILE__)."/".$fontface;
        }
        for($i=0;$i<$length;$i++){
            $fontcolor=imagecolorallocate($im,mt_rand(0,120),mt_rand(0,120),mt_rand(0,120)); //这样保证随机出来的颜色较深。
            $codex= substr($code,$i,1);
            imagettftext($im,mt_rand(16,20),mt_rand(-60,60),40*$i+20,mt_rand(30,35),$fontcolor,$fontface,$codex);
        }
        $this->output($im,$type);
    }
   
    function output($im,$type='png')
    {
        header("Content-type: image/".$type);
        $ImageFun='Image'.$type;
        $ImageFun($im);
        imagedestroy($im);
    }

[!--infotagslink--]

相关文章

  • PHP 验证码不显示只有一个小红叉的解决方法

    最近想自学PHP ,做了个验证码,但不知道怎么搞的,总出现一个如下图的小红叉,但验证码就是显示不出来,原因如下 未修改之前,出现如下错误; (1)修改步骤如下,原因如下,原因是apache权限没开, (2)点击打开php.int., 搜索extension=ph...2013-10-04
  • JS实现随机生成验证码

    这篇文章主要为大家详细介绍了JS实现随机生成验证码,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-09-06
  • php二维码生成

    本文介绍两种使用 php 生成二维码的方法。 (1)利用google生成二维码的开放接口,代码如下: /** * google api 二维码生成【QRcode可以存储最多4296个字母数字类型的任意文本,具体可以查看二维码数据格式】 * @param strin...2015-10-21
  • Java生成随机姓名、性别和年龄的实现示例

    这篇文章主要介绍了Java生成随机姓名、性别和年龄的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-10-01
  • jQuery Real Person验证码插件防止表单自动提交

    本文介绍的jQuery插件有点特殊,防自动提交表单的验证工具,就是我们经常用到的验证码工具,先给大家看看效果。效果图如下: 使用说明 需要使用jQuery库文件和Real Person库文件 同时需要自定义验证码显示的CSS样式 使用实例...2015-11-08
  • C#生成随机数功能示例

    这篇文章主要介绍了C#生成随机数功能,涉及C#数学运算与字符串操作相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25
  • jQuery为动态生成的select元素添加事件的方法

    下面小编就为大家带来一篇jQuery为动态生成的select元素添加事件的方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2016-09-01
  • php生成唯一数字id的方法汇总

    关于生成唯一数字ID的问题,是不是需要使用rand生成一个随机数,然后去数据库查询是否有这个数呢?感觉这样的话有点费时间,有没有其他方法呢?当然不是,其实有两种方法可以解决。 1. 如果你只用php而不用数据库的话,那时间戳+随...2015-11-24
  • PHP自动生成后台导航网址的最佳方法

    经常制作开发不同的网站的后台,写过很多种不同的后台导航写法。 最终积累了这种最写法,算是最好的吧...2013-09-29
  • php实现点击可刷新验证码

    验证码类文件 CreateImg.class.php <&#63;php class ValidationCode { private $width,$height,$codenum; public $checkcode; //产生的验证码 private $checkimage; //验证码图片 private $disturbColor = ''; /...2015-11-08
  • Jquery插件实现点击获取验证码后60秒内禁止重新获取

    通过jquery.cookie.js插件可以快速实现“点击获取验证码后60秒内禁止重新获取(防刷新)”的功能效果图:先到官网(http://plugins.jquery.com/cookie/)下载cookie插件,放到相应文件夹,代码如下:复制代码 代码如下: <!DOCTYPE ht...2015-03-15
  • 基于JavaScript实现验证码功能

    这篇文章主要介绍了基于JavaScript实现验证码功能的相关资料...2017-04-03
  • js生成随机数的方法实例

    js生成随机数主要用到了内置的Math对象的random()方法。用法如:Math.random()。它返回的是一个 0 ~ 1 之间的随机数。有了这么一个方法,那生成任意随机数就好理解了。比如实际中我们可能会有如下的需要: (1)生成一个 0 - 1...2015-10-21
  • 单击按钮发送验证码,出现倒计时的简单实例

    下面小编就为大家带来一篇单击按钮发送验证码,出现倒计时的简单实例。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧 代码...2017-07-06
  • PHP验证码生成与验证例子

    验证码是一个现在WEB2.0中常见的一个功能了,像注册、登录又或者是留言页面,都需要注册码来验证当前操作者的合法性,我们会看到有些网站没有验证码,但那是更高级的验证了,...2016-11-25
  • PHP生成不同颜色、不同大小的tag标签函数

    复制代码 代码如下:function getTagStyle(){ $minFontSize=8; //最小字体大小,可根据需要自行更改 $maxFontSize=18; //最大字体大小,可根据需要自行更改 return 'font-size:'.($minFontSize+lcg_value()*(abs($maxFo...2013-10-04
  • 基于Pytorch版yolov5的滑块验证码破解思路详解

    这篇文章主要介绍了基于Pytorch版yolov5的滑块验证码破解思路详解,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-02-25
  • Bootstrap中文本框的宽度变窄并且加入一副验证码图片的实现方法

    这篇文章主要介绍了Bootstrap中文本框的宽度变窄并且加入一副验证码图片的实现方法的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下...2016-06-24
  • 工信部的ICP备案网站登录时验证码一直输入不正确怎么回事

    工信部的ICP备案网站登录时验证码一直输入不正确怎么回事,为了防止一些机器采集人工信部对于查询验证做得识别度极低,所以许多的朋友都会发现输入验证码一直有问题了,那...2016-10-10
  • php中利用str_pad函数生成数字递增形式的产品编号

    解决办法:$str=”QB”.str_pad(($maxid[0]["max(id)"]+1),5,”0″,STR_PAD_LEFT ); 其中$maxid[0]["max(id)"]+1) 是利用max函数从数据库中找也ID最大的一个值, ID为主键,不会重复。 str_pad() 函数把字符串填充为指...2013-10-04