Search code examples
phpxamppgd

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


I am pretty new to PHP and currently using 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. 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. I have uninstalled and reinstalled 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);
  ?>

Black screen error image

Edit: I have been able to pinpoint the problem to the header(Content-type) line. Still haven't figured the solution yet.


Solution

  • 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

    When I decide to create an image file rather than sending the image through a 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.

    The solution that worked for me was to place ob_clean() before header line.

    For more explanation see the link below. 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.