您好,登錄后才能下訂單哦!
這篇文章主要介紹php不能生成圖片怎么辦,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
1、解決辦法
打開gd2庫(kù),通過phpinfo進(jìn)行查看。清除bom,代碼是頂行開始寫的,所以問題可能出現(xiàn)在代碼上。
在header前加上ob_clean()語句,隨后就可以運(yùn)行了。
2、注意點(diǎn)
生成圖片時(shí),header('Content-type: image/png');前面不能有輸出?;蛘撸懊婕樱簅b_clean(); 即使用輸出也可以通過這句來清除輸出緩存。
3、解決實(shí)例
//設(shè)置 驗(yàn)證碼高度寬度\上面字符個(gè)數(shù) $img_w = 70; $img_h = 22; $font = 5; $char_len = 5; //數(shù)組合并, range()函數(shù)返回一個(gè)范圍數(shù)組 $char = array_merge ( range ( 'a', 'z' ), range ( 'A', 'Z' ), range ( '1', '9' ) ); $rand_keys = array_rand ( $char, $char_len ); //隨機(jī)從數(shù)組中取指定個(gè)數(shù)的元素,生成鍵值 if ($char_len == 1) { //若只有一個(gè)數(shù),則array_rand()返回非數(shù)組類型 $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) { //設(shè)置直線顏色 $color = imageColorAllocate ( $img, mt_rand ( 0, 255 ), mt_rand ( 0, 255 ), mt_rand ( 0, 255 ) ); //在$img圖像上隨機(jī)畫一條直線 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不能生成圖片怎么辦”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。