Home >Backend Development >PHP Problem >What should I do if the php image displays garbled characters?

What should I do if the php image displays garbled characters?

藏色散人
藏色散人Original
2021-11-09 09:46:324363browse

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.

What should I do if the php image displays garbled characters?

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(&#39;./font/arial.ttf&#39;);
    $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(&#39;Content-Type: image/gif&#39;);
            $save_path = $save_path . $text .&#39;.gif&#39;;
            imagegif($dst, $save_path, 90);
            break;
        //JPG
        case 2:
            //header(&#39;Content-Type: image/jpeg&#39;);
            $save_path = $save_path . $text .&#39;.jpg&#39;;
            imagejpeg($dst, $save_path, 90);
            break;
        //PNG
        case 3:
            //header(&#39;Content-Type: image/png&#39;);
            $save_path = $save_path . $text .&#39;.png&#39;;
            imagepng($dst, $save_path, 90);
            break;
        default:
            break;
    }

    imagedestroy($dst);

}

$dst_path = &#39;7002.jpg&#39;;
//$text     = &#39;7001&#39;;
//setWater($dst_path, &#39;./tmppic/&#39;, $text);

for ($i=1; $i<=240; $i++) {
    $text = &#39;00&#39;. $i;
    $text = substr($text, -3);
    
    setWater($dst_path, &#39;./tmppic/&#39;, $text);
}

echo(&#39;ok&#39;);

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!

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