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

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

藏色散人
Release: 2023-03-08 16:44:01
Original
2042 people have browsed it

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);
Copy after login

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(); // --清空(擦掉)输出缓冲区
Copy after login
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!

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template