简体   繁体   English

生成验证码图像 PHP

[英]Generate Captcha Image PHP

I want to generate Captcha Image using PHP imagettftext.我想使用 PHP imagettftext 生成验证码图像。 And following is my code.以下是我的代码。 It gives an empty image.它给出了一个空图像。 I want to resolve this issue.我想解决这个问题。

I have tested the code but was unable to find why the image is not bieng showed although my random text is being passed to the function of captcha image generation.我已经测试了代码,但无法找到为什么图像没有 bieng 显示,尽管我的随机文本被传递给验证码图像生成功能。

<?php

    function generateRandomString($length = 10) {
        $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $randomString = '';
        for ($i = 0; $i < $length; $i++) {
            $randomString .= $characters[rand(0, strlen($characters) - 1)];
        }
        return $randomString;
    }

    function generateCaptchaImage($text = 'good'){
        echo $text;
        echo 'OK';
        // Set the content-type
        header('Content-Type: image/png');

        $width = 200;
        $height = 30;
        $font = 'ThisisKeSha.ttf';

        // Create the image
        $im = imagecreatetruecolor($width, $height);

        //ADD NOISE - DRAW background squares
        $square_count = 6;
        for ($i = 0; $i < 10; $i++) {
          $cx = (int)rand(0, $width/2);
          $cy = (int)rand(0, $height);
          $h  = $cy + (int)rand(0, $height/5);
          $w  = $cx + (int)rand($width/3, $width);
          imagefilledrectangle($im, $cx, $cy, $w, $h, $white);
        }

        //ADD NOISE - DRAW ELLIPSES
        $ellipse_count = 5;
        for ($i = 0; $i < $ellipse_count; $i++) {
          $cx = (int)rand(-1*($width/2), $width + ($width/2));
          $cy = (int)rand(-1*($height/2), $height + ($height/2));
          $h  = (int)rand($height/2, 2*$height);
          $w  = (int)rand($width/2, 2*$width);
          imageellipse($im, $cx, $cy, $w, $h, $grey);
        }

        // Add the text
        imagettftext($im, 20, 0, 10, 20, $black, $font, $text);

        imagepng($im);

    }

    $randomString = generateRandomString();
    generateCaptchaImage($randomString);

    ?>

Try this code;试试这个代码;

<?php
    function generateRandomString($length = 10) {
        $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $randomString = '';
        for ($i = 0; $i < $length; $i++) {
            $randomString .= $characters[rand(0, strlen($characters) - 1)];
        }
        return $randomString;
    }
    function generateCaptchaImage($text = 'good'){
        // Set the content-type
        header('Content-Type: image/png');
        $width  = 200;
        $height = 30;
        // Create the image
        $im = imagecreatetruecolor($width, $height);

        // Create some colors
        $white  = imagecolorallocate($im, 255, 255, 255);
        $grey   = imagecolorallocate($im, 128, 128, 128);
        $black  = imagecolorallocate($im, 0, 0, 0);
        imagefilledrectangle($im, 0, 0, 399, 29, $white);

        //ADD NOISE - DRAW background squares
        $square_count = 6;
        for($i = 0; $i < $square_count; $i++){
            $cx = rand(0,$width);
            $cy = (int)rand(0, $width/2);
            $h  = $cy + (int)rand(0, $height/5);
            $w  = $cx + (int)rand($width/3, $width);
            imagefilledrectangle($im, $cx, $cy, $w, $h, $white);
        }

        //ADD NOISE - DRAW ELLIPSES
        $ellipse_count = 5;
        for ($i = 0; $i < $ellipse_count; $i++) {
          $cx = (int)rand(-1*($width/2), $width + ($width/2));
          $cy = (int)rand(-1*($height/2), $height + ($height/2));
          $h  = (int)rand($height/2, 2*$height);
          $w  = (int)rand($width/2, 2*$width);
          imageellipse($im, $cx, $cy, $w, $h, $grey);
        }

        // Replace path by your own font path
        $font = 'ThisisKeSha.ttf';

        // Add some shadow to the text
        imagettftext($im, 20, 0, 11, 21, $grey, $font, $text);

        // Add the text
        imagettftext($im, 20, 0, 10, 20, $black, $font, $text);

        // Using imagepng() results in clearer text compared with imagejpeg()
        imagepng($im);
        imagedestroy($im);
    }

    $randomString = generateRandomString();
    generateCaptchaImage($randomString);
?>

You didnt generated the colors $white , $black and $grey .您没有生成$white$black$grey Also the font must be there in the same folder of this php file.此外,字体必须位于此 php 文件的同一文件夹中。

Solved from comments从评论中解决

  • $white and $black are not defined. $white$black没有定义。
  • Remove echo $text;删除echo $text; and echo 'OK';echo 'OK'; . .
  • Is ThisisKeSha.ttf on your server and in the same directory as this? ThisisKeSha.ttf是否在您的服务器上并与它位于同一目录中?

$white and $black were not defined. $white$black没有定义。

Nice Captcha image generator:不错的验证码图像生成器:

class captcha{
    public static function create(){

        $image = imagecreatetruecolor(160, 40);
        imagefilledrectangle($image, 0, 0, 160, 40, imagecolorallocate($image, 244, 245, 245));

        $code="";
        for($i=0;$i<6;$i++){
            $color = imagecolorallocate($image, rand(160,230), rand(160,240), rand(160,250));
            imageline($image, rand(10,140), rand(2,35), rand(10,140), rand(5,35), $color) ;
        }
        for($i=1;$i<6;$i++){
            $color = imagecolorallocate($image, rand(50,150), rand(50,140), rand(50,150));
            $char = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
            $num = $char[rand(0, strlen($char)-1)];
            $code .= $num;
            //use a proper font file like verdana.ttf and change the font name at bellow
            imagefttext($image, rand(14, 18), rand(-10,40), $i*25, rand(24,34), $color, __DIR__."/verdana.ttf", $num);
        }

        imagejpeg($image,"cap.jpg");

    }
}    

captcha::create();

echo "<img src=\"cap.jpg\">";

It's a easy way to create captacha in PHP, You can try this, 这是在PHP中创建captacha的简单方法,你可以尝试这个,

<?php
@session_start();
header("Content-type: image/png");
$_SESSION["captcha"] = substr(md5(time()),0,5);
$im = imagecreate(110, 30);
$white = imagecolorallocate($im, 244, 255, 255);
$red = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 0, 0, 0);
$size = $_SESSION["captcha"];
$text = "$size";
$font = 'comic.TTF';
imagettftext($im, 20, 0, 25, 20, $red, $font, $text);
imagettftext($im, 20, 0, 25, 20, $black, $font, $text);
imagepng($im);
imagedestroy($im);
?>

php session used on this. 用于此的php会话。 Here is more details about Captcha in PHP 这里有关于PHP中的Captcha的更多细节

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM