PHP生成GIF动态图片验证码

 更新时间:2016年11月25日 16:56  点击:1307
ImageCode函数通过GIFEncoder类实现的GIF动画的PHP源代码,有兴趣的朋友可以研究一下。
 代码如下 复制代码

<?php
/**
 * 调用示例
 * */
session_start();
$randCode = '';
$chars = 'abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPRSTUVWXYZ23456789';
for ( $i = 0; $i < 4; $i++ )
{
 $randCode .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
}
$_SESSION['code'] = strtoupper($randCode);   // 记录session
ImageCode($randCode, 60);    // 显示GIF动画

/**
 * ImageCode 生成GIF图片验证
 * @param $string 字符串
 * @param $width 宽度
 * @param $height 高度
 * */
function ImageCode($string = '', $width = 75, $height = 25)
{
 $authstr = $string ? $string : ((time() % 2 == 0) ? mt_rand(1000, 9999) : mt_rand(10000, 99999));
 
 $board_width = $width;
 $board_height = $height;
 // 生成一个32帧的GIF动画
 for($i = 0; $i < 32; $i++)
 {
  ob_start();
     $image = imagecreate($board_width, $board_height);
     imagecolorallocate($image, 0,0,0);
     // 设定文字颜色数组
   $colorList[] = ImageColorAllocate($image, 15,73,210);
   $colorList[] = ImageColorAllocate($image, 0,64,0);
   $colorList[] = ImageColorAllocate($image, 0,0,64);
   $colorList[] = ImageColorAllocate($image, 0,128,128);
   $colorList[] = ImageColorAllocate($image, 27,52,47);
   $colorList[] = ImageColorAllocate($image, 51,0,102);
   $colorList[] = ImageColorAllocate($image, 0,0,145);
   $colorList[] = ImageColorAllocate($image, 0,0,113);
   $colorList[] = ImageColorAllocate($image, 0,51,51);
   $colorList[] = ImageColorAllocate($image, 158,180,35);
   $colorList[] = ImageColorAllocate($image, 59,59,59);
   $colorList[] = ImageColorAllocate($image, 0,0,0);
   $colorList[] = ImageColorAllocate($image, 1,128,180);
   $colorList[] = ImageColorAllocate($image, 0,153,51);
   $colorList[] = ImageColorAllocate($image, 60,131,1);
   $colorList[] = ImageColorAllocate($image, 0,0,0);
   $fontcolor = ImageColorAllocate($image, 0,0,0);
   $gray = ImageColorAllocate($image, 245,245,245);
   
     $color = imagecolorallocate($image, 255,255,255);
     $color2 = imagecolorallocate($image, 255,0,0);
    
     imagefill($image, 0, 0, $gray);
    
     $space = 15;  // 字符间距
     if($i > 0)   // 屏蔽第一帧
     {
      for ($k = 0; $k < strlen($authstr); $k++)
      {
    $colorRandom = mt_rand(0,sizeof($colorList)-1);
    $float_top = rand(0,4);
    $float_left = rand(0,3);
    imagestring($image, 6, $space * $k, $top + $float_top, substr($authstr, $k, 1), $colorList[$colorRandom]);
   }
     }
    
  for ($k = 0; $k < 20; $k++)
  {
   $colorRandom = mt_rand(0,sizeof($colorList)-1);
    imagesetpixel($image, rand()%70 , rand()%15 , $colorList[$colorRandom]);
 
  }
  // 添加干扰线
  for($k = 0; $k < 3; $k++)
  {
   $colorRandom = mt_rand(0, sizeof($colorList)-1);
   // $todrawline = rand(0,1);
   $todrawline = 1;
   if($todrawline)
   {
    imageline($image, mt_rand(0, $board_width), mt_rand(0,$board_height), mt_rand(0,$board_width), mt_rand(0,$board_height), $colorList[$colorRandom]);
   }
   else
   {
    $w = mt_rand(0,$board_width);
    $h = mt_rand(0,$board_width);
    imagearc($image, $board_width - floor($w / 2) , floor($h / 2), $w, $h,  rand(90,180), rand(180,270), $colorList[$colorRandom]);
   }
  }
     imagegif($image);
     imagedestroy($image);
     $imagedata[] = ob_get_contents();
     ob_clean(); 
     ++$i; 
 }
 
 $gif = new GIFEncoder($imagedata); 
 Header ('Content-type:image/gif'); 
 echo $gif->GetAnimation(); 
}


/**
 * GIFEncoder类
 * */
