zoukankan      html  css  js  c++  java
  • gd2验证码

    <?php
    //创建画布
    $image = imagecreatetruecolor(300,200);
    //分配背景色
    $bg = imagecolorallocate($image,mt_rand(2,200),mt_rand(2,200),mt_rand(2,200));
    //填充颜色
    imagefill($image,0,0,$bg);
    $font = 5;
    $array = array_merge(range(0,9),range('A','Z'));
    $index = array_rand($array,5);
    shuffle($index);
    $string = '';
    foreach($index as $value){
        $string .=$array[$value];
    }
    $x = (imagesx($image) - imagefontwidth($font)*strlen($string))/2;
    $y = (imagesy($image) -imagefontheight($font))/2;
    //设置字符串颜色
    $color2 = imagecolorallocate($image,mt_rand(2,200),mt_rand(2,200),mt_rand(2,200));
    //将提供的字符串,写入图片
    imagestring($image,$font,$x,$y,$string,$color2);
    //输出图像
    header('content-type:image/jpeg');
    imagejpeg($image);
    //销毁图像
    imagedestroy($image);
    <?php
    //自定义图像
    $filename = './timg.jpg';
    $index = strrpos($filename,'.');
    $ext = substr($filename,$index+1);
    $array = array(
        'jpg' => 'imagecreatefromjpeg',
        'png' => 'imagecreatefrompng ',
        'gif' => 'imagecreatefromgif'
    );
    $image = $array[$ext]($filename);
    //$image = imagecreatefromjpeg($filename);
    $font = 5;
    $array = array_merge(range(0,9),range('A','Z'));
    $index = array_rand($array,5);
    shuffle($index);
    $string = '';
    foreach($index as $value){
        $string .=$array[$value];
    }
    $x = (imagesx($image) - imagefontwidth($font)*strlen($string))/2;
    $y = (imagesy($image) -imagefontheight($font))/2;
    //设置字符串颜色
    $color2 = imagecolorallocate($image,mt_rand(2,200),mt_rand(2,200),mt_rand(2,200));
    //将提供的字符串,写入图片
    imagestring($image,$font,$x,$y,$string,$color2);
    //输出图像
    header('content-type:image/jpeg');
    imagejpeg($image);
    //销毁图像
    imagedestroy($image);
    <?php
    //缩略图
    $filename = './timg.jpg';
    $index = strrpos($filename,'.');
    $ext = substr($filename,$index+1);
    $array = array(
        'jpg' => 'imagecreatefromjpeg',
        'png' => 'imagecreatefrompng ',
        'gif' => 'imagecreatefromgif'
    );
    $image = $array[$ext]($filename);
    //得到图像宽高1
    // $src_w = imagesx($image);
    // $src_h = imagesy($image);
    //得到图像宽高2
    list($src_w,$src_h) = getimagesize($filename);
    $dst_w = $src_w*0.5;
    $dst_h = $src_h*0.5;
    $dst_image = imagecreatetruecolor($dst_w,$dst_h);
    //重新拷贝获取大小1  速度快
    //imagecopyresampled($dst_image,$image,0,0,0,0,$dst_w, $dst_h, $src_w, $src_h);
    //重新拷贝获取大小2  速度慢
    imagecopyresized($dst_image,$image,0,0,0,0,$dst_w, $dst_h, $src_w, $src_h);
    
    header('content-type:image/jpeg');
    imagejpeg($dst_image);
    //销毁图像
    imagedestroy($image,$dst_image);
  • 相关阅读:
    poj 2728 Desert King(最小比率生成树,迭代法)
    HDU
    hud 2089 不要62 (数位dp)
    食物链(带全并查集)
    docNet基础学完感想
    zoj 1081 (改进的弧长算法)(转)
    zoj 1962 How Many Fibs?(字符串化为数字处理)
    zoj 1109 zoj 1109 Language of FatMouse(字典树)
    iOS开发网络数据之AFNetworking使用
    iOS 使用AFNetworking
  • 原文地址:https://www.cnblogs.com/mengor/p/12060231.html
Copyright © 2011-2022 走看看