Home >Backend Development >PHP Problem >Solution to why php cannot display images

Solution to why php cannot display images

藏色散人
藏色散人Original
2020-11-20 10:04:155540browse

php cannot display images because there are other outputs in the source code besides img output. The solution is to cancel any output before the header is called.

Solution to why php cannot display images

Recommended: "PHP Video Tutorial"

Specific questions:

php files can output pictures but cannot display them!

Use

imagegif($image,'verify.php');
echo <img  scr=‘verify.php’/ alt="Solution to why php cannot display images" >;

to output pictures.

But changing it to

header ( "content-type:image/gif" );
imagegif($image);

can output pictures. But it can't be displayed. There are no errors in the file.

How can I display it? . . .

Solved. Add ob_end_clean();

in the first line.

Solution to why php cannot display images

##Solution:

After testing Both methods can be displayed correctly. It is estimated that the reason why your second method cannot be displayed normally is:

In the second method, in addition to the img output, there are other outputs.

Please refer to the sample code below:

<?php
// 创建新的图像实例
$im = imagecreatetruecolor(100, 100);
// 设置背景为白色
imagefilledrectangle($im, 0, 0, 99, 99, 0xFFFFFF);
//在图像上写字
imagestring($im, 3, 40, 20, &#39;GD Library&#39;, 0xFFBA00);
 
//echo "这一行如果加上就不能正常显示下面的图像了。";
 
// 输出图像到浏览器
header(&#39;Content-Type: image/gif&#39;);
imagegif($im);
 
imagedestroy($im);
?>

Why the second method cannot have content in front of the header. To find out the reason, please refer to the following explanation:

Header() must be called before any actual output, whether it is an ordinary html tag, a blank line or space in a file, or a blank line or space in a PHP file.

Simple sentence: If there is output before header() is called, an error will occur.

The above is the detailed content of Solution to why php cannot display images. 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
Previous article:How to modify xml in phpNext article:How to modify xml in php