導航:首頁 > 編程語言 > phpimagestring亂碼

phpimagestring亂碼

發布時間:2022-10-04 00:55:11

php生成圖片,網頁上顯示一堆亂碼,哪裡設置錯了,求大神啊!

給你這個代碼用吧

我給你改好了

<?php
session_start();

header("content-type:image/png");//設置創建圖像的格式

$image_width=70;//設置圖像寬度

$image_height=18;//設置圖像高度

srand(microtime()*100000);//設置隨機數的種子

for($i=0;$i<4;$i++){//循環輸出一個4位的隨機數

$new_number.=dechex(rand(0,15));

}

$_SESSION['code']=$new_number;//將獲取的隨機數驗證碼寫入到SESSION變數中


$num_image=imagecreate($image_width,$image_height);//創建一個畫布

imagecolorallocate($num_image,255,255,255);//設置畫布的顏色

for($i=0;$i<strlen($_SESSION['code']);$i++){//循環讀取SESSION變數中的驗證碼

$font=mt_rand(3,5);//設置隨機的字體

$x=mt_rand(1,8)+$image_width*$i/4;//設置隨機字元所在位置的X坐標

$y=mt_rand(1,$image_height/4);//設置隨機字元所在位置的Y坐標

$color=imagecolorallocate($num_image,mt_rand(0,100),mt_rand(0,150),mt_rand(0,200));//設置字元的顏色

imagestring($num_image,$font,$x,$y,$_SESSION['code'][$i],$color);//水平輸出字元

}

imagepng($num_image);//生成PNG格式的圖像

imagedestroy($num_image);//釋放圖像資源
?>

⑵ php在製作圖形中用imagestring函數寫入的漢字出來的卻是亂碼怎麼改成漢字

需要將漢字轉為UTF-8編碼。

iconv('GBK','UTF-8','漢字')

⑶ 用php畫圖時出現亂碼,代碼哪裡需要改的嗎

這樣寫
不要有html代碼,而且你最後一個還寫錯了imagegjpeg($im);,應該是imagejpeg($im);header信息最好在上面
<?php
header('Content-Type: image/jpeg');
$im = imagecreatetruecolor(120, 20);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5, 'A Simple Text String', $text_color);
imagejpeg($im);
?>

⑷ 求助!php製作上傳圖片時候遇到的問題

<?php

/*
* 參數說明
* $max_file_size : 上傳文件大小限制, 單位BYTE
* $destination_folder : 上傳文件路徑
* $watermark : 是否附加水印(1為加水印,其他為不加水印);
*
* 使用說明:
* 1. 將PHP.INI文件裡面的"extension=php_gd2.dll"一行前面的;號去掉,因為我們要用到GD庫;
* 2. 將extension_dir =改為你的php_gd2.dll所在目錄;
*/

// 上傳文件類型列表

$uptypes = array (
'image/jpg',
'image/png',
'image/jpeg',
'image/pjpeg',
'image/gif',
'image/bmp',
'image/x-png'
);

$max_file_size = 20000000; //上傳文件大小限制,單位BYTE

$destination_folder = 'uploadimg/'; //上傳文件路徑

$watermark = 1; //是否附加水印(1為加水印,其他為不加水印);

$watertype = 1; //水印類型(1為文字,2為圖片)

$waterposition = 1; //水印位置(1為左下角,2為右下角,3為左上角,4為右上角,5為居中);

$waterstring = "http://www.xplore.cn/"; //水印字元串

$waterimg = "xplore.gif"; //水印圖片

$imgpreview = 1; //是否生成預覽圖(1為生成,其他為不生成);

$imgpreviewsize = 1 / 2; //縮略圖比例

?>
<html>
<head>
<title>ZwelL圖片上傳程序</title>
</head>
<body>
<form id="upfile" name="upform" enctype="multipart/form-data" method="post" action="">
<label for="upfile">上傳文件:</label>
<input type="file" name="upfile" id="fileField" />
<input type="submit" name="submit" value="上傳"/>

</form>

<?php

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
//判斷是否有上傳文件

