Home >PHP Framework >ThinkPHP >What should I do if thinkphp cannot display images?

What should I do if thinkphp cannot display images?

爱喝马黛茶的安东尼
爱喝马黛茶的安东尼Original
2019-08-26 10:17:304003browse

What should I do if thinkphp cannot display images?

I encountered a very strange BUG today. I wrote a PHP program that ran normally locally, but when it was published to Party A's server, a BUG failed to display the verification code.

What should I do if thinkphp cannot display images?

Related recommendations: "ThinkPHP Tutorial"

The specific manifestation is that a small cracked picture icon is displayed on the Chrome browser. Right-click the image to open it in a new window. It is still a small cracked image, not an error message (when the xdebug extension and PHP error reporting are enabled). If so, it means that there is no problem with the verification code URL routing and business logic code. There is a problem with the verification code generation process. My debugging idea is as follows:

1. Check whether the GD library is installed correctly. Linux recommends using yum or apt or pecl to install it. Windows recommends using phpStudy. Integrated environment, he has integrated commonly used extensions, just right-click to open them. (Applicable to situations where the verification code image displays an error message and a bunch of garbled characters when it opens in a new window. If there is an error message, please debug according to the error message)

What should I do if thinkphp cannot display images?

2. Check whether the font called by the verification code is missing. Some fonts called by the verification code components are not installed on the target computer. This requires analysis of the source code of the verification code module. I use ThinkPHP’s official think-captcha. You can find its source code at vendortopthinkthink-captchasrc. You can see here at vendortopthinkthink-captchaassets that this verification code plug-in comes with its own font, so ThinkPHP’s verification code is basically unnecessary. Considering the font issue, if it is another verification code plug-in, you still need to check this place.

3. Download the buggy verification code image and the local test normal verification code image, use Notepad to view and compare.

What should I do if thinkphp cannot display images?

#The picture above is the notepad of the verification code image in the local environment. We use a more professional hexadecimal editor to analyze the file differences.

What should I do if thinkphp cannot display images?

As shown in the picture, we found that the bad file bad has exactly the same content except for the addition of EF BB BF and other numbers in front of it, indicating that PHP has too many errors when rendering the image. These texts were rendered. As for the reason, I guess it may be that there is no professional code editor on the server, so the operation and maintenance personnel used ordinary Windows Notepad to modify the PHP code, resulting in the addition of this BOM header.

If it is the third reason, there are two solutions. One is to use ob_clean to clear the output buffer of php before calling the imagepng output image code of the verification code. There is also the use of some tools to remove the BOM of the code in batches.

What should I do if thinkphp cannot display images?

I use ob_clean here, add ob_clean() before line 203 of Captcha.php, the main file of think-captcha, so that it can be solved once and for all. If you have strict requirements on performance and stability, it is recommended to use tools to remove BOM in batches, which is safer. At the same time, it is also recommended that you install a professional code editor such as editplus on the server at any time. Do not use Windows Notepad to modify the code.

The above is the detailed content of What should I do if thinkphp 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