利用PHP 簡單實現加減法驗證碼


<?php
 
header('Content-Type: image/png');
$im = imagecreatetruecolor( 200 , 50 );
//生成圖片長寬 // Create some colors $white = imagecolorallocate($im, 255, 255, 255); $grey = imagecolorallocate($im, 128, 128, 128); $black = imagecolorallocate($im, 0, 0, 0);//背景色 imagefilledrectangle($im, 0, 0, 200, 49, $white); // The text to draw $rand = rand( 1 ,2 ) ; $one = rand(1,9); $two = rand(1,9); if( $rand == 1 ){ $result = $one + $two; $text = "$one+$two=?"; }else{ if( $one < $two ){ list( $two , $one ) = [ $one , $two ]; } $result = $one - $two; $text = "$one-$two=?"; } session_start(); $_SESSION['code'] = $result; // Replace font $font = './Arvo-Regular.ttf'; //imageline( $im , 0 , rand( 1,49 ) , 199 , 30 , $grey ); //干擾線
// Add the text $i = 0; $len = strlen( $text ); while( $i < $len ){ if( is_numeric( $text[$i] ) ){ imagettftext($im, 20, rand(-45,45), 20 * ($i+1) , 30, $black, $font, $text[$i]); }else{ imagettftext($im, 20, 0 , 20 * ($i+1) , 30, $black, $font, $text[$i]); } //echo $text[$i]; $i ++; } // Using imagepng() results in clearer text compared with imagejpeg() imagepng($im); imagedestroy($im);

  如果生成圖片為空,請檢測字體引入路徑是否有問題。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM