Home > Article > Backend Development > Solution to why php cannot display images
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.
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:
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, 'GD Library', 0xFFBA00); //echo "这一行如果加上就不能正常显示下面的图像了。"; // 输出图像到浏览器 header('Content-Type: image/gif'); 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!