if (is_uploaded_file($_FILES['upfile']['tmp_name'])) {
$upfile = $_FILES['upfile'];
print_r($_FILES['upfile']);

$name = $upfilep['name']; //文件名

$type = $upfile['type']; //文件類型

$size = $upfile['size']; //文件大小

$tmp_name = $upfile['tmp_name']; //臨時文件

$error = $upfile['error']; //出錯原因

if ($max_file_size < $size) { //判斷文件的大小

echo '上傳文件太大';
exit ();
}

if (!in_arrar($type, $uptypes)) { //判斷文件的類型

echo '上傳文件類型不符' . $type;
exit ();
}

if (!file_exists($destination_folder)) {
mkdir($destination_folder);
}

if (file_exists("upload/" . $_FILES["file"]["name"])) {
echo $_FILES["file"]["name"] . " already exists. ";
} else {
move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}

$pinfo = pathinfo($name);
$ftype = $pinfo['extension'];
$destination = $destination_folder . time() . "." . $ftype;
if (file_exists($destination) && $overwrite != true) {
echo "同名的文件已經存在了";
exit ();
}

if (!move_uploaded_file($tmp_name, $destination)) {
echo "移動文件出錯";
exit ();
}

$pinfo = pathinfo($destination);
$fname = $pinfo[basename];
echo " <font color=red>已經成功上傳</font><br>文件名: <font color=blue>" .$destination_folder . $fname . "</font><br>";
echo " 寬度:" . $image_size[0];
echo " 長度:" . $image_size[1];
echo "<br> 大小:" . $file["size"] . " bytes";

if ($watermark == 1) {
$iinfo = getimagesize($destination, $iinfo);
$nimage = imagecreatetruecolor($image_size[0], $image_size[1]);
$white = imagecolorallocate($nimage, 255, 255, 255);
$black = imagecolorallocate($nimage, 0, 0, 0);
$red = imagecolorallocate($nimage, 255, 0, 0);
imagefill($nimage, 0, 0, $white);
switch ($iinfo[2]) {
case 1 :
$simage = imagecreatefromgif($destination);
break;
case 2 :
$simage = imagecreatefromjpeg($destination);
break;
case 3 :
$simage = imagecreatefrompng($destination);
break;
case 6 :
$simage = imagecreatefromwbmp($destination);
break;
default :
die("不支持的文件類型");
exit;
}

image($nimage, $simage, 0, 0, 0, 0, $image_size[0], $image_size[1]);
imagefilledrectangle($nimage, 1, $image_size[1] - 15, 80, $image_size[1], $white);

switch ($watertype) {
case 1 : //加水印字元串

imagestring($nimage, 2, 3, $image_size[1] - 15, $waterstring, $black);
break;
case 2 : //加水印圖片

$simage1 = imagecreatefromgif("xplore.gif");
image($nimage, $simage1, 0, 0, 0, 0, 85, 15);
imagedestroy($simage1);
break;
}

switch ($iinfo[2]) {
case 1 :
//imagegif($nimage, $destination);

imagejpeg($nimage, $destination);
break;
case 2 :
imagejpeg($nimage, $destination);
break;
case 3 :
imagepng($nimage, $destination);
break;
case 6 :
imagewbmp($nimage, $destination);
//imagejpeg($nimage, $destination);

break;
}

//覆蓋原上傳文件

imagedestroy($nimage);
imagedestroy($simage);

}

if ($imgpreview == 1) {
echo "<br>圖片預覽:<br>";
echo "<img src=\"" . $destination . "\" width=" . ($image_size[0] *$imgpreviewsize) . " height=" . ($image_size[1] * $imgpreviewsize);
echo " alt=\"圖片預覽:\r文件名:" . $destination . "\r上傳時間:\">";
}

}

}
?>
</body>
</html>
你對比一下,就知道原因了,祝你成功

⑸ php驗證碼字體的問題

//設置字體樣式
$font=dirname(__FILE__).'/font/arial.ttf';

$font在提供的代碼里其實沒有用到,而它正是用來指定字體的。

.ttf文件是一個字體庫,這種文件在系統中其實很多,你可以在C盤搜索一下.ttf,也可以到網上去下載喜歡的。

然後將$font賦值為.ttf文件所在的路徑,或者將.ttf文件復制到$font設置的路徑。


//輸出驗證碼
for($i=0;$i<strlen($_SESSION['code']);$i++){
imagestring($_img,$font_size,$i*$_width/$codelen+mt_rand(1,10),mt_rand(1,$_height/2),$_SESSION['code'][$i],$font_color);
}

這段是畫驗證碼的代碼,但是imagestring方法不提供字體設置,要換成imagettftext方法。

修改如下

for($i=0;$i<strlen($_SESSION['code']);$i++){
imagettftext($_img,$font_size,0,$i*$_width/$codelen+mt_rand(1,10),mt_rand(1,$_height/2),$font_color,$font,$_SESSION['code'][$i]);
}

⑹ php圖像存在錯誤顯示不出來

看了代碼後,有以下提示供參考:


1、從代碼中,沒有看到輸出驗證字元圖像的代碼。


2、建議編寫類代碼時,添加一個__construct構造函數,用於對上面的 $img 等重要參數進行初始化。


3、代碼中生成驗證字元的代碼有些bug,會造成輸出字元不足4個:


$this->code.=$this->charset[mt_rand(0,$_len)];


修正:


$this->code.=$this->charset[mt_rand(0,$_len-1)];


運行結果圖:


⑺ php驗證碼輸出全是亂碼

