简体   繁体   English

Wordpress 标题问题('Content-type:image/png');

[英]Wordpress problem with header('Content-type: image/png');

i'am trying to put a CAPTCHA in a wordpress custom plugin.我正在尝试将验证码放入 wordpress 自定义插件中。

I did follow that tutorial:我确实遵循了该教程:

https://code.tutsplus.com/tutorials/build-your-own-captcha-and-contact-form-in-php-.net-5362 https://code.tutsplus.com/tutorials/build-your-own-captcha-and-contact-form-in-php-.net-5362

<?php
session_start();
 
$permitted_chars = 'ABCDEFGHJKLMNPQRSTUVWXYZ';
  
function generate_string($input, $strength = 10) {
    $input_length = strlen($input);
    $random_string = '';
    for($i = 0; $i < $strength; $i++) {
        $random_character = $input[mt_rand(0, $input_length - 1)];
        $random_string .= $random_character;
    }
  
    return $random_string;
}
 
$image = imagecreatetruecolor(200, 50);
 
imageantialias($image, true);
 
$colors = [];
 
$red = rand(125, 175);
$green = rand(125, 175);
$blue = rand(125, 175);
 
for($i = 0; $i < 5; $i++) {
  $colors[] = imagecolorallocate($image, $red - 20*$i, $green - 20*$i, $blue - 20*$i);
}
 
imagefill($image, 0, 0, $colors[0]);
 
for($i = 0; $i < 10; $i++) {
  imagesetthickness($image, rand(2, 10));
  $line_color = $colors[rand(1, 4)];
  imagerectangle($image, rand(-10, 190), rand(-10, 10), rand(-10, 190), rand(40, 60), $line_color);
}
 
$black = imagecolorallocate($image, 0, 0, 0);
$white = imagecolorallocate($image, 255, 255, 255);
$textcolors = [$black, $white];
 
$fonts = [dirname(__FILE__).'/Acme.ttf'];
 
$string_length = 6;
$captcha_string = generate_string($permitted_chars, $string_length);
 
$_SESSION['captcha_text'] = $captcha_string;
 
for($i = 0; $i < $string_length; $i++) {
  $letter_space = 170/$string_length;
  $initial = 15;
   
  imagettftext($image, 24, rand(-15, 15), $initial + $i*$letter_space, rand(25, 45), $textcolors[rand(0, 1)], $fonts[array_rand($fonts)], $captcha_string[$i]);
}
 
header('Content-type: image/png');
imagepng($image);
imagedestroy($image);
?>

At first wordpress didn't find the font, but i manage to pass the error.起初wordpress没有找到字体,但我设法通过错误。 Then i call by shortcode the html. And i all i got is the alt and broken img.然后我通过短代码调用 html。我得到的只是 alt 和损坏的 img。 If i put directly the script in the function chrome tell me that my image contain errors.如果我直接将脚本放入 function chrome 告诉我我的图像包含错误。 I did try to add utf8 encode and to pass the image without the header, none of that work.我确实尝试添加 utf8 编码并在没有 header 的情况下传递图像,但这些都不起作用。

    public function CAPTCHA_shortcode_function() {
        ob_start();
        ?>
        <div class="elem-group">
            <label for="captcha">Please Enter the Captcha Text</label>
            <img src="CAPTCHA-generate-image.php" alt="CAPTCHA" class="captcha-image"><i class="fas fa-redo refresh-captcha"></i>
            <br>
            <input type="text" id="captcha" name="captcha_challenge" pattern="[A-Z]{6}">
        </div>
        <?php

        $CAPTCHA = ob_get_clean();

        return $CAPTCHA;

    }

If you have any idea, something must be wrong ^^ Thanks a lot !如果您有任何想法,一定是出了什么问题^^非常感谢!

We did find a solution:我们确实找到了解决方案:

    imagepng($captcha_image,"captcha_image.png");
    $imgData = file_get_contents('captcha_image.png');
    imagedestroy($captcha_image);

    // $imgData = ob_get_clean();

    return $imgData;

Then call it in the function like this然后在function这样调用

        ob_start();
        $imgData = $this->CAPTCHA_generate();

        ?>

        <div id="CAPTCHA_box">
            <label for="captcha">Entrez les </label><br>
            <?php echo '<img class="captcha_image" src="data:image/png;base64,'.base64_encode($imgData).'" />'; ?>
            <i class="fas fa-redo refresh_captcha"></i>
            <br>
            <input type="text" id="captcha" name="captcha_challenge" pattern="[A-Z]{6}">
        </div>

        <?php

        $CAPTCHA = ob_get_clean();

        return $CAPTCHA;

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

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