Class GIFEncoder
{
    var $GIF = "GIF89a";                /* GIF header 6 bytes        */ 
    var $VER = "GIFEncoder V2.06";        /* Encoder version             */ 

    var $BUF = Array ( ); 
    var $LOP =  0; 
    var $DIS =  2; 
    var $COL = -1; 
    var $IMG = -1; 

    var $ERR = Array ( 
        'ERR00' =>"Does not supported function for only one image!", 
        'ERR01' =>"Source is not a GIF image!", 
        'ERR02' =>"Unintelligible flag ", 
        'ERR03' =>"Could not make animation from animated GIF source", 
    ); 
   
    function GIFEncoder ($GIF_src, $GIF_dly = 100, $GIF_lop = 0, $GIF_dis = 0,  $GIF_red = 0, $GIF_grn = 0, $GIF_blu = 0, $GIF_mod = 'bin' )
    { 
        if (!is_array($GIF_src) && !is_array($GIF_tim))
        { 
            printf ( "%s: %s", $this->VER, $this->ERR['ERR00']); 
            exit( 0 );
        } 
        $this->LOP = ($GIF_lop > -1) ? $GIF_lop : 0; 
        $this->DIS = ($GIF_dis > -1) ? (( $GIF_dis < 3 ) ? $GIF_dis : 3) : 2; 
        $this->COL = ($GIF_red > -1 && $GIF_grn > -1 && $GIF_blu > -1) ? ($GIF_red | ($GIF_grn << 8) | ($GIF_blu << 16)) : -1; 

        for ($i = 0, $src_count = count($GIF_src); $i < $src_count; $i++ )
        {
            if (strToLower( $GIF_mod ) == "url")
            { 
             $this->BUF[] = fread (fopen($GIF_src [$i], "rb"), filesize ($GIF_src [$i])); 
            } 
            elseif(strToLower($GIF_mod) == "bin")
            { 
                $this->BUF [ ] = $GIF_src [ $i ]; 
            } 
            else
            { 
                printf("%s: %s ( %s )!", $this->VER, $this->ERR [ 'ERR02' ], $GIF_mod); 
                exit(0); 
            } 
            if (substr($this->BUF[$i], 0, 6) != "GIF87a" && substr($this->BUF [$i], 0, 6) != "GIF89a")
            { 
                printf( "%s: %d %s", $this->VER, $i, $this->ERR ['ERR01']); 
                exit(0); 
            } 
            for ($j = (13 + 3 * (2 << (ord($this->BUF[$i]{10}) & 0x07 ))), $k = TRUE; $k; $j++)
            { 
                switch ($this->BUF [$i]{$j})
                { 
                    case "!": 
                        if ((substr($this->BUF[$i], ($j + 3), 8)) == "NETSCAPE")
                        { 
                                printf( "%s: %s ( %s source )!", $this->VER, $this->ERR ['ERR03'], ($i + 1)); 
                                exit( 0 ); 
                        } 
                        break; 
                    case ";": 
                        $k = FALSE; 
                        break; 
                } 
            } 
        } 
        GIFEncoder::GIFAddHeader(); 
        for($i = 0, $count_buf = count($this->BUF); $i < $count_buf; $i++)
        { 
         GIFEncoder::GIFAddFrames($i, $GIF_dly[$i]); 
        } 
        GIFEncoder::GIFAddFooter(); 
    } 
   
    function GIFAddHeader ( )
    { 
  $cmap = 0; 
  
  if (ord($this->BUF[0]{10}) & 0x80 )
  { 
   $cmap = 3 * ( 2 << ( ord ( $this->BUF [ 0 ]{10} ) & 0x07 )); 
   
   $this->GIF .= substr ( $this->BUF [ 0 ], 6, 7); 
   $this->GIF .= substr ( $this->BUF [ 0 ], 13, $cmap); 
   $this->GIF .= "!37713NETSCAPE2.031" . GIFEncoder::GIFWord ( $this->LOP ) . ""; 
  } 
    } 
   
    function GIFAddFrames ( $i, $d )
    { 

        $Locals_str = 13 + 3 * (2 <<(ord($this->BUF[$i]{10}) & 0x07)); 

        $Locals_end = strlen($this->BUF[$i]) - $Locals_str - 1; 
        $Locals_tmp = substr ($this->BUF[$i], $Locals_str, $Locals_end); 

        $Global_len = 2 << (ord( $this->BUF [0]{10} ) & 0x07 ); 
        $Locals_len = 2 << (ord( $this->BUF[$i]{10}) & 0x07); 

        $Global_rgb = substr($this->BUF[0], 13, 3 * (2 << ( ord ( $this->BUF[0]{10} ) & 0x07))); 
        $Locals_rgb = substr ( $this->BUF[$i], 13, 3 * (2 << ( ord ( $this->BUF[$i]{10} ) & 0x07))); 

        $Locals_ext = "!xF9x04" . chr(($this->DIS << 2) + 0) . chr(($d >> 0) & 0xFF) . chr(($d >> 8) & 0xFF) . "x0x0"; 

        if ( $this->COL > -1 && ord($this->BUF[$i]{10}) & 0x80)
        { 
            for($j = 0; $j < (2 << (ord( $this->BUF[$i]{10}) & 0x07)); $j++ )
            { 
                if(ord ($Locals_rgb{3 * $j + 0}) == ($this->COL >>  0) & 0xFF && ord ( $Locals_rgb { 3 * $j + 1 } ) == ( $this->COL >>  8 ) & 0xFF && ord ( $Locals_rgb { 3 * $j + 2 } ) == ( $this->COL >> 16 ) & 0xFF )
                { 
                    $Locals_ext = "!xF9x04" . chr(($this->DIS << 2) + 1) . chr (( $d >> 0) & 0xFF) . chr (( $d >> 8) & 0xFF) . chr ($j) . "x0"; 
                    break; 
                } 
            } 
        } 
        switch ( $Locals_tmp { 0 } )
        { 
            case "!": 
                $Locals_img = substr($Locals_tmp, 8, 10); 
                $Locals_tmp = substr($Locals_tmp, 18, strlen ($Locals_tmp) - 18); 
                break; 
            case ",": 
                $Locals_img = substr($Locals_tmp, 0, 10); 
                $Locals_tmp = substr($Locals_tmp, 10, strlen($Locals_tmp) - 10); 
                break; 
        } 
        if ( ord ( $this->BUF[$i]{10} ) & 0x80 && $this->IMG > -1 )
        { 
            if ( $Global_len == $Locals_len )
            { 
    if ( GIFEncoder::GIFBlockCompare ( $Global_rgb, $Locals_rgb, $Global_len ) )
    { 
     $this->GIF .= ( $Locals_ext . $Locals_img . $Locals_tmp ); 
    } 
    else
    { 
     $byte  = ord ( $Locals_img{9}); 
     $byte |= 0x80; 
     $byte &= 0xF8; 
     $byte |= ( ord ( $this->BUF [ 0 ]{10}) & 0x07); 
     $Locals_img{9} = chr($byte); 
     $this->GIF .= ($Locals_ext . $Locals_img . $Locals_rgb . $Locals_tmp); 
    } 
            } 
            else
            { 
                $byte  = ord($Locals_img{9}); 
                $byte |= 0x80; 
                $byte &= 0xF8; 
                $byte |= (ord($this->BUF[$i]{10}) & 0x07); 
                $Locals_img {9} = chr($byte); 
                $this->GIF .= ($Locals_ext . $Locals_img . $Locals_rgb . $Locals_tmp); 
            } 
        } 
        else
        { 
            $this->GIF .= ( $Locals_ext . $Locals_img . $Locals_tmp ); 
        } 
        $this->IMG  = 1; 
    } 
   
    function GIFAddFooter ( ) { 
        $this->GIF .= ";"; 
    } 
   
    function GIFBlockCompare ( $GlobalBlock, $LocalBlock, $Len )
    { 
        for ( $i = 0; $i < $Len; $i++ )
        { 
            if($GlobalBlock { 3 * $i + 0 } != $LocalBlock { 3 * $i + 0 } || $GlobalBlock { 3 * $i + 1 } != $LocalBlock { 3 * $i + 1 } || $GlobalBlock { 3 * $i + 2 } != $LocalBlock{3 * $i + 2})
            { 
                return ( 0 ); 
            } 
        } 
        return ( 1 ); 
    } 
   
    function GIFWord ( $int )
    { 
       return ( chr ( $int & 0xFF ) . chr ( ( $int >> 8 ) & 0xFF ) ); 
    } 
   
    function GetAnimation ( )
    { 
        return ($this->GIF); 
    } 
}

