Skip to content

Zeroplex 生活隨筆

軟體開發、伺服器和生活瑣事

小 縮小字型大小。 中 重設字型大小。 大 放大字型大小。

PHP Captcha 大惡搞

Posted on 2008 年 4 月 19 日2021 年 3 月 12 日 By 日落 在〈PHP Captcha 大惡搞〉中有 4 則留言

自從上次改過別人的 PHP Captcha 以後,這次玩得更瘋了。

今天跟 scwu 聊過以後想出一堆鬼主意來當作 captcha 上面的文字,可以考慮的有注音文、火星文和一堆有的沒的符號。後來想整人所以決定讓大家動動頭腦,出點數學題目讓大家做,如加減乘除:

不過加減乘除對對電腦來說太簡單了,以後要試試看來點方程式求解,有必要來個微積分也行,不過這樣就得大費周章的請 LeTex 出來幫忙了。

上面的 captcha 我改名叫做「mathcha」,程式碼如下:

<?php
    $font_face = 'arialbd.ttf'; //font you want to use
    $width='80';
    $height='25';
    $font_size = $height * 0.65;

    $image = imagecreate($width, $height) or die('Cannot initialize new GD image stream');
    $background_color = imagecolorallocate($image, 0, 0, 0);
    $text_color = imagecolorallocate($image, 255, 255, 255);
    $noise_color = imagecolorallocate($image, 150, 255, 150);

    //create dots
    for( $i=0; $i<($width*$height)/3; $i++ ) {
        imagefilledellipse($image, mt_rand(0,$width), mt_rand(0,$height), 1, 1, $noise_color);
    }

    //print text
    $n1 = mt_rand(1,20);
    $n2 = mt_rand(1,20);
    $string = "$n1 + $n2";
    $textbox = imagettfbbox($font_size, 0, $font_face, $string) or die('Error in imagettfbbox function');
    $x = ($width - $textbox[4])/2;
    $y = ($height - $textbox[5])/2;
    imagettftext($image, $font_size, 0, $x, $y, $text_color, $font_face , $string) or die('Error in imagettftext function');

    /* output captcha image to browser */
    header('Content-Type: image/jpeg');
    imagejpeg($image);
    imagedestroy($image);

    $_SESSION['mathcha'] = $n1 + $n2;
?>

記得你把你用的字型和這個程式放在同一個目錄。

Tags:PHP, 程式設計

文章導覽

Previous Post: Mediawiki 參數設定
Next Post: 找對老師真的很重要

Comments (4) on “PHP Captcha 大惡搞”

  1. Unknown表示:
    2008 年 4 月 19 日20:07

    http://legnaleurc.blogspot.com/2008/04/blog-post_20.html

    回覆
  2. Unknown表示:
    2008 年 4 月 25 日10:09

    帥阿~日落

    弄個偏微分就算到臭頭了

    回覆
  3. 匿名表示:
    2008 年 5 月 7 日06:07

    哈哈哈哈!這個有意思!XD

    回覆
  4. essoduke表示:
    2009 年 7 月 20 日13:36

    您的拋磚引玉,讓小弟做了成語版來共襄盛舉 XD…

    http://essoduke.org/2009/07/20/1304

    回覆

發佈回覆給「匿名」的留言 取消回覆

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *


其他

關於我  (About me)

小額贊助

  文章 RSS Feed

  留言 RSS Feed

Apache AWS Bash C/C++ Docker FreeBSD GCP Git Google Java JavaScript Laravel Linux Microsoft MSSQL MySQL Nginx PHP PHPUnit PostgreSQL Python Qt Ubuntu Unix Vim Web Windows WordPress XD 作業系統 分享 好站推薦 專題 攝影 新奇搞笑 新聞 旅遊 生活雜記 程式設計 網路架站 網頁設計 資訊學習 資訊安全 遊戲 音樂


創用 CC 授權條款
本著作係採用創用 CC 姓名標示-相同方式分享 4.0 國際 授權條款授權.