Home > Article > Backend Development > What should I do if php fails to create an image?
The solution to the failure of php to create an image: first open the corresponding PHP file; then add the code "ob_clean();" before the header; and finally save the changes.
The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer
Solve the problem of GD images generated by php not being displayed
First look at the code that generates the image
<?php $img = @imagecreate(110, 20) or die("GD图像创建失败!"); $bg = imagecolorallocate($img, 255, 255, 255); $text_color = imagecolorallocate($img, 55, 55, 55); imagestring($img, 5, 0, 3, "I'm leo", $text_color); header('Content-Type:image/png'); imagepng($img); imagedestroy($img);
The above code is not displayed in the browser. After many calls, the image It cannot be displayed in the browser, only a small cracked icon that has not been loaded successfully.
Solution
After many searches, a function solved this problem.
That is to add the following code before the header:ob_clean(); // --清空(擦掉)输出缓冲区Note: Although the problem is solved, I still don’t understand why I need to do this. Could it be that after multiple calls, the buffer is still affected? If anyone knows, please leave a message with detailed explanation. Thank you! [Recommended learning: "
PHP Video Tutorial
"]The above is the detailed content of What should I do if php fails to create an image?. For more information, please follow other related articles on the PHP Chinese website!