缩略图用得最多的是我们上传图片时生成一张小图,这样就可以很好的解决图片大小或影响网站整体美观的问题了,下面介绍的生成图片缩略图类都不支持图片上传,我们要先上传再调用此类来操作。

//使用如下类就可以生成图片缩略图,

 代码如下 复制代码

<?php
class resizeimage
{
    //图片类型
    var $type;
    //实际宽度
    var $width;
    //实际高度
    var $height;
    //改变后的宽度
    var $resize_width;
    //改变后的高度
    var $resize_height;
    //是否裁图
    var $cut;
    //源图象
    var $srcimg;
    //目标图象地址
    var $dstimg;
    //临时创建的图象
    var $im;

    function resizeimage($img, $wid, $hei,$c,$dstpath)
    {
        $this->srcimg = $img;
        $this->resize_width = $wid;
        $this->resize_height = $hei;
        $this->cut = $c;
        //图片的类型
  
$this->type = strtolower(substr(strrchr($this->srcimg,"."),1));

        //初始化图象
        $this->initi_img();
        //目标图象地址
        $this -> dst_img($dstpath);
        //--
        $this->width = imagesx($this->im);
        $this->height = imagesy($this->im);
        //生成图象
        $this->newimg();
        ImageDestroy ($this->im);
    }
    function newimg()
    {
        //改变后的图象的比例
        $resize_ratio = ($this->resize_width)/($this->resize_height);
        //实际图象的比例
        $ratio = ($this->width)/($this->height);
        if(($this->cut)=="1")
        //裁图
        {
            if($ratio>=$resize_ratio)
            //高度优先
            {
                $newimg = imagecreatetruecolor($this->resize_width,$this->resize_height);
                imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width,$this-

>resize_height, (($this->height)*$resize_ratio), $this->height);
                ImageJpeg ($newimg,$this->dstimg);
            }
            if($ratio<$resize_ratio)
            //宽度优先
            {
                $newimg = imagecreatetruecolor($this->resize_width,$this->resize_height);
                imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, $this-

>resize_height, $this->width, (($this->width)/$resize_ratio));
                ImageJpeg ($newimg,$this->dstimg);
            }
        }
        else
        //不裁图
        {
            if($ratio>=$resize_ratio)
            {
                $newimg = imagecreatetruecolor($this->resize_width,($this->resize_width)/$ratio);
                imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, ($this-

>resize_width)/$ratio, $this->width, $this->height);
                ImageJpeg ($newimg,$this->dstimg);
            }
            if($ratio<$resize_ratio)
            {
                $newimg = imagecreatetruecolor(($this->resize_height)*$ratio,$this->resize_height);
                imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, ($this->resize_height)*$ratio,

$this->resize_height, $this->width, $this->height);
                ImageJpeg ($newimg,$this->dstimg);
            }
        }
    }
    //初始化图象
    function initi_img()
    {
        if($this->type=="jpg")
        {
            $this->im = imagecreatefromjpeg($this->srcimg);
        }
        if($this->type=="gif")
        {
            $this->im = imagecreatefromgif($this->srcimg);
        }
        if($this->type=="png")
        {
            $this->im = imagecreatefrompng($this->srcimg);
        }
    }
    //图象目标地址
    function dst_img($dstpath)
    {
        $full_length  = strlen($this->srcimg);

        $type_length  = strlen($this->type);
        $name_length  = $full_length-$type_length;


        $name         = substr($this->srcimg,0,$name_length-1);
        $this->dstimg = $dstpath;


//echo $this->dstimg;
    }
}
?>

类的调用方法

$resizeimage = new resizeimage("图片源文件地址", "200", "100", "0","缩略图地址");

//就只用上面的一句话,就能生成缩略图,其中,源文件和缩略图地址可以相同,200,100分别代表宽和高

实例2

PHP缩略图 等比例无损压缩,可填充空白区域补充色

 代码如下 复制代码

<?php
error_reporting( E_ALL );

// 测试
imagezoom('1.jpg', '2.jpg', 400, 300, '#FFFFFF');

/*
    php缩略图函数:
        等比例无损压缩,可填充补充色 author: 华仔
    主持格式:
        bmp 、jpg 、gif、png
    param:
        @srcimage : 要缩小的图片
        @dstimage : 要保存的图片
        @dst_width: 缩小宽
        @dst_height: 缩小高
        @backgroundcolor: 补充色  如:#FFFFFF  支持 6位  不支持3位
*/
function imagezoom( $srcimage, $dstimage,  $dst_width, $dst_height, $backgroundcolor ) {
       
        // 中文件名乱码
        if ( PHP_OS == 'WINNT' ) {
                $srcimage = iconv('UTF-8', 'GBK', $srcimage);
                $dstimage = iconv('UTF-8', 'GBK', $dstimage);
        }
   
    $dstimg = imagecreatetruecolor( $dst_width, $dst_height );
    $color = imagecolorallocate($dstimg
        , hexdec(substr($backgroundcolor, 1, 2))
        , hexdec(substr($backgroundcolor, 3, 2))
        , hexdec(substr($backgroundcolor, 5, 2))
    );
    imagefill($dstimg, 0, 0, $color);
   
    if ( !$arr=getimagesize($srcimage) ) {
                echo "要生成缩略图的文件不存在";
                exit;
        }
       
    $src_width = $arr[0];
    $src_height = $arr[1];
    $srcimg = null;
    $method = getcreatemethod( $srcimage );
    if ( $method ) {
        eval( '$srcimg = ' . $method . ';' );
    }
   
    $dst_x = 0;
    $dst_y = 0;
    $dst_w = $dst_width;
    $dst_h = $dst_height;
    if ( ($dst_width / $dst_height - $src_width / $src_height) > 0 ) {
        $dst_w = $src_width * ( $dst_height / $src_height );
        $dst_x = ( $dst_width - $dst_w ) / 2;
    } elseif ( ($dst_width / $dst_height - $src_width / $src_height) < 0 ) {
        $dst_h = $src_height * ( $dst_width / $src_width );
        $dst_y = ( $dst_height - $dst_h ) / 2;
    }

    imagecopyresampled($dstimg, $srcimg, $dst_x
        , $dst_y, 0, 0, $dst_w, $dst_h, $src_width, $src_height);
   
    // 保存格式
    $arr = array(
        'jpg' => 'imagejpeg'
        , 'jpeg' => 'imagejpeg'
        , 'png' => 'imagepng'
        , 'gif' => 'imagegif'
        , 'bmp' => 'imagebmp'
    );
    $suffix = strtolower( array_pop(explode('.', $dstimage ) ) );
    if (!in_array($suffix, array_keys($arr)) ) {
        echo "保存的文件名错误";
        exit;
    } else {
        eval( $arr[$suffix] . '($dstimg, "'.$dstimage.'");' );
    }
   
    imagejpeg($dstimg, $dstimage);
   
    imagedestroy($dstimg);
    imagedestroy($srcimg);
   
}


function getcreatemethod( $file ) {
        $arr = array(
                '474946' => "imagecreatefromgif('$file')"
                , 'FFD8FF' => "imagecreatefromjpeg('$file')"
                , '424D' => "imagecreatefrombmp('$file')"
                , '89504E' => "imagecreatefrompng('$file')"
        );
        $fd = fopen( $file, "rb" );
        $data = fread( $fd, 3 );
       
        $data = str2hex( $data );
       
        if ( array_key_exists( $data, $arr ) ) {
                return $arr[$data];
        } elseif ( array_key_exists( substr($data, 0, 4), $arr ) ) {
                return $arr[substr($data, 0, 4)];
        } else {
                return false;
        }
}

function str2hex( $str ) {
        $ret = "";
       
        for( $i = 0; $i < strlen( $str ) ; $i++ ) {
                $ret .= ord($str[$i]) >= 16 ? strval( dechex( ord($str[$i]) ) )
                        : '0'. strval( dechex( ord($str[$i]) ) );
        }
       
        return strtoupper( $ret );
}

// BMP 创建函数  php本身无
function imagecreatefrombmp($filename)
{
   if (! $f1 = fopen($filename,"rb")) return FALSE;

   $FILE = unpack("vfile_type/Vfile_size/Vreserved/Vbitmap_offset", fread($f1,14));
   if ($FILE['file_type'] != 19778) return FALSE;

   $BMP = unpack('Vheader_size/Vwidth/Vheight/vplanes/vbits_per_pixel'.
                 '/Vcompression/Vsize_bitmap/Vhoriz_resolution'.
                 '/Vvert_resolution/Vcolors_used/Vcolors_important', fread($f1,40));
   $BMP['colors'] = pow(2,$BMP['bits_per_pixel']);
   if ($BMP['size_bitmap'] == 0) $BMP['size_bitmap'] = $FILE['file_size'] - $FILE['bitmap_offset'];
   $BMP['bytes_per_pixel'] = $BMP['bits_per_pixel']/8;
   $BMP['bytes_per_pixel2'] = ceil($BMP['bytes_per_pixel']);
   $BMP['decal'] = ($BMP['width']*$BMP['bytes_per_pixel']/4);
   $BMP['decal'] -= floor($BMP['width']*$BMP['bytes_per_pixel']/4);
   $BMP['decal'] = 4-(4*$BMP['decal']);
   if ($BMP['decal'] == 4) $BMP['decal'] = 0;
  
   $PALETTE = array();
   if ($BMP['colors'] < 16777216)
   {
    $PALETTE = unpack('V'.$BMP['colors'], fread($f1,$BMP['colors']*4));
   }
  
   $IMG = fread($f1,$BMP['size_bitmap']);
   $VIDE = chr(0);

   $res = imagecreatetruecolor($BMP['width'],$BMP['height']);
   $P = 0;
   $Y = $BMP['height']-1;
   while ($Y >= 0)
   {
        $X=0;
        while ($X < $BMP['width'])
        {
         if ($BMP['bits_per_pixel'] == 24)
            $COLOR = unpack("V",substr($IMG,$P,3).$VIDE);
         elseif ($BMP['bits_per_pixel'] == 16)
         { 
            $COLOR = unpack("n",substr($IMG,$P,2));
            $COLOR[1] = $PALETTE[$COLOR[1]+1];
         }
         elseif ($BMP['bits_per_pixel'] == 8)
         { 
            $COLOR = unpack("n",$VIDE.substr($IMG,$P,1));
            $COLOR[1] = $PALETTE[$COLOR[1]+1];
         }
         elseif ($BMP['bits_per_pixel'] == 4)
         {
            $COLOR = unpack("n",$VIDE.substr($IMG,floor($P),1));
            if (($P*2)%2 == 0) $COLOR[1] = ($COLOR[1] >> 4) ; else $COLOR[1] = ($COLOR[1] & 0x0F);
            $COLOR[1] = $PALETTE[$COLOR[1]+1];
         }
         elseif ($BMP['bits_per_pixel'] == 1)
         {
            $COLOR = unpack("n",$VIDE.substr($IMG,floor($P),1));
            if     (($P*8)%8 == 0) $COLOR[1] =  $COLOR[1]        >>7;
            elseif (($P*8)%8 == 1) $COLOR[1] = ($COLOR[1] & 0x40)>>6;
            elseif (($P*8)%8 == 2) $COLOR[1] = ($COLOR[1] & 0x20)>>5;
            elseif (($P*8)%8 == 3) $COLOR[1] = ($COLOR[1] & 0x10)>>4;
            elseif (($P*8)%8 == 4) $COLOR[1] = ($COLOR[1] & 0x8)>>3;
            elseif (($P*8)%8 == 5) $COLOR[1] = ($COLOR[1] & 0x4)>>2;
            elseif (($P*8)%8 == 6) $COLOR[1] = ($COLOR[1] & 0x2)>>1;
            elseif (($P*8)%8 == 7) $COLOR[1] = ($COLOR[1] & 0x1);
            $COLOR[1] = $PALETTE[$COLOR[1]+1];
         }
         else
            return FALSE;
         imagesetpixel($res,$X,$Y,$COLOR[1]);
         $X++;
         $P += $BMP['bytes_per_pixel'];
        }
        $Y--;
        $P+=$BMP['decal'];
   }
   fclose($f1);

return $res;
}
// BMP 保存函数,php本身无
function imagebmp ($im, $fn = false)
{
    if (!$im) return false;
           
    if ($fn === false) $fn = 'php://output';
    $f = fopen ($fn, "w");
    if (!$f) return false;
           
    $biWidth = imagesx ($im);
    $biHeight = imagesy ($im);
    $biBPLine = $biWidth * 3;
    $biStride = ($biBPLine + 3) & ~3;
    $biSizeImage = $biStride * $biHeight;
    $bfOffBits = 54;
    $bfSize = $bfOffBits + $biSizeImage;
           
    fwrite ($f, 'BM', 2);
    fwrite ($f, pack ('VvvV', $bfSize, 0, 0, $bfOffBits));
           
    fwrite ($f, pack ('VVVvvVVVVVV', 40, $biWidth, $biHeight, 1, 24, 0, $biSizeImage, 0, 0, 0, 0));
           
    $numpad = $biStride - $biBPLine;
    for ($y = $biHeight - 1; $y >= 0; --$y)
    {
        for ($x = 0; $x < $biWidth; ++$x)
        {
            $col = imagecolorat ($im, $x, $y);
            fwrite ($f, pack ('V', $col), 3);
        }
        for ($i = 0; $i < $numpad; ++$i)
            fwrite ($f, pack ('C', 0));
    }
    fclose ($f);
    return true;
}

