简体   繁体   English

PHP gd 库 displsys 黑屏,带有白框而不是图像

[英]PHP gd library displsys black screen with white box instead of image

I am pretty new to PHP and currently using XAMPP.我对 PHP 还很陌生,目前正在使用 XAMPP。 I wanted to create a captcha image using the gd library but then I noticed that all I got was a black screen with a little white box outline at the middle.我想使用 gd 库创建一个验证码图像,但后来我注意到我得到的只是一个黑屏,中间有一个小白框轮廓。 This is the output in spite of any code used.尽管使用了任何代码,但这是输出。 I have tried different code samples from different websites to no avail.我尝试了来自不同网站的不同代码示例,但无济于事。 I have confirmed that the gd library has been enabled.我已确认 gd 库已启用。 I have uninstalled and reinstalled xampp.我已经卸载并重新安装了 xampp。 I have also searched related questions but none of the suggested solutions works for me.我也搜索了相关问题,但建议的解决方案都不适合我。

here is my code这是我的代码

 session_start();

 // Set some important CAPTCHA constants
 define('CAPTCHA_NUMCHARS', 6);  // number of characters in pass-phrase
define('CAPTCHA_WIDTH', 100);   // width of image
define('CAPTCHA_HEIGHT', 25);   // height of image

// Generate the random pass-phrase
$pass_phrase = "";
for ($i = 0; $i < CAPTCHA_NUMCHARS; $i++) {
  $pass_phrase .= chr(rand(97, 122));
}

// Store the encrypted pass-phrase in a session variable
$_SESSION['pass_phrase'] = SHA1($pass_phrase);

// Create the image
$img = imagecreatetruecolor(CAPTCHA_WIDTH, CAPTCHA_HEIGHT); 
$bg_color = imagecolorallocate($img, 255, 255, 255);     // white
$text_color = imagecolorallocate($img, 0, 0, 0);         // black
$graphic_color = imagecolorallocate($img, 64, 64, 64);   // dark gray

// Fill the background
imagefilledrectangle($img, 0, 0, CAPTCHA_WIDTH, CAPTCHA_HEIGHT, $bg_color);

// Draw some random lines
for ($i = 0; $i < 5; $i++) {
  imageline($img, 0, rand() % CAPTCHA_HEIGHT, CAPTCHA_WIDTH, rand() % 
CAPTCHA_HEIGHT, 
  $graphic_color);
}

// Sprinkle in some random dots
for ($i = 0; $i < 50; $i++) {
  imagesetpixel($img, rand() % CAPTCHA_WIDTH, rand() % CAPTCHA_HEIGHT, 
$graphic_color);
}
// Draw the pass-phrase string
imagettftext($img, 18, 0, 5, CAPTCHA_HEIGHT - 5, $text_color, 'Courier New Bold.ttf', $pass_phrase);

// Output the image as a PNG using a header
header("Content-type: image/png");
imagepng($img);

// Clean up
imagedestroy($img);
  ?>

黑屏错误图片

Edit: I have been able to pinpoint the problem to the header(Content-type) line.编辑:我已经能够将问题查明到 header(Content-type) 行。 Still haven't figured the solution yet.目前还没有想出解决办法。

After hours of scouring different forums.经过几个小时的搜索不同的论坛。 I have come to understand the problem better.我已经更好地理解了这个问题。 The problem is not with gd library but rather the header(Content-type) line问题不在于 gd 库,而在于 header(Content-type) 行

When I decide to create an image file rather than sending the image through a header.当我决定创建一个图像文件而不是通过 header 发送图像时。 The correctly displays.正确显示。

Once I figured that out, finding the solution became easier as now I am looking for the solution in the right place.一旦我弄清楚了,找到解决方案就变得更容易了,因为现在我正在正确的地方寻找解决方案。

The problem is that my PHP script is emitting a UTF-8 byte order mark (EF BB BF) before outputting the PNG image content.问题是我的 PHP 脚本在输出 PNG 图像内容之前发出了 UTF-8 字节顺序标记(EF BB BF)。

The solution that worked for me was to place ob_clean() before header line.对我有用的解决方案是将 ob_clean() 放在 header 行之前。

For more explanation see the link below.有关更多说明,请参阅下面的链接。 https://stackoverflow.com/a/22565248/10349485 https://stackoverflow.com/a/22565248/10349485

I used and pieced together different solutions from different forums to better understand and to arrive at this solution but the link above was my final destination.我使用并拼凑了来自不同论坛的不同解决方案,以更好地理解并得出这个解决方案,但上面的链接是我的最终目的地。

I am not deleting the question even though there are other similar questions because of the amount of trouble it took me to get and understand this problem, also because the answers there didn't work for me.即使还有其他类似的问题,我也不会删除该问题,因为我费了很大力气才能理解和理解这个问题,也因为那里的答案对我不起作用。 Hopefully, this helps someone in the future avoid the trouble I went through.希望这可以帮助将来的人避免我遇到的麻烦。

from many days i'm also face this problem but this solution work for me use __DIR__ with your font file path like that很多天以来,我也遇到了这个问题,但这个解决方案对我有用,使用__DIR__和你的字体文件路径

           `$font_path = __DIR__ .'/font.ttf';`

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

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