在Symfony2中生成验证码2
2016-03-17 00:42
459 查看
编辑
执行
然后就可以在控制器里调用该依赖包啦,下面是具体的代码:
如此,验证码图片的访问地址为:
composer.json,在
require段中加入一行依赖:
"gregwar/captcha": "1.*"
执行
composer update安装依赖包
然后就可以在控制器里调用该依赖包啦,下面是具体的代码:
<?php namespace Site\CommonBundle\Controller; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; use Symfony\Component\HttpFoundation\Response; use Gregwar\Captcha\PhraseBuilder; use Gregwar\Captcha\CaptchaBuilder; class DefaultController extends BaseController { /** * 生成验证码. * * @Route("captcha", name="site_common_captcha") */ public function captchaAction() { $req = $this->getRequest(); $width = intval($req->get('width')) ?: 150; // 验证码图片的宽度 $height = intval($req->get('height')) ?: 50; // 验证码图片的高度 $length = 5; // 验证码字符的长度 $no_effect = true; // 是否忽略验证图片上的干扰线条 $pharse = new PhraseBuilder(); $captcha = new CaptchaBuilder($pharse->build($length)); $image = $captcha->setIgnoreAllEffects($no_effect)->build($width, $height)->get(); $session = $req->getSession(); $session->set('captcha', $captcha->getPhrase()); $res = new Response($image); $res->headers->set('content-type', 'image/jpeg'); return $res; } }
如此,验证码图片的访问地址为:
http://www.example.com/captcha?width=100&height=30(可以传递参数改变图片的尺寸)
相关文章推荐
- iOS APP上架过程常见问题
- VMware Workstation 12 Player安装Windows10 x64出现的问题
- [POJ 2516]Minimum Cost[费用流][建图]
- 基本运算
- Android——Activity练习
- 深度优先搜索和广度优先搜索的简单对比
- POJ 3070 矩阵快速幂解决fib问题
- php发送邮件类
- Windows下的串口类及功能实现
- 构造器的调用
- 使用jlink直接烧norflash或者nandflash不借助uboot的猜想
- Go初探
- JVM
- Linux---centos编译安装ffmpeg
- android 9png的效果a
- POJ 1035 Spell checker(字符串比较)
- 十道海量数据处理面试题
- TCP的三次握手(建立连接)和四次挥手(关闭连接)
- mysql5.6配置版
- 数学#扩展欧几里德 POJ 1061&2115&2891