?>

总结,第一个类文件不如第二个好,因为第一个生成图之后图片会有点变模糊哦,后面这个生成类是高质量的。

一个PHP智能把图片生成缩略图类,可自动根据你图片的大小生成等比例的图片哦,有需要的朋友可参考参考。
 代码如下 复制代码
< ?php
  
 /***************************************  *作者:落梦天蝎(beluckly)
 *完成时间:2006-12-18
 *类名:CreatMiniature
 *功能:生成多种类型的缩略图
 *基本参数:$srcFile,$echoType
 *方法用到的参数:
                 $toFile,生成的文件
                 $toW,生成的宽
                 $toH,生成的高
                 $bk1,背景颜色参数 以255为最高
                 $bk2,背景颜色参数
                 $bk3,背景颜色参数
  
 *例子:
  
     include("thumb.php");
     $cm=new CreatMiniature();
     $cm->SetVar("1.jpg","file");
     $cm->Distortion("dis_bei.jpg",150,200);
     $cm->Prorate("pro_bei.jpg",150,200);
     $cm->Cut("cut_bei.jpg",150,200);
      $cm->BackFill("fill_bei.jpg",150,200);
  
 ***************************************/
  
 class CreatMiniature
 {
     //公共变量
     var $srcFile="";        //原图
     var $echoType;            //输出图片类型,link--不保存为文件;file--保存为文件
     var $im="";                //临时变量
     var $srcW="";            //原图宽
     var $srcH="";            //原图高
  
     //设置变量及初始化
     function SetVar($srcFile,$echoType)
     {
         $this->srcFile=$srcFile;
         $this->echoType=$echoType;
  
         $info = "";
         $data = GetImageSize($this->srcFile,$info);
         switch ($data[2])          {
          case 1:
            if(!function_exists("imagecreatefromgif")){
             echo "你的GD库不能使用GIF格式的图片,请使用Jpeg或PNG格式!<a href='javascript:go(-1);'>返回</a>";
             exit();
            }
            $this->im = ImageCreateFromGIF($this->srcFile);
            break;
         case 2:
           if(!function_exists("imagecreatefromjpeg")){
            echo "你的GD库不能使用jpeg格式的图片,请使用其它格式的图片!<a href='javascript:go(-1);'>返回</a>";
            exit();
           }
           $this->im = ImageCreateFromJpeg($this->srcFile);    
           break;
         case 3:
           $this->im = ImageCreateFromPNG($this->srcFile);    
           break;
       }
       $this->srcW=ImageSX($this->im);
       $this->srcH=ImageSY($this->im); 
     }
     
     //生成扭曲型缩图
     function Distortion($toFile,$toW,$toH)
     {
         $cImg=$this->CreatImage($this->im,$toW,$toH,0,0,0,0,$this->srcW,$this->srcH);
         return $this->EchoImage($cImg,$toFile);
         ImageDestroy($cImg);
     }
     
     //生成按比例缩放的缩图
     function Prorate($toFile,$toW,$toH)
     {
         $toWH=$toW/$toH;
         $srcWH=$this->srcW/$this->srcH;
         if($toWH< =$srcWH)
         {
             $ftoW=$toW;
             $ftoH=$ftoW*($this->srcH/$this->srcW);
         }
      else          {
               $ftoH=$toH;
               $ftoW=$ftoH*($this->srcW/$this->srcH);
         }
         if($this->srcW>$toW||$this->srcH>$toH)
         {
             $cImg=$this->CreatImage($this->im,$ftoW,$ftoH,0,0,0,0,$this->srcW,$this->srcH);
             return $this->EchoImage($cImg,$toFile);
             ImageDestroy($cImg);
         }
         else
         {
             $cImg=$this->CreatImage($this->im,$this->srcW,$this->srcH,0,0,0,0,$this->srcW,$this->srcH);
             return $this->EchoImage($cImg,$toFile);
             ImageDestroy($cImg);
         }
     }
     
     //生成最小裁剪后的缩图
     function Cut($toFile,$toW,$toH)
     {
           $toWH=$toW/$toH;
           $srcWH=$this->srcW/$this->srcH;
           if($toWH< =$srcWH)
           {
                $ctoH=$toH;
                $ctoW=$ctoH*($this->srcW/$this->srcH);
           }
           else
           {
               $ctoW=$toW;
               $ctoH=$ctoW*($this->srcH/$this->srcW);
           } 
         $allImg=$this->CreatImage($this->im,$ctoW,$ctoH,0,0,0,0,$this->srcW,$this->srcH);
         $cImg=$this->CreatImage($allImg,$toW,$toH,0,0,($ctoW-$toW)/2,($ctoH-$toH)/2,$toW,$toH);
         return $this->EchoImage($cImg,$toFile);
         ImageDestroy($cImg);
         ImageDestroy($allImg);
     }
  
     //生成背景填充的缩图
     function BackFill($toFile,$toW,$toH,$bk1=255,$bk2=255,$bk3=255)
     {
         $toWH=$toW/$toH;
         $srcWH=$this->srcW/$this->srcH;
         if($toWH< =$srcWH)
         {
             $ftoW=$toW;
             $ftoH=$ftoW*($this->srcH/$this->srcW);
         }
         else
         {
               $ftoH=$toH;
               $ftoW=$ftoH*($this->srcW/$this->srcH);
         }
         if(function_exists("imagecreatetruecolor"))
         {
             @$cImg=ImageCreateTrueColor($toW,$toH);
             if(!$cImg)
             {
                 $cImg=ImageCreate($toW,$toH);
             }
         }
         else
         {
             $cImg=ImageCreate($toW,$toH);
         }
         $backcolor = imagecolorallocate($cImg, $bk1, $bk2, $bk3);        //填充的背景颜色
         ImageFilledRectangle($cImg,0,0,$toW,$toH,$backcolor);
         if($this->srcW>$toW||$this->srcH>$toH)
         {     
             $proImg=$this->CreatImage($this->im,$ftoW,$ftoH,0,0,0,0,$this->srcW,$this->srcH);
             /*
              if($ftoW< $toW)
              {
                  ImageCopyMerge($cImg,$proImg,($toW-$ftoW)/2,0,0,0,$ftoW,$ftoH,100);
              }
              else if($ftoH<$toH)
              {
                  ImageCopyMerge($cImg,$proImg,0,($toH-$ftoH)/2,0,0,$ftoW,$ftoH,100);
              }
              */
             if($ftoW<$toW)
             {
                  ImageCopy($cImg,$proImg,($toW-$ftoW)/2,0,0,0,$ftoW,$ftoH);
             }
             else if($ftoH<$toH)
             {
                  ImageCopy($cImg,$proImg,0,($toH-$ftoH)/2,0,0,$ftoW,$ftoH);
             }
             else
             {
                  ImageCopy($cImg,$proImg,0,0,0,0,$ftoW,$ftoH);
             } 
         }
         else
         {
              ImageCopyMerge($cImg,$this->im,($toW-$ftoW)/2,($toH-$ftoH)/2,0,0,$ftoW,$ftoH,100);
         }
         return $this->EchoImage($cImg,$toFile);
         ImageDestroy($cImg);
     }
     
  
     function CreatImage($img,$creatW,$creatH,$dstX,$dstY,$srcX,$srcY,$srcImgW,$srcImgH)
     {
         if(function_exists("imagecreatetruecolor"))
         {
             @$creatImg = ImageCreateTrueColor($creatW,$creatH);              if($creatImg) 
                 ImageCopyResampled($creatImg,$img,$dstX,$dstY,$srcX,$srcY,$creatW,$creatH,$srcImgW,$srcImgH);
             else
             {
                 $creatImg=ImageCreate($creatW,$creatH);
                 ImageCopyResized($creatImg,$img,$dstX,$dstY,$srcX,$srcY,$creatW,$creatH,$srcImgW,$srcImgH);
             }
          }
          else
          {
             $creatImg=ImageCreate($creatW,$creatH);
             ImageCopyResized($creatImg,$img,$dstX,$dstY,$srcX,$srcY,$creatW,$creatH,$srcImgW,$srcImgH);
          }
          return $creatImg;
     }
     
     //输出图片,link---只输出,不保存文件。file--保存为文件
     function EchoImage($img,$to_File)
     {
         switch($this->echoType)          {
             case "link":
                 if(function_exists('imagejpeg')) return ImageJpeg($img);
                 else return ImagePNG($img);
                 break;
             case "file":
                 if(function_exists('imagejpeg')) return ImageJpeg($img,$to_File);
                 else return ImagePNG($img,$to_File);
                 break;
         }
     }
  
 }
 ?>
