php不能生成圖片如何解決

這篇文章主要介紹“php不能生成圖片如何解決”,在日常操作中,相信很多人在php不能生成圖片如何解決問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”php不能生成圖片如何解決”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!

10年積累的做網站、成都網站設計經驗,可以快速應對客戶對網站的新想法和需求。提供各種問題對應的解決方案。讓選擇我們的客戶得到更好、更有力的網絡服務。我雖然不認識你,你也不認識我。但先網站制作后付款的網站建設流程,更有阜陽免費網站建設讓你可以放心的選擇與我們合作。

解決辦法

1、打開gd2庫,通過phpinfo進行查看。清除bom,代碼是頂行開始寫的,所以問題可能出現在代碼上。

2、在header前加上ob_clean()語句,隨后就可以運行了。

注意點

生成圖片時,header('Content-type: image/png');前面不能有輸出?;蛘?,前面加:ob_clean(); 即使用輸出也可以通過這句來清除輸出緩存。

解決實例

//設置 驗證碼高度寬度\上面字符個數
 
$img_w = 70;
 
$img_h = 22;
 
$font = 5;
 
$char_len = 5;
 
//數組合并, range()函數返回一個范圍數組
 
$char = array_merge ( range ( 'a', 'z' ), range ( 'A', 'Z' ), range ( '1', '9' ) );
 
$rand_keys = array_rand ( $char, $char_len ); //隨機從數組中取指定個數的元素,生成鍵值
 
if ($char_len == 1) { //若只有一個數,則array_rand()返回非數組類型
 
         $rand_keys = array ($rand_keys );
 
}
 
shuffle($rand_keys);  //可以不用
 
$code = '';
 
foreach ( $rand_keys as $k ) {
 
         $code .= $char [$k];
 
}
 
session_start ();
 
$_SESSION ['captcha'] = $code;
 
  
 
//添加線、色
 
//創(chuàng)建新圖像
 
$img = imagecreatetruecolor ( $img_w, $img_h );
 
//分配顏色
 
$bg_color = imagecolorallocate ( $img, 0xcc, 0xcc, 0xcc );
 
//畫布背景色
 
imagefill ( $img, 0, 0, $bg_color );
 
//干擾線
 
for($i = 0; $i < 300; ++$i) {
 
         $color = imagecolorallocate ( $img, mt_rand ( 0, 255 ), mt_rand ( 0, 255 ), mt_rand ( 0, 255 ) );
 
         imagesetpixel ( $img, mt_rand ( 0, $img_w ), mt_rand ( 0, $img_h ), $color );
 
}
 
for($i = 0; $i <= 10; ++ $i) {
 
         //設置直線顏色
 
         $color = imageColorAllocate ( $img, mt_rand ( 0, 255 ), mt_rand ( 0, 255 ), mt_rand ( 0, 255 ) );
 
         //在$img圖像上隨機畫一條直線
 
         imageline ( $img, mt_rand ( 0, $img_w ), mt_rand ( 0, $img_h ), mt_rand ( 0, $img_w ), mt_rand ( 0, $img_h ), $color );
 
         //imagesetpixel($img,mt_rand(0,$img_w),mt_rand(0,$img_h),$color);
 
}
 
  
 
//加加框
 
$rect_color = imagecolorallocate ( $img, 0x90, 0x90, 0x90 );
 
imagerectangle ( $img, 0, 0, $img_w - 1, $img_h - 1, $rect_color );
 
$str_color = imagecolorallocate ( $img, mt_rand ( 0, 100 ), mt_rand ( 0, 100 ), mt_rand ( 0, 100 ) );
 
$font_w = imagefontwidth ( $font );
 
$font_h = imagefontheight ( $font );
 
$str_len = $font_w * $char_len;
 
imagestring ( $img, $font, ($img_w - $str_len) / 2, ($img_h - $font_h) / 2, $code, $str_color );

到此,關于“php不能生成圖片如何解決”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續(xù)學習更多相關知識,請繼續(xù)關注創(chuàng)新互聯網站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>

本文標題:php不能生成圖片如何解決
轉載來于:http://muchs.cn/article10/gppjdo.html

成都網站建設公司_創(chuàng)新互聯,為您提供網站策劃、網站建設、自適應網站網站導航、網站制作全網營銷推廣

廣告

聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯

成都做網站