What should I do if PHP cannot display the picture using GD?
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!

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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

To work on file upload we are going to use the form helper. Here, is an example for file upload.

Validator can be created by adding the following two lines in the controller.

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.