在php中生成缩略图是程序开发中常用的,下面我找了几个不错的php生成缩略图的实现程序,有需要的朋友可使用,本人亲测绝对好用哦。

 

创建图像缩略图需要许多时间,此代码将有助于了解缩略图的逻辑。

 代码如下 复制代码

 
/**********************
*@filename - path to the image
*@tmpname - temporary path to thumbnail
*@xmax - max width
*@ymax - max height
*/
function resize_image($filename, $tmpname, $xmax, $ymax)
{
    $ext = explode(".", $filename);
    $ext = $ext[count($ext)-1];

    if($ext == "jpg" || $ext == "jpeg")
        $im = imagecreatefromjpeg($tmpname);
    elseif($ext == "png")
        $im = imagecreatefrompng($tmpname);
    elseif($ext == "gif")
        $im = imagecreatefromgif($tmpname);

    $x = imagesx($im);
    $y = imagesy($im);

    if($x <= $xmax && $y <= $ymax)
        return $im;

    if($x >= $y) {
        $newx = $xmax;
        $newy = $newx * $y / $x;
    }
    else {
        $newy = $ymax;
        $newx = $x / $y * $newy;
    }

    $im2 = imagecreatetruecolor($newx, $newy);
    imagecopyresized($im2, $im, 0, 0, 0, 0, floor($newx), floor($newy), $x, $y);
    return $im2;
}

例2

 代码如下 复制代码

function creat_thumbnail($img,$w,$h,$path)
{
 $org_info = getimagesize($img); //获得图像大小且是通过post传递过来的
 //var_dump($org_info);
 //Array ( [0] => 1024 [1] => 768 [2] => 3 [3] => width="1024" height="768" [bits] => 8 [mime] => image/png )
 $orig_x = $org_info[0]; //图像宽度
 $orig_y = $org_info[1]; //图像高度
 $orig_type = $org_info[2]; //图片类别即后缀 1 = GIF,2 = JPG,3 = PNG,

 //是真彩色图像
 if (function_exists("imagecreatetruecolor"))
 {
  switch($orig_type)
  {
   //从给定的gif文件名中取得的图像
   case 1  : $thumb_type = ".gif"; $_creatImage = "imagegif"; $_function = "imagecreatefromgif";
   break;
   //从给定的jpeg,jpg文件名中取得的图像
   case 2  : $thumb_type = ".jpg"; $_creatImage = "imagejpeg"; $_function = "imagecreatefromjpeg";
   break;
   //从给定的png文件名中取得的图像
   case 3  : $thumb_type = ".png"; $_creatImage = "imagepng"; $_function = "imagecreatefrompng";
   break;
  }
 }
 //如果从给定的文件名可取得的图像
 if(function_exists($_function))
 {
  $orig_image = $_function($img); //从给定的$img文件名中取得的图像
 }
 if (($orig_x / $orig_y) >= (4 / 3)) //如果宽/高 >= 4/3
 {
  $y = round($orig_y / ($orig_x / $w)); //对浮点数进行四舍五入
  $x = $w;
 }
 else //即 高/宽 >= 4/3
 {
  $x = round($orig_x / ($orig_y / $h));
  $y = $h;
 }
 $sm_image = imagecreatetruecolor($x, $y); //创建真彩色图片
 //重采样拷贝部分图像并调整大小
 Imagecopyresampled($sm_image, $orig_image, 0, 0, 0, 0, $x, $y, $orig_x, $orig_y);
 //imageJPEG($sm_image, '', 80); //在浏览器输出图像
 $tnpath = $path."/"."s_".date('YmdHis').$thumb_type; //缩略图的路径
 $thumbnail = @$_creatImage($sm_image, $tnpath, 80); //生成图片,成功返回true(或1)
 imagedestroy ($sm_image); //销毁图像
 if($thumbnail==true)
 {
  return $tnpath;
 }
}

生成缩略图是当我们要上传图片时如果图片较大我们需要生成一张指定大小的小图以在列表页面显示,这样可节省资源也是现在常用的做法,下面我来分享一个php生成缩略图类,支持自定义高和宽。还支持按高和宽截图
 代码如下 复制代码

class resizeimage 

    //图片类型 
    var $type; 
    //实际宽度 
    var $width; 
    //实际高度 
    var $height; 
    //改变后的宽度 
    var $resize_width; 
    //改变后的高度 
    var $resize_height; 
    //是否裁图 
    var $cut; 
    //源图象 
    var $srcimg; 
    //目标图象地址 
    var $dstimg; 
    //临时创建的图象 
    var $im; 
    function resizeimage($img, $wid, $hei,$c,$dstpath) 
    { 
        $this->srcimg = $img; 
        $this->resize_width = $wid; 
        $this->resize_height = $hei; 
        $this->cut = $c; 
        //图片的类型 

$this->type = strtolower(substr(strrchr($this->srcimg,"."),1)); 
        //初始化图象 
        $this->initi_img(); 
        //目标图象地址 
        $this -> dst_img($dstpath); 
        //-- 
        $this->width = imagesx($this->im); 
        $this->height = imagesy($this->im); 
        //生成图象 
        $this->newimg(); 
        ImageDestroy ($this->im); 
    } 
    function newimg() 
    { 
        //改变后的图象的比例 
        $resize_ratio = ($this->resize_width)/($this->resize_height); 
        //实际图象的比例 
        $ratio = ($this->width)/($this->height); 
        if(($this->cut)=="1") 
        //裁图 
        { 
            if($ratio>=$resize_ratio) 
            //高度优先 
            { 
                $newimg = imagecreatetruecolor($this->resize_width,$this->resize_height); 
                imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width,$this->resize_height, (($this->height)*$resize_ratio), $this->height); 
                ImageJpeg ($newimg,$this->dstimg); 
            } 
            if($ratio<$resize_ratio) 
            //宽度优先 
            { 
                $newimg = imagecreatetruecolor($this->resize_width,$this->resize_height); 
                imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, $this->resize_height, $this->width, (($this->width)/$resize_ratio)); 
                ImageJpeg ($newimg,$this->dstimg); 
            } 
        } 
        else
        //不裁图 
        { 
            if($ratio>=$resize_ratio) 
            { 
                $newimg = imagecreatetruecolor($this->resize_width,($this->resize_width)/$ratio); 
                imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, ($this->resize_width)/$ratio, $this->width, $this->height); 
                ImageJpeg ($newimg,$this->dstimg); 
            } 
            if($ratio<$resize_ratio) 
            { 
                $newimg = imagecreatetruecolor(($this->resize_height)*$ratio,$this->resize_height); 
                imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, ($this->resize_height)*$ratio, $this->resize_height, $this->width, $this->height); 
                ImageJpeg ($newimg,$this->dstimg); 
            } 
        } 
    } 
    //初始化图象 
    function initi_img() 
    { 
        if($this->type=="jpg") 
        { 
            $this->im = imagecreatefromjpeg($this->srcimg); 
        } 
        if($this->type=="gif") 
        { 
            $this->im = imagecreatefromgif($this->srcimg); 
        } 
        if($this->type=="png") 
        { 
            $this->im = imagecreatefrompng($this->srcimg); 
        } 
    } 
    //图象目标地址 
    function dst_img($dstpath) 
    { 
        $full_length  = strlen($this->srcimg); 
        $type_length  = strlen($this->type); 
        $name_length  = $full_length-$type_length; 

        $name         = substr($this->srcimg,0,$name_length-1); 
        $this->dstimg = $dstpath; 

//echo $this->dstimg; 
    } 

$resizeimage = new resizeimage("11.jpg", "200", "150", "1","17.jpg"); 
?>

实例二

 代码如下 复制代码

<?php

/**
 * 生成缩略图
 * @author yangzhiguo0903@163.com
 * @param string     源图绝对完整地址{带文件名及后缀名}
 * @param string     目标图绝对完整地址{带文件名及后缀名}
 * @param int        缩略图宽{0:此时目标高度不能为0,目标宽度为源图宽*(目标高度/源图高)}
 * @param int        缩略图高{0:此时目标宽度不能为0,目标高度为源图高*(目标宽度/源图宽)}
 * @param int        是否裁切{宽,高必须非0}
 * @param int/float  缩放{0:不缩放, 0<this<1:缩放到相应比例(此时宽高限制和裁切均失效)}
 * @return boolean
 */
function img2thumb($src_img, $dst_img, $width = 75, $height = 75, $cut = 0, $proportion = 0)
{
    if(!is_file($src_img))
    {
        return false;
    }
    $ot = fileext($dst_img);
    $otfunc = 'image' . ($ot == 'jpg' ? 'jpeg' : $ot);
    $srcinfo = getimagesize($src_img);
    $src_w = $srcinfo[0];
    $src_h = $srcinfo[1];
    $type  = strtolower(substr(image_type_to_extension($srcinfo[2]), 1));
    $createfun = 'imagecreatefrom' . ($type == 'jpg' ? 'jpeg' : $type);

    $dst_h = $height;
    $dst_w = $width;
    $x = $y = 0;

    /**
     * 缩略图不超过源图尺寸(前提是宽或高只有一个)
     */
    if(($width> $src_w && $height> $src_h) || ($height> $src_h && $width == 0) || ($width> $src_w && $height == 0))
    {
        $proportion = 1;
    }
    if($width> $src_w)
    {
        $dst_w = $width = $src_w;
    }
    if($height> $src_h)
    {
        $dst_h = $height = $src_h;
    }

    if(!$width && !$height && !$proportion)
    {
        return false;
    }
    if(!$proportion)
    {
        if($cut == 0)
        {
            if($dst_w && $dst_h)
            {
                if($dst_w/$src_w> $dst_h/$src_h)
                {
                    $dst_w = $src_w * ($dst_h / $src_h);
                    $x = 0 - ($dst_w - $width) / 2;
                }
                else
                {
                    $dst_h = $src_h * ($dst_w / $src_w);
                    $y = 0 - ($dst_h - $height) / 2;
                }
            }
            else if($dst_w xor $dst_h)
            {
                if($dst_w && !$dst_h)  //有宽无高
                {
                    $propor = $dst_w / $src_w;
                    $height = $dst_h  = $src_h * $propor;
                }
                else if(!$dst_w && $dst_h)  //有高无宽
                {
                    $propor = $dst_h / $src_h;
                    $width  = $dst_w = $src_w * $propor;
                }
            }
        }
        else
        {
            if(!$dst_h)  //裁剪时无高
            {
                $height = $dst_h = $dst_w;
            }
            if(!$dst_w)  //裁剪时无宽
            {
                $width = $dst_w = $dst_h;
            }
            $propor = min(max($dst_w / $src_w, $dst_h / $src_h), 1);
            $dst_w = (int)round($src_w * $propor);
            $dst_h = (int)round($src_h * $propor);
            $x = ($width - $dst_w) / 2;
            $y = ($height - $dst_h) / 2;
        }
    }
    else
    {
        $proportion = min($proportion, 1);
        $height = $dst_h = $src_h * $proportion;
        $width  = $dst_w = $src_w * $proportion;
    }

    $src = $createfun($src_img);
    $dst = imagecreatetruecolor($width ? $width : $dst_w, $height ? $height : $dst_h);
    $white = imagecolorallocate($dst, 255, 255, 255);
    imagefill($dst, 0, 0, $white);

    if(function_exists('imagecopyresampled'))
    {
        imagecopyresampled($dst, $src, $x, $y, 0, 0, $dst_w, $dst_h, $src_w, $src_h);
    }
    else
    {
        imagecopyresized($dst, $src, $x, $y, 0, 0, $dst_w, $dst_h, $src_w, $src_h);
    }
    $otfunc($dst, $dst_img);
    imagedestroy($dst);
    imagedestroy($src);
    return true;
}

使用方法

 代码如下 复制代码

$src_img = "./ROSI_050_002.JPG";
$dst_img = "./ROSI_050_002_thumb.jpg";
$stat = img2thumb($src_img, $dst_img, $width = 200, $height = 300, $cut = 0, $proportion = 0);
if($stat){
 echo 'Resize Image Success!<br />';
 echo '<img src="'.$dst_img.'" />'; 
}else{
 echo 'Resize Image Fail!'; 
}

[!--infotagslink--]

相关文章

  • 使用PHP+JavaScript将HTML页面转换为图片的实例分享

    这篇文章主要介绍了使用PHP+JavaScript将HTML元素转换为图片的实例分享,文后结果的截图只能体现出替换的字体,也不能说将静态页面转为图片可以加快加载,只是这种做法比较interesting XD需要的朋友可以参考下...2016-04-19
  • C#从数据库读取图片并保存的两种方法

    这篇文章主要介绍了C#从数据库读取图片并保存的方法,帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下...2021-01-16
  • Photoshop古装美女图片转为工笔画效果制作教程

    今天小编在这里就来给各位Photoshop的这一款软件的使用者们来说说把古装美女图片转为细腻的工笔画效果的制作教程,各位想知道方法的使用者们,那么下面就快来跟着小编一...2016-09-14
  • Python 图片转数组,二进制互转操作

    这篇文章主要介绍了Python 图片转数组,二进制互转操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-09
  • php抓取网站图片并保存的实现方法

    php如何实现抓取网页图片,相较于手动的粘贴复制,使用小程序要方便快捷多了,喜欢编程的人总会喜欢制作一些简单有用的小软件,最近就参考了网上一个php抓取图片代码,封装了一个php远程抓取图片的类,测试了一下,效果还不错分享...2015-10-30
  • jquery左右滚动焦点图banner图片鼠标经过显示上下页按钮

    jquery左右滚动焦点图banner图片鼠标经过显示上下页按钮...2013-10-13
  • 利用JS实现点击按钮后图片自动切换的简单方法

    下面小编就为大家带来一篇利用JS实现点击按钮后图片自动切换的简单方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2016-10-25
  • PHP 验证码不显示只有一个小红叉的解决方法

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

    Photoshop的这一款软件小编相信很多的人都已经是使用过了吧,那么今天小编在这里就给大家带来了用Photoshop软件制作枪战电影海报的教程,想知道制作步骤的玩家们,那么下面...2016-09-14
  • js实现上传图片及时预览

    这篇文章主要为大家详细介绍了js实现上传图片及时预览的相关资料,具有一定的参考价值,感兴趣的朋友可以参考一下...2016-05-09
  • jQuery Real Person验证码插件防止表单自动提交

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

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

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

    这篇文章主要介绍了Java生成随机姓名、性别和年龄的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-10-01
  • python opencv通过4坐标剪裁图片

    图片剪裁是常用的方法,那么如何通过4坐标剪裁图片,本文就详细的来介绍一下,感兴趣的小伙伴们可以参考一下...2021-06-04
  • C#生成随机数功能示例

    这篇文章主要介绍了C#生成随机数功能,涉及C#数学运算与字符串操作相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25
  • 使用PHP下载CSS文件中的图片的代码

    共享一段使用PHP下载CSS文件中的图片的代码 复制代码 代码如下: <?php //note 设置PHP超时时间 set_time_limit(0); //note 取得样式文件内容 $styleFileContent = file_get_contents('images/style.css'); //not...2013-10-04
  • PHP swfupload图片上传的实例代码

    PHP代码如下:复制代码 代码如下:if (isset($_FILES["Filedata"]) || !is_uploaded_file($_FILES["Filedata"]["tmp_name"]) || $_FILES["Filedata"]["error"] != 0) { $upload_file = $_FILES['Filedata']; $fil...2013-10-04
  • 微信小程序如何获取图片宽度与高度

    这篇文章主要给大家介绍了关于微信小程序如何获取图片宽度与高度的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-03-10
  • C#中图片旋转和翻转(RotateFlipType)用法分析

    这篇文章主要介绍了C#中图片旋转和翻转(RotateFlipType)用法,实例分析了C#图片旋转及翻转Image.RotateFlip方法属性的常用设置技巧,需要的朋友可以参考下...2020-06-25