jjzjj

php - codeigniter 验证码助手没有增加文本字体大小

coder 2024-04-27 原文

这似乎是一个重复的问题,但事实并非如此。 我问以下问题:

  1. codeingiter 助手是否有增加字体大小的功能?即使我通过了以下数组设置似乎也没有改变。

    'font_size' => 36

2.我使用的字体有以下字体大小,但验证码大小仍然没有变化

这是怎么回事?

/* Setup config to pass into the create_captcha function */
                $capache_config = array(
                    'img_path' => 'captcha/',
                    'img_url' => base_url() . 'captcha/',
                    'img_width' => '150',
                    'font_path' => base_url() . 'captcha/font/captcha4.ttf',
                    'img_height' => 40,
                    'expiration' => 7200
                );

                /* Generate the captcha */
                $captcha = create_captcha($capache_config);

但是即使我增加了宽度或高度,我也不能把它变大吗? 你对此有什么了解吗?

最佳答案

Codeigniter 验证码助手不允许更改字体大小。以下将替换默认的 create_captcha 函数并允许您更改字体大小。我使用的 CodeIgniter 版本是 2.2.0。这是工作/测试代码。

第 1 步: 创建一个名为 MY_captcha_helper.php 的新帮助程序文件,并将其保存在 application/helper 中 CodeIgniter 文件夹的根目录下.文件内容:

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * CodeIgniter
 *
 * An open source application development framework for PHP 5.1.6 or newer
 *
 * @package     CodeIgniter
 * @author      ExpressionEngine Dev Team
 * @copyright   Copyright (c) 2008 - 2014, EllisLab, Inc.
 * @license     http://codeigniter.com/user_guide/license.html
 * @link        http://codeigniter.com
 * @since       Version 1.0
 * @filesource
 */

// ------------------------------------------------------------------------

/**
 * CodeIgniter CAPTCHA Helper
 *
 * @package     CodeIgniter
 * @subpackage  Helpers
 * @category    Helpers
 * @author      ExpressionEngine Dev Team
 * @link        http://codeigniter.com/user_guide/helpers/xml_helper.html
 */

// ------------------------------------------------------------------------

/**
 * Create CAPTCHA
 *
 * @access  public
 * @param   array   array of data for the CAPTCHA
 * @param   string  path to create the image in
 * @param   string  URL to the CAPTCHA image folder
 * @param   string  server path to font
 * @return  string
 */