看代碼是沒得問題的,

  1. 訪問驗證碼生成方法/函數的路徑不對;

2.文件編碼不對,UTF8編碼沒有去掉BOM頭;

3.驗證碼生成方法有問題。
4.環境不支持。

⑻ 初學php生成圖像,輸出到瀏覽器中,看不了圖片,是什麼問題

<?php
header("content-type:image/png");
$im = @imagecreatetruecolor(120, 20) or die('Cannot Initialize new GD image stream');
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5, 'A Simple Text String', $text_color);
imagepng($im);
imagedestroy($im);
?>

你這個是要輸出圖片,因此只需要這部分就可以了。你的出現亂碼是因為你header之前輸出了<html><head>等等東西導致header失敗。

⑼ php做驗證碼輸出圖片顯示的是亂碼,怎麼破

把第一個header()去掉,第二個header()裡面寫錯了
<?php
header("Content-Type:image/jpg");
$image=imagecreate(300,80);
$bgcolor=imagecolorallocate($image,200,60,90);
$write=imagecolorallocate($image,0,0,0);
imagestring($image,5,80,30,"I Like PHP",$write);
imagejpeg($image);
imagedestroy($image);
?>

⑽ PHP寫的圖形驗證碼亂碼

我吧我的給你用吧

class wxhVerify {

static function imageVerify($length=4, $mode=1, $width=48, $height=22, $verifyName='verify') {
$randval = randomString($length, $mode);
$_SESSION[$verifyName] = md5($randval);
$width = ($length * 10 + 10) > $width ? $length * 10 + 10 : $width;
$im = @imagecreatetruecolor($width, $height);
$r = Array(225, 255, 255, 223);
$g = Array(225, 236, 237, 255);
$b = Array(225, 236, 166, 125);
$key = mt_rand(0, 3);
//隨機背景色
$backColor = imagecolorallocate($im, $r[$key], $g[$key], $b[$key]);
//邊框色
$borderColor = imagecolorallocate($im, 100, 100, 100);
//點顏色
$pointColor = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
@imagefilledrectangle($im, 0, 0, $width - 1, $height - 1, $backColor);
@imagerectangle($im, 0, 0, $width - 1, $height - 1, $borderColor);
$stringColor = imagecolorallocate($im, mt_rand(0, 200), mt_rand(0, 120), mt_rand(0, 120));
//干擾
for ($i = 0; $i < 10; $i++) {
$fontcolor = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
imagearc($im, mt_rand(-10, $width), mt_rand(-10, $height), mt_rand(30, 300), mt_rand(20, 200), 55, 44, $fontcolor);
}
for ($i = 0; $i < 25; $i++) {
$fontcolor = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
imagesetpixel($im, mt_rand(0, $width), mt_rand(0, $height), $pointColor);
}
for ($i = 0; $i < $length; $i++) {
imagestring($im, 5, $i * 10 + 5, mt_rand(1, 8), $randval{$i}, $stringColor);
}
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
exit;
}

}

//生成隨機字元串
function randomString($len=6, $type=1, $addChars='') {
switch ($type) {
case 0:
$chars = '' . $addChars;
break;
case 1:
$chars = str_repeat('0123456789', 3);
break;
case 2:
$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' . $addChars;
break;
case 3:
$chars = 'abcdefghijklmnopqrstuvwxyz' . $addChars;
break;
default :
//默認去掉了容易混淆的字元oOLl和數字01,要增加請使用addChars參數
$chars = '' . $addChars;
break;
}
if ($len > 10) {
//位數過長重復字元串一定次數
$chars = $type == 1 ? str_repeat($chars, $len) : str_repeat($chars, 5);
}
$chars = str_shuffle($chars);
return substr($chars, 0, $len);
}

調用 wxhVerify::imageVerify();

閱讀全文

與phpimagestring亂碼相關的資料

熱點內容
安卓機如何在電腦備份圖片 瀏覽:923
ca證書加密機價格 瀏覽:798
天乾地支年份演算法 瀏覽:796
程序員打造的視頻 瀏覽:6
java和php通信 瀏覽:680
為什麼黑程序員 瀏覽:162
程序員男生 瀏覽:455
戴爾文件夾內文件怎麼置頂 瀏覽:582
雲伺服器6m網速 瀏覽:722
vivo手機中國聯通伺服器地址 瀏覽:862
工程總控編譯失敗 瀏覽:706
燕趙紅楓app如何下載 瀏覽:867
php查殺軟體 瀏覽:878
教育管理學pdf 瀏覽:547
伺服器均衡怎麼使用 瀏覽:626
linux中jps 瀏覽:954
單片機實驗感想 瀏覽:561
程序員級別數學演算法邏輯 瀏覽:900
2k21公園怎麼換伺服器 瀏覽:724
php釋放資料庫連接 瀏覽:722