| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
<?php //1.qi啟用gd庫GD庫提供了一系列用來處理圖片的API,使用GD庫可以處理圖片,或者產生圖片。 // 在網站上GD庫通常用來產生縮圖或者用來對圖片加浮水印或者對網站資料產生報表。 session_start(); // 把GBK編碼的字串轉換成UTF-8字串,第一個參數之所以寫GBK,是因為本php檔案在主機中儲存的編碼是GBK編碼 // UTF-8編碼瀏覽器普遍支援,通用性強,這裡就轉換成UTF-8 $str = iconv("GBK", "utf-8", "芸芸眾生綠水青山名勝古迹敞開心胸便會雲蒸霞蔚快樂將永遠伴隨著你"); if(!is_string($str) || !mb_check_encoding($str,"utf-8")) { exit("不是字串或者不是utf-8"); } $zhongwenku_size; // 按UTF-8編碼方式擷取字串的長度 $zhongwenku_size = mb_strlen($str,"UTF-8"); // 把上述字元匯入數組中 $zhongwenku = array(); for( $i=0; $i<$zhongwenku_size; $i++) { $zhongwenku[$i] = mb_substr($str, $i,1,"UTF-8"); } $result = ""; // 圖片上要寫入的四個字元 for($i=0; $i<4; $i++) { switch (rand(0, 1)) { case 0: $result.=$zhongwenku[rand(0, $zhongwenku_size-1)]; break; case 1: $result.=dechex(rand(0,15)); break; } } $_SESSION["check"] = $result; // 建立一個真彩圖片 寬100,高30 $img = imagecreatetruecolor(100, 30); // 分配背景顏色 $bg = imagecolorallocate($img, 0, 0, 0); // 分配文字顏色 $te = imagecolorallocate($img, 255,255,255); // 在圖片上寫字串 //imagestring($img, rand(3,8), rand(1,70), rand(1,10), $result, $te); // 在圖片上根據載入字型可以寫出特殊字型 imagettftext($img, 13, rand(2, 9), 20 ,20, $te, "MSYH.TTF",$result); $_SESSION["check"] = $result; for($i=0; $i<3; $i++) { // $t = imagecolorallocate($img, rand(0, 255),rand(0, 255),rand(0, 255)); // 畫線 imageline($img, 0, rand(0, 20), rand(70,100), rand(0, 20), $te); } $t = imagecolorallocate($img, rand(0, 255),rand(0, 255),rand(0, 255)); // 為圖片添加噪點 for($i=0; $i<200; $i++) { imagesetpixel($img, rand(1, 100), rand(1, 30), $t); } // 發送http頭資訊 指定本次發送的是image中的jpeg header("Content-type: image/jpeg"); // 輸出jpeg圖片至瀏覽器 imagejpeg($img); ?> |