The solution to the problem that php pictures display garbled characters: 1. Open the corresponding PHP code file; 2. Add "header("Content-Type:image/jpg");" to the header of the page to declare the picture. Just type.
The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer
php picture shows a garbled code what to do?
The picture displays garbled characters:
Just add the following code to the head of the page, which is used to declare the picture type.
<?php header("Content-Type:image/jpg");//图片编码设置
Attachment:
PHP adds encoding to pictures
function setWater($dst_path, $save_path, $text) { // 创建图片的实例 $dst = imagecreatefromstring(file_get_contents($dst_path)); // 文字样式 $font = realpath('./font/arial.ttf'); $black = imagecolorallocate($dst, 255, 255, 255); //字体颜色 // 加文字 imagefttext($dst, 22, 0, 20, 40, $black, $font, $text); // 输出图片 list($dst_w, $dst_h, $dst_type) = getimagesize($dst_path); switch ($dst_type) { //GIF case 1: //header('Content-Type: image/gif'); $save_path = $save_path . $text .'.gif'; imagegif($dst, $save_path, 90); break; //JPG case 2: //header('Content-Type: image/jpeg'); $save_path = $save_path . $text .'.jpg'; imagejpeg($dst, $save_path, 90); break; //PNG case 3: //header('Content-Type: image/png'); $save_path = $save_path . $text .'.png'; imagepng($dst, $save_path, 90); break; default: break; } imagedestroy($dst); } $dst_path = '7002.jpg'; //$text = '7001'; //setWater($dst_path, './tmppic/', $text); for ($i=1; $i<=240; $i++) { $text = '00'. $i; $text = substr($text, -3); setWater($dst_path, './tmppic/', $text); } echo('ok');
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of What should I do if the php image displays garbled characters?. For more information, please follow other related articles on the PHP Chinese website!