php無法產生圖片的解決方法:1、開啟gd2庫;2、在生成圖片前將所有輸出刪除掉;3、透過「ob_clean();」來清除輸出快取。
本文操作環境:Windows7系統、PHP7.1版,DELL G3電腦
php無法產生圖片怎麼辦? PHP驗證碼不能產生圖片,原因解決
產生圖片時,header('Content-type: image/png');前面不能有輸出! ! !
或者,前面加:ob_clean(); 即使用輸出也可以透過這句話來清除輸出快取!很重要! ! !
當然,首先要開啟gd2函式庫,可透過phpinfo查看。清除了bom,程式碼也是頂行開始寫的,因此問題可能出現在程式碼上。後來經過研究,發現還是要更改程序,要在header前面加上ob_clean()這個語句,這樣就可以運行了!哈哈,編碼調試程序如當醫生,百練成剛。
<?php //设置 验证码高度宽度\上面字符个数 $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; //添加线、色 //创建新图像 $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 );
設定字串顏色
$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_w = $font_w * $char_len; //字符串宽 imageString($img, $font, ($img_w-$str_w)/2, ($img_h-$font_h)/2, $code, $str_color); echo 'ddd'; //输出影响生成图片,查找了大半天的原因终于找到了 ob_clean(); //也可以加上这句,这样前面有输出,清除输出缓存 //生成图片 header ( 'Content-Type: image/png' );//header前不能加任何输出或加ob_clean()清除 imagepng($img); //----4 销毁画布 imagedestroy($img);
推薦學習:《PHP影片教學》
以上是php無法產生圖片怎麼辦的詳細內容。更多資訊請關注PHP中文網其他相關文章!

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

SublimeText3 Linux新版
SublimeText3 Linux最新版

記事本++7.3.1
好用且免費的程式碼編輯器

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

禪工作室 13.0.1
強大的PHP整合開發環境