解決PHP產生校正碼時“映像因其本身有錯無法顯示”的錯誤

來源:互聯網
上載者:User

標籤:des   style   blog   http   color   使用   io   for   ar   

今天使用http://crazymud.iteye.com/blog/452293給出的代碼進行PHP產生校正碼功能的實現,發現firefox一直提示“映像.......因其本身有錯無法顯示”的問題,作者也提示了說“如果瀏覽器顯示“映像XXX因其本身有錯無法顯示”,可盡量去掉文中空格”,但把代碼中所有空格都去掉了還是不能顯示檢驗圖片。於是深度google,大部分的解決方案也是將“<?”這句代碼前的空格斷行符號一切都刪掉以防止有html輸出,但這根本解決不了我的問題,後來看到有人在Header("Content-type: image/PNG");這句代碼前使用ob_clean()清除輸出,結果一試之下果然成功。現將原http://crazymud.iteye.com/blog/452293作者的代碼修改後貼到下面,以備不時之需(使用方法參看原帖):

    <?php     session_start();     session_register("login_check_number");       //先成生背景,再把產生的驗證碼放上去     $img_height=70;//先定義圖片的長、寬     $img_width=25;     $authnum=‘‘;     //生產驗證碼字元     $ychar="0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";     $list=explode(",",$ychar);     for($i=0;$i<4;$i++){         $randnum=rand(0,35);         $authnum.=$list[$randnum];     }     //把驗證碼字元儲存到session     $_SESSION["login_check_number"] = $authnum;            $aimg = imagecreate($img_height,$img_width);    //產生圖片     imagecolorallocate($aimg, 255,255,255);            //圖片底色,ImageColorAllocate第1次定義顏色PHP就認為是底色了     $black = imagecolorallocate($aimg, 0,0,0);        //定義需要的黑色          for ($i=1; $i<=100; $i++) {         imagestring($aimg,1,mt_rand(1,$img_height),mt_rand(1,$img_width),"@",imagecolorallocate($aimg,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255)));     }          //為了區別於背景,這裡的顏色不超過200,上面的不小於200     for ($i=0;$i<strlen($authnum);$i++){         imagestring($aimg, mt_rand(3,5),$i*$img_height/4+mt_rand(2,7),mt_rand(1,$img_width/2-2), $authnum[$i],imagecolorallocate($aimg,mt_rand(0,100),mt_rand(0,150),mt_rand(0,200)));     }     imagerectangle($aimg,0,0,$img_height-1,$img_width-1,$black);//畫一個矩形     ob_clean();  //關鍵代碼,防止出現‘映像因其本身有錯無法顯示‘的問題。    Header("Content-type: image/PNG");     ImagePNG($aimg);//產生png格式     ImageDestroy($aimg);     ?> 

 

解決PHP產生校正碼時“映像因其本身有錯無法顯示”的錯誤

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.