Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4026834
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T11:01:17+00:00 2026-05-20T11:01:17+00:00

I am creating a captcha image (please do not suggest premade ones). It gives

  • 0

I am creating a captcha image (please do not suggest premade ones).

It gives you the ability to call

$captcha = new captcha();
$captcha->size(30)->getImage();

which sets the font size to 30 and then generates the captcha.

my issue is with sizing.

How can I work out how wide the image should be?
the captcha has 6 characters.
I thought I could just do ($size*6)+10 to make the image wide enough + give it a 5px padding on each side (text is centered), but apparently not.


Code

<?php

session_start();

class captcha {

    private $font = 'monofont.ttf';
    private $characters = '23456789bcdfghjkmnpqrstvwxyz';
    private $size = 30;
    private $count = 6;
    private $colors = array(
        'b' => array('r' => 0,   'g' => 0,   'b' => 0),
        't' => array('r' => 200, 'g' => 200, 'b' => 200),
        'n' => array('r' => 127, 'g' => 127, 'b' => 127)
    );


    function count($count) {
        $this->count = $count;
        return $this;
    }

    function size($size){
        $this->size = $size;
        return $this;
    }

    function characters($characters) {
        $this->characters = $characters;
        return $this;
    }

    function backgroundColor($red, $green, $blue){
        $this->colors['b']['r'] = $red;
        $this->colors['b']['g'] = $green;
        $this->colors['b']['b'] = $blue;
        return $this;
    }

    function noiseColor($red, $green, $blue){
        $this->colors['n']['r'] = $red;
        $this->colors['n']['g'] = $green;
        $this->colors['n']['b'] = $blue;
        return $this;
    }

    function textColor($red, $green, $blue){
        $this->colors['t']['r'] = $red;
        $this->colors['t']['g'] = $green;
        $this->colors['t']['b'] = $blue;
        return $this;
    }

    function generateCode() {
        $code = '';
        $i = 0;
        while ($i < $this->count) {
            $code .= strtoupper(substr($this->characters, mt_rand(0, strlen($this->characters) - 1), 1));
            $i++;
        }
        return $code;
    }

    function getWidth() {
        return ($this->count * $this->size);
    }

    function getHeight() {
        return $this->size+10;
    }


    function getCaptcha() {

        $code = $this->generateCode();
        $this->width = $this->getWidth();
        $this->height = $this->getHeight();

        $image = @imagecreate($this->width, $this->height) or die('Cannot initialize new GD image stream');

        //define colors
        $tColor = imagecolorallocate($image, $this->colors['t']['r'], $this->colors['t']['g'], $this->colors['t']['b']);
        $nColor = imagecolorallocate($image, $this->colors['n']['r'], $this->colors['n']['g'], $this->colors['n']['b']);
        $bColor = imagecolorallocate($image, $this->colors['b']['r'], $this->colors['b']['g'], $this->colors['b']['b']);

        //fill image
        imagefill($image, 0, 0, $bColor);

        /* generate random dots in background */

        for ($i = 0; $i < ($this->width * $this->height) / 3; $i++) {

            imagefilledellipse($image, mt_rand(0, $this->width), mt_rand(0, $this->height), 1, 1, $nColor);

        }

        /* generate random lines in background */

        for ($i = 0; $i < ($this->width * $this->height) / 150; $i++) {

            imageline($image, mt_rand(0, $this->width), mt_rand(0, $this->height), mt_rand(0, $this->width), mt_rand(0, $this->height), $nColor);
        }

        /* create textbox and add text */

        $textbox = imagettfbbox($this->size, 0, $this->font, $code) or die('Error in imagettfbbox function');

        $x = ($this->width - $textbox[4]) / 2;

        $y = ($this->height - $textbox[5]) / 2;

        imagettftext($image, $this->size, 0, $x, $y, $tColor, $this->font, $code) or die('Error in imagettftext function');
        //imagefilter($image, IMG_FILTER_NEGATE);
        imagefilter($image, IMG_FILTER_SMOOTH, 1);

        /* output captcha image to browser */

        header('Content-Type: image/jpeg');

        header('Cache-Control: no-cache, must-revalidate');

        imagejpeg($image);

        imagedestroy($image);

        $_SESSION['security_code'] = $code;

    }
}

$captcha = new captcha();
$captcha->size(30)->count(6)->getCaptcha();

?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T11:01:18+00:00Added an answer on May 20, 2026 at 11:01 am

    You can use imagegetttfbbox() to determine the bounding box which can contain your string. But since you’re generating a captcha, which means the characters are highly distored, I don’t think it’d particularly accurate. It’s intended for regular un-altered text.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Creating an installer for possible remote systems so that if they do not have
Creating a table without tbody using javascript createElement/appendChild will not add tbody in Firebug
Creating extensions got much easier with Vs2010, but this seems not to be the
i am creating a registration form for that i want to implement the captcha
Creating a layout in Java as the number of TableLayouts required is not known
The recaptcha API allows pure JavaScript captcha creating and verifying . But, how can
I am creating a custom captcha where random numbers are generated and you have
I have a user registration form in PHP .I put captcha image check in
I am creating a CAPTCHA input using SimpleCaptcha, and did validation of the Captcha
I am creating a signup application with validation and Captcha (using SimpleCaptcha). I am

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.