Error creating captcha image "The http://localhost/project/index.php image contains errors and cannot be displayed"

Asked

Viewed 67 times

0

I’m studying a book in which the code is the same and even then it doesn’t work someone can tell me what could be happening?

<?php

$pass_chars = "";
for($i = 0; $i < CAPTCHA_NUMCHARS; $i++){
    $pass_chars .= chr(rand(97, 122)); //cria um codigo de letras de com 7 caracteres
}

            //CRIA A IMAGEM
            $img = imagecreatetruecolor(CAPTCHA_WIDTH, CAPTCHA_HEIGHT);
            //DEFINE O FUNDO BRANCO COM LETRAS PRETAS E GRAFICOS CINSAS
            $bg_color = imagecolorallocate($img, 255, 255, 255);//branco
            $text_color = imagecolorallocate($img, 0, 0, 0);//preto
            $graphic_color = imagecolorallocate($img, 70, 70, 70);//cinza

            //PREENCHE O FUNDO
            imagefilledrectangle($img, 0, 0, CAPTCHA_WIDTH, CAPTCHA_HEIGHT, $bg_color);

            //INSERE ALGUMAS LINHAS ALEATORIAS    
            for($i = 0; $i < 5; $i++){
                imageline($img, 0, rand() % CAPTCHA_HEIGHT, CAPTCHA_WIDTH, rand() % CAPTCHA_HEIGHT, $graphic_color);  
            }
            //INSERE ALGUNS PONTOS ALEATORIOS
            for($i = 0; $i < 50; $i++){
                imagesetpixel($img, rand() % CAPTCHA_HEIGHT, rand() % CAPTCHA_HEIGHT, $graphic_color);  
            }
            //DESENHA A STRING DA SENHA
                imagettftext($img, 18, 0, 5, CAPTCHA_HEIGHT - 5, $text_color, 'Courier New Bold.ttf', $pass_chars);

            //FAZ UM OUTPUT DA IMAGEM COMO JPEG, usando cabeçalho
                header("Content-type:image/png");
                imagepng($img);

                imagedestroy($img);

1 answer

1

I was able to solve I switched the source and after some pages the script of the book presents something that had not been presented until then I needed to start a session and put the character variable of the image inside an encrypted session variable follows part of the corrected code

<?php session_start(); $pass_chars = ""; for($i = 0; $i < CAPTCHA_NUMCHARS; $i++){ $pass_chars .= chr(rand(97, 122)); //cria um codigo de letras de com 7 caracteres } //arnazena a senha criptografada em uma variavel de sessão $_SESSION['pass_chars'] = sha1($pass_chars); //CRIA A IMAGEM $img = imagecreatetruecolor(CAPTCHA_WIDTH, CAPTCHA_HEIGHT); ...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.