php圖片顯示一片亂碼的解決方法:1、開啟對應的PHP程式碼檔案;2、在頁面頭部加上「header("Content-Type:image/jpg");」程式碼宣告圖片類型即可。

本文操作環境:windows7系統、PHP7.1版、DELL G3電腦
php圖片顯示一片亂碼怎麼辦?
圖片顯示亂碼:
頁面頭加上以下程式碼即可,是用來宣告圖片類型的。
<?php
header("Content-Type:image/jpg");//图片编码设置附:
PHP在圖片上加上編碼
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');推薦學習:《PHP影片教學》
以上是php圖片顯示一片亂碼怎麼辦的詳細內容。更多資訊請關注PHP中文網其他相關文章!