Home  >  Article  >  Backend Development  >  What should I do if php fails to create an image?

What should I do if php fails to create an image?

藏色散人
藏色散人Original
2021-03-11 17:59:201983browse

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.

What should I do if php fails to create an image?

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&#39;m leo", $text_color);
header(&#39;Content-Type:image/png&#39;);
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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn