Solution to the problem that PHP cannot display pictures using gd: 1. Save the file format as utf-8 format without BOM; 2. Use "ob_clean();" to clear the buffer.
The operating environment of this article: Windows 7 system, PHP version 7.1, DELL G3 computer
PHP uses the GD library to draw images and cannot display the problem
Draw image document style based on the official GD library
Original basic style
$width = 120; $height = 50; $img = @imagecreatetruecolor($width, $height) or die('Cannot Initialize new GD image stream');;//新建一个GD图像资源 $img_bgcolor = imagecolorallocate($img, mt_rand(157,255), mt_rand(157,255), mt_rand(157,255));//背景色 $img_textcolor = imagecolorallocate($img, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255));//字体颜色 $img_rectangle = imagefilledrectangle($img,0,0,$width, $height,$img_bgcolor);//画一个矩形图像 imagestring($img, 1, 5, 5, 'A Simple Text String', $img_textcolor); // 输出图像 header("Content-type:image/png"); imagepng($img); imagedestroy($img);//销毁图像
But it will not work in actual application The problem of normal display of images:
The reasons may be as follows:
Reason 1: The file encoding format has a BOM header. The solution is to use a third-party encoding tool, sublime Or notepad, etc., save the file format as utf-8 format without BOM
Cause 2: PHP buffer problem, the solution is to use ob_clean(); to clear the buffer.
Commonly used buffer function ob function
Reference: http://php.net/manual/zh/ref.outcontrol.php
ob_get_contents() - 返回输出缓冲区的内容 ob_flush() - 冲刷出(送出)输出缓冲区中的内容 ob_clean() - 清空(擦掉)输出缓冲区 ob_end_flush() - 冲刷出(送出)输出缓冲区内容并关闭缓冲 ob_end_clean() - 清空(擦除)缓冲区并关闭输出缓冲 flush() - 刷新输出缓冲
Judge whether the GD library is installed
function_exists('imagecreate') Judge get_extension_funcs() by judging whether the method provided by the extension library exists, but it is not comprehensive and this method is not recommended (because some extensions There is no function provided)
in_array('extension', get_loaded_extensions()) Determine whether the extension is installed by verifying whether it is in the loaded extension (comprehensive, but not concise)
extension_loade('Extension library name') Verify by judging whether the extension library is loaded (comprehensive and not concise, it is recommended to use this method to verify whether the extension is installed and loaded)
Recommended learning: "PHP Video Tutorial 》
The above is the detailed content of What should I do if PHP cannot display the picture using GD?. For more information, please follow other related articles on the PHP Chinese website!

The article compares ACID and BASE database models, detailing their characteristics and appropriate use cases. ACID prioritizes data integrity and consistency, suitable for financial and e-commerce applications, while BASE focuses on availability and

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

Article discusses best practices for PHP input validation to enhance security, focusing on techniques like using built-in functions, whitelist approach, and server-side validation.

The article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand

The article discusses the benefits of using password_hash and password_verify in PHP for securing passwords. The main argument is that these functions enhance password protection through automatic salt generation, strong hashing algorithms, and secur

The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

The article discusses strategies to prevent XSS attacks in PHP, focusing on input sanitization, output encoding, and using security-enhancing libraries and frameworks.

The article discusses the use of interfaces and abstract classes in PHP, focusing on when to use each. Interfaces define a contract without implementation, suitable for unrelated classes and multiple inheritance. Abstract classes provide common funct


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 English version
Recommended: Win version, supports code prompts!

WebStorm Mac version
Useful JavaScript development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SublimeText3 Linux new version
SublimeText3 Linux latest version