怎么把验证码的图片存起来

php中文网
发布: 2016-06-06 20:22:36
原创
1819人浏览过

如代码gd生成了图片$im:


   public function generateCode(){
        $w = 50;
        $h = 20;
        $this->load->config('common');
        $common_config = $this->config->item('common');
        $pic_verify_code = $common_config['pic_verify_code'];
        $code = func::getRandCode($pic_verify_code);
        //创建图片,定义颜色值
        $im = imagecreate($w, $h);
        $black = imagecolorallocate($im,90,90,90);
        $gray = imagecolorallocate($im, 189,252,201);
        $bgcolor = imagecolorallocate($im, 255, 255, 255);
        //填充背景
        imagefill($im, 0, 0, $gray);

        //画边框
        imagerectangle($im, 0, 0, $w-1, $h-1, $black);

        //随机绘制两条虚线,起干扰作用
        $style = array ($black,$black,$black,$black,$black,
            $gray,$gray,$gray,$gray,$gray
        );
        imagesetstyle($im, $style);
        $y1 = rand(0, $h);
        $y2 = rand(0, $h);
        $y3 = rand(0, $h);
        $y4 = rand(0, $h);
        imageline($im, 0, $y1, $w, $y3, IMG_COLOR_STYLED);
        imageline($im, 0, $y2, $w, $y4, IMG_COLOR_STYLED);

        //在画布上随机生成大量黑点,起干扰作用
        for ($i = 0; $i < 80; $i++) {
            imagesetpixel($im, rand(0, $w), rand(0, $h), $black);
        }
        //将数字随机显示在画布上,字符的水平间距和位置都按一定波动范围随机生成
        $strx = rand(3, 8);
        for ($i = 0; $i < $pic_verify_code; $i++) {
            $strpos = rand(1, 6);
            imagestring($im, 5, $strx, $strpos, substr($code, $i, 1), $black);
            $strx += rand(8, 12);
        }
登录后复制

那么问题来了,怎么把这个图片存到我的服务器上面呢?

VoxDeck
VoxDeck

美间AI推出的演示文稿制作智能体

VoxDeck 90
查看详情 VoxDeck

回复内容:

如代码gd生成了图片$im:


   public function generateCode(){
        $w = 50;
        $h = 20;
        $this->load->config('common');
        $common_config = $this->config->item('common');
        $pic_verify_code = $common_config['pic_verify_code'];
        $code = func::getRandCode($pic_verify_code);
        //创建图片,定义颜色值
        $im = imagecreate($w, $h);
        $black = imagecolorallocate($im,90,90,90);
        $gray = imagecolorallocate($im, 189,252,201);
        $bgcolor = imagecolorallocate($im, 255, 255, 255);
        //填充背景
        imagefill($im, 0, 0, $gray);

        //画边框
        imagerectangle($im, 0, 0, $w-1, $h-1, $black);

        //随机绘制两条虚线,起干扰作用
        $style = array ($black,$black,$black,$black,$black,
            $gray,$gray,$gray,$gray,$gray
        );
        imagesetstyle($im, $style);
        $y1 = rand(0, $h);
        $y2 = rand(0, $h);
        $y3 = rand(0, $h);
        $y4 = rand(0, $h);
        imageline($im, 0, $y1, $w, $y3, IMG_COLOR_STYLED);
        imageline($im, 0, $y2, $w, $y4, IMG_COLOR_STYLED);

        //在画布上随机生成大量黑点,起干扰作用
        for ($i = 0; $i < 80; $i++) {
            imagesetpixel($im, rand(0, $w), rand(0, $h), $black);
        }
        //将数字随机显示在画布上,字符的水平间距和位置都按一定波动范围随机生成
        $strx = rand(3, 8);
        for ($i = 0; $i < $pic_verify_code; $i++) {
            $strpos = rand(1, 6);
            imagestring($im, 5, $strx, $strpos, substr($code, $i, 1), $black);
            $strx += rand(8, 12);
        }
登录后复制

那么问题来了,怎么把这个图片存到我的服务器上面呢?

write to file ,e. fwite()

imagepng($im,'pic.png');
登录后复制

验证码直接发给客户端不就好了,保存起来干嘛;
xxx.php直接:
imagepng($im);
登陆那里怎么把验证码的图片存起来

相关标签:
php
最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号