if ( ! function_exists('create_captcha'))
{
    function create_captcha($data = '', $img_path = '', $img_url = '', $font_path = '')
    {
        $defaults = array('word' => '', 'img_path' => '', 'img_url' => '', 'img_width' => '150', 'img_height' => '30', 'font_path' => '', 'expiration' => 7200, 'font_size' => 5);

        foreach ($defaults as $key => $val)
        {
            if ( ! is_array($data))
            {
                if ( ! isset($$key) OR $$key == '')
                {
                    $$key = $val;
                }
            }
            else
            {
                $$key = ( ! isset($data[$key])) ? $val : $data[$key];
            }
        }

        if ($img_path == '' OR $img_url == '')
        {
            return FALSE;
        }

        if ( ! @is_dir($img_path))
        {
            return FALSE;
        }

        if ( ! is_writable($img_path))
        {
            return FALSE;
        }

        if ( ! extension_loaded('gd'))
        {
            return FALSE;
        }

        // -----------------------------------
        // Remove old images
        // -----------------------------------

        list($usec, $sec) = explode(" ", microtime());
        $now = ((float)$usec + (float)$sec);

        $current_dir = @opendir($img_path);

        while ($filename = @readdir($current_dir))
        {
            if ($filename != "." and $filename != ".." and $filename != "index.html")
            {
                $name = str_replace(".jpg", "", $filename);

                if (($name + $expiration) < $now)
                {
                    @unlink($img_path.$filename);
                }
            }
        }

        @closedir($current_dir);

        // -----------------------------------
        // Do we have a "word" yet?
        // -----------------------------------

       if ($word == '')
       {
            $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';

            $str = '';
            for ($i = 0; $i < 8; $i++)
            {
                $str .= substr($pool, mt_rand(0, strlen($pool) -1), 1);
            }

            $word = $str;
       }

        // -----------------------------------
        // Determine angle and position
        // -----------------------------------

        $length = strlen($word);
        $angle  = ($length >= 6) ? rand(-($length-6), ($length-6)) : 0;
        $x_axis = rand(6, (360/$length)-16);
        $y_axis = ($angle >= 0 ) ? rand($img_height, $img_width) : rand(6, $img_height);

        // -----------------------------------
        // Create image
        // -----------------------------------

        // PHP.net recommends imagecreatetruecolor(), but it isn't always available
        if (function_exists('imagecreatetruecolor'))
        {
            $im = imagecreatetruecolor($img_width, $img_height);
        }
        else
        {
            $im = imagecreate($img_width, $img_height);
        }

        // -----------------------------------
        //  Assign colors
        // -----------------------------------

        $bg_color       = imagecolorallocate ($im, 255, 255, 255);
        $border_color   = imagecolorallocate ($im, 153, 102, 102);
        $text_color     = imagecolorallocate ($im, 204, 153, 153);
        $grid_color     = imagecolorallocate($im, 255, 182, 182);
        $shadow_color   = imagecolorallocate($im, 255, 240, 240);

        // -----------------------------------
        //  Create the rectangle
        // -----------------------------------

        ImageFilledRectangle($im, 0, 0, $img_width, $img_height, $bg_color);

        // -----------------------------------
        //  Create the spiral pattern
        // -----------------------------------

        $theta      = 1;
        $thetac     = 7;
        $radius     = 16;
        $circles    = 20;
        $points     = 32;

        for ($i = 0; $i < ($circles * $points) - 1; $i++)
        {
            $theta = $theta + $thetac;
            $rad = $radius * ($i / $points );
            $x = ($rad * cos($theta)) + $x_axis;
            $y = ($rad * sin($theta)) + $y_axis;
            $theta = $theta + $thetac;
            $rad1 = $radius * (($i + 1) / $points);
            $x1 = ($rad1 * cos($theta)) + $x_axis;
            $y1 = ($rad1 * sin($theta )) + $y_axis;
            imageline($im, $x, $y, $x1, $y1, $grid_color);
            $theta = $theta - $thetac;
        }

        // -----------------------------------
        //  Write the text
        // -----------------------------------

        $use_font = ($font_path != '' AND file_exists($font_path) AND function_exists('imagettftext')) ? TRUE : FALSE;

        if ($use_font == FALSE)
        {
            $font_size = $font_size != 5 ? $font_size : 5;
            $x = rand(0, $img_width/($length/3));
            $y = 0;
        }
        else
        {
            $font_size = $font_size != 5 ? $font_size : 16;
            $x = rand(0, $img_width/($length/1.5));
            $y = $font_size+2;
        }

        for ($i = 0; $i < strlen($word); $i++)
        {
            if ($use_font == FALSE)
            {
                $y = rand(0 , $img_height/2);
                imagestring($im, $font_size, $x, $y, substr($word, $i, 1), $text_color);
                $x += ($font_size*2);
            }
            else
            {
                $y = rand($img_height/2, $img_height-3);
                imagettftext($im, $font_size, $angle, $x, $y, $text_color, $font_path, substr($word, $i, 1));
                $x += $font_size;
            }
        }


        // -----------------------------------
        //  Create the border
        // -----------------------------------

        imagerectangle($im, 0, 0, $img_width-1, $img_height-1, $border_color);

        // -----------------------------------
        //  Generate the image
        // -----------------------------------

        $img_name = $now.'.jpg';

        ImageJPEG($im, $img_path.$img_name);

        $img = "<img src=\"$img_url$img_name\" width=\"$img_width\" height=\"$img_height\" style=\"border:0;\" alt=\" \" />";

        ImageDestroy($im);

        return array('word' => $word, 'time' => $now, 'image' => $img);
    }
}

// ------------------------------------------------------------------------

/* End of file captcha_helper.php */
/* Location: ./system/heleprs/captcha_helper.php */

我已经改变了:

这个

$defaults = array('word' => '', 'img_path' => '', 'img_url' => '', 'img_width' => '150', 'img_height' => '30', 'font_path' => '', 'expiration' => 7200);

进入这个

$defaults = array('word' => '', 'img_path' => '', 'img_url' => '', 'img_width' => '150', 'img_height' => '30', 'font_path' => '', 'expiration' => 7200, 'font_size' => 5);

还有这个

if ($use_font == FALSE)
{
    $font_size = 5;
    $x = rand(0, $img_width/($length/3));
    $y = 0;
}
else
{
    $font_size = 16;
    $x = rand(0, $img_width/($length/1.5));
    $y = $font_size+2;
}

进入这个

if ($use_font == FALSE)
{
    $font_size = $font_size != 5 ? $font_size : 5;
    $x = rand(0, $img_width/($length/3));
    $y = 0;
}
else
{
    $font_size = $font_size != 5 ? $font_size : 16;
    $x = rand(0, $img_width/($length/1.5));
    $y = $font_size+2;
}

第 2 步:在 CodeIgniter 文件夹的根目录下创建一个名为 captcha 的文件夹(您可能已经拥有它)并在 apache 当前用户/组下授予它 755 的权限。

第 3 步:将您的 .ttf 自定义字体保存在 captcha/fonts 下。

第 4 步:您的 Controller 代码:

$this->load->helper('captcha');

$capache_config = array(
    'img_path' => 'captcha/',
    'img_url' => base_url() . 'captcha/',
    'img_width' => 190,
    'img_height' => 60,
    'font_size' => 20,
    'font_path' => FCPATH. 'captcha/font/verdana.ttf',
    'expiration' => 7200
);

/* Generate the captcha */
$captcha = create_captcha($capache_config);

if ($captcha !== FALSE) {
    echo $captcha['image'];
} else {
    die('No captcha was created');
}

第 5 步:示例输出:

关于php - codeigniter 验证码助手没有增加文本字体大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30122368/

有关php - codeigniter 验证码助手没有增加文本字体大小的更多相关文章

  1. ruby-on-rails - 如何验证 update_all 是否实际在 Rails 中更新 - 2

    给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru

  2. ruby - 具有身份验证的私有(private) Ruby Gem 服务器 - 2

    我想安装一个带有一些身份验证的私有(private)Rubygem服务器。我希望能够使用公共(public)Ubuntu服务器托管内部gem。我读到了http://docs.rubygems.org/read/chapter/18.但是那个没有身份验证-如我所见。然后我读到了https://github.com/cwninja/geminabox.但是当我使用基本身份验证(他们在他们的Wiki中有)时,它会提示从我的服务器获取源。所以。如何制作带有身份验证的私有(private)Rubygem服务器?这是不可能的吗?谢谢。编辑:Geminabox问题。我尝试“捆绑”以安装新的gem..

  3. ruby - 难道Lua没有和Ruby的method_missing相媲美的东西吗? - 2

    我好像记得Lua有类似Ruby的method_missing的东西。还是我记错了? 最佳答案 表的metatable的__index和__newindex可以用于与Ruby的method_missing相同的效果。 关于ruby-难道Lua没有和Ruby的method_missing相媲美的东西吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/7732154/

  4. ruby-on-rails - 如果为空或不验证数值,则使属性默认为 0 - 2

    我希望我的UserPrice模型的属性在它们为空或不验证数值时默认为0。这些属性是tax_rate、shipping_cost和price。classCreateUserPrices8,:scale=>2t.decimal:tax_rate,:precision=>8,:scale=>2t.decimal:shipping_cost,:precision=>8,:scale=>2endendend起初,我将所有3列的:default=>0放在表格中,但我不想要这样,因为它已经填充了字段,我想使用占位符。这是我的UserPrice模型:classUserPrice回答before_val

  5. ruby-on-rails - rails 目前在重启后没有安装 - 2

    我有一个奇怪的问题:我在rvm上安装了ruby​​onrails。一切正常,我可以创建项目。但是在我输入“railsnew”时重新启动后,我有“程序'rails'当前未安装。”。SystemUbuntu12.04ruby-v"1.9.3p194"gemlistactionmailer(3.2.5)actionpack(3.2.5)activemodel(3.2.5)activerecord(3.2.5)activeresource(3.2.5)activesupport(3.2.5)arel(3.0.2)builder(3.0.0)bundler(1.1.4)coffee-rails(

  6. ruby-on-rails - 如何验证非模型(甚至非对象)字段 - 2

    我有一个表单,其中有很多字段取自数组(而不是模型或对象)。我如何验证这些字段的存在?solve_problem_pathdo|f|%>... 最佳答案 创建一个简单的类来包装请求参数并使用ActiveModel::Validations。#definedsomewhere,atthesimplest:require'ostruct'classSolvetrue#youcouldevencheckthesolutionwithavalidatorvalidatedoerrors.add(:base,"WRONG!!!")unlesss

  7. ruby - 在没有 sass 引擎的情况下使用 sass 颜色函数 - 2

    我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re

  8. ruby-on-rails - 如何将验证与模型分开 - 2

    我有一些非常大的模型,我必须将它们迁移到最新版本的Rails。这些模型有相当多的验证(User有大约50个验证)。是否可以将所有这些验证移动到另一个文件中?说app/models/validations/user_validations.rb。如果可以,有人可以提供示例吗? 最佳答案 您可以为此使用关注点:#app/models/validations/user_validations.rbrequire'active_support/concern'moduleUserValidationsextendActiveSupport:

  9. ruby-on-rails - 跳过状态机方法的所有验证 - 2

    当我的预订模型通过rake任务在状态机上转换时,我试图找出如何跳过对ActiveRecord对象的特定实例的验证。我想在reservation.close时跳过所有验证!叫做。希望调用reservation.close!(:validate=>false)之类的东西。仅供引用,我们正在使用https://github.com/pluginaweek/state_machine用于状态机。这是我的预订模型的示例。classReservation["requested","negotiating","approved"])}state_machine:initial=>'requested

  10. ruby - 如何在 Rails 4 中使用表单对象之前的验证回调? - 2

    我有一个服务模型/表及其注册表。在表单中,我几乎拥有服务的所有字段,但我想在验证服务对象之前自动设置其中一些值。示例:--服务Controller#创建Action:defcreate@service=Service.new@service_form=ServiceFormObject.new(@service)@service_form.validate(params[:service_form_object])and@service_form.saverespond_with(@service_form,location:admin_services_path)end在验证@ser

随机推荐