PHP碎碼——ci的驗證碼

來源:互聯網
上載者:User

標籤:php


自己寫驗證碼的時候在文字定位上拿捏不準


於是把ci驗證碼主要部分扣了出來,看看,一般般,幹擾線是亮點


代碼去除了很多判斷,簡化了很多,顯示效果一樣

<?php$defaults = array(    ‘word‘        => ‘‘,    ‘img_path‘    => ‘./‘,    ‘img_url‘    => ‘‘,    ‘img_width‘    => ‘150‘,    ‘img_height‘    => ‘30‘,    ‘font_path‘    => ‘./instance/font/Elephant.ttf‘,    ‘expiration‘    => 7200,    ‘word_length‘    => 4,    ‘font_size‘    => 16,    ‘img_id‘    => ‘‘,    ‘pool‘        => ‘0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ‘,    ‘colors‘    => array(        ‘background‘    => array(255,255,255),        ‘border‘    => array(153,102,102),        ‘text‘        => array(204,153,153),        ‘grid‘        => array(255,182,182)    ));// 將變數從數組中取出foreach ($defaults as $key => $val){        $$key = $val;}// 產生隨機碼$word = ‘‘;$pool_length = strlen($pool);$rand_max = $pool_length - 1;for ($i = 0; $i < $word_length; $i++){    $word .= $pool[mt_rand(0, $rand_max)];}// 驗證碼長度$length    = strlen($word);//角度$angle    = ($length >= 6) ? mt_rand(-($length-6), ($length-6)) : 0;$x_axis    = mt_rand(6, (360/$length)-16);$y_axis = ($angle >= 0) ? mt_rand($img_height, $img_width) : mt_rand(6, $img_height);// 建立圖片控制代碼$im = imagecreatetruecolor($img_width, $img_height);// 建立顏色foreach ($colors as $key => $values){    $colors[$key] = imagecolorallocate($im, $colors[$key][0], $colors[$key][1], $colors[$key][2]);}// 畫了個和圖片等大的矩形,就是背景ImageFilledRectangle($im, 0, 0, $img_width, $img_height, $colors[‘background‘]);// -----------------幹擾線--------------------------$theta        = 1;$thetac        = 7;$radius        = 16;$circles    = 20;$points        = 32;for ($i = 0, $cp = ($circles * $points) - 1; $i < $cp; $i++){    $theta += $thetac;    $rad = $radius * ($i / $points);    $x = ($rad * cos($theta)) + $x_axis;    $y = ($rad * sin($theta)) + $y_axis;    $theta += $thetac;    $rad1 = $radius * (($i + 1) / $points);    $x1 = ($rad1 * cos($theta)) + $x_axis;    $y1 = ($rad1 * sin($theta)) + $y_axis;    imageline($im, $x, $y, $x1, $y1, $colors[‘grid‘]);    $theta -= $thetac;}// --------------------------------------------------------------// 無字型// ($font_size > 5) && $font_size = 5;// $x = mt_rand(0, $img_width / ($length / 3));// $y = 0;// for ($i = 0; $i < $length; $i++){//     $y = mt_rand(0 , $img_height / 2);//     imagestring($im, $font_size, $x, $y, $word[$i], $colors[‘text‘]);//     $x += ($font_size * 2);// }// 有字型// x 第一個字元隨機在圖片的前面一小段,後面依次加一個字元大小的寬度// y 圖片的1/2到距離底部3px之間隨機($font_size > 30) && $font_size = 30;$x = mt_rand(0, $img_width / ($length / 1.5));$y = $font_size + 2;for ($i = 0; $i < $length; $i++){    $y = mt_rand($img_height / 2, $img_height - 3);    imagettftext($im, $font_size, $angle, $x, $y, $colors[‘text‘], $font_path, $word[$i]);    $x += $font_size;}//畫矩形框imagerectangle($im, 0, 0, $img_width - 1, $img_height - 1, $colors[‘border‘]);// 刪除以前儲存的圖片$now = microtime(TRUE);$current_dir = opendir($img_path);while ($filename = readdir($current_dir)){    if (in_array(substr($filename, -4), array(‘.jpg‘, ‘.png‘))&& (str_replace(array(‘.jpg‘, ‘.png‘), ‘‘, $filename) + 0.1) < $now){        @unlink($img_path.$filename);    }}@closedir($current_dir);// 拼接檔案路徑並儲存$img_url = rtrim($img_url, ‘/‘).‘/‘;$img_filename = $now.‘.jpg‘;imagejpeg($im, $img_path.$img_filename);$img = ‘<img  src="‘.$img_url.$img_filename.‘" style="width: ‘.$img_width.‘; height: ‘.$img_height .‘; border: 0;" alt=" " />‘;// ImageDestroy($im);// return array(‘word‘ => $word, ‘time‘ => $now, ‘image‘ => $img, ‘filename‘ => $img_filename);// echo $img;//通知瀏覽器是png格式header(‘Content-type:image/jpg‘);//以png格式輸出imagejpeg($im);//銷毀記憶體中的圖片資源imagedestroy($im);


PHP碎碼——ci的驗證碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.