การวาดตัวอักษรภาษาไทยลงรูปภาพ (UTF-8)

ค้นหาอยู่นานสำหรับการวาดภาษาไทยลงบนรูปภาพ ซึ่งปกติมักมีปัญหาการใช้งานภาษาไทย โค้ดนี้สามารถนำไปดัดแปลงใช้กับ captcha ที่เป็นภาษาไทยได้เลยครับ

โดยปกติ เราสามารถใช้ฟังก์ชั่น imagettftext() ในการวาดตัวอักษรลงบนรูปภาพได้
imagettftext ( resource $image , float $size , float $angle , int $x , int $y , int $color , string $fontfile , string $text )

$image คือ resource ซี่งอาจได้จากการสร้างโดย imagecreatetruecolor()
$size คือ ขนาดของฟ้อนต์ที่ต้องการวาด (GD1 มีหน่วยเป็น pixel, GD2 มีหน่วยเป็น point)
$angle คือ องศาสำหรับการวาด 0 คือวาดปกติ, 90 วาดตั้งฉาก, 180 วาดกลับหัว (ขวาไปซ้าย) เป็นต้น
$x และ $y คือตำแหน่งเริ่มต้นวาดตามแนวแกน x และ y ของรูปภาพ
$color คือ resource ของสีของตัวอักษรที่ต้องการ อาจได้มาจากการสร้างโดย imagecolorallocate()
$fontfile คือ ไฟล์ฟ้อนต์ที่ต้องการวาด ถ้าอยู่คนละ path กับโปรแกรม ต้องระบุ path ให้ครบถ้วนด้วย เช่น C:\Windows\tahoma.ttf
$text คือ ข้อความที่ต้องการวาด ในรูป UTF-8 และตัวอักษรในรูป ก (รองรับภาษาอื่นๆ รวมทั้งภาษาไทย)

สิ่งที่เราต้องการจริงๆก็คือ ไฟล์ Font True Type สำหรับภาษาไทย และการแปลงตัวอักษรจาก ก ให้เป้น ก เท่านั้น
<?php
function utf8tohtml($utf8, $encodeTags) {
    $result = '';
    for ($i = 0; $i < strlen($utf8); $i++) {
        $char = $utf8[$i];
        $ascii = ord($char);
        if ($ascii < 128) {
            // one-byte character
            $result .= ($encodeTags) ? htmlentities($char) : $char;
        } else if ($ascii < 192) {
            // non-utf8 character or not a start byte
        } else if ($ascii < 224) {
            // two-byte character
            $result .= htmlentities(substr($utf8, $i, 2), ENT_QUOTES, 'UTF-8');
            $i++;
        } else if ($ascii < 240) {
            // three-byte character
            $ascii1 = ord($utf8[$i+1]);
            $ascii2 = ord($utf8[$i+2]);
            $unicode = (15 & $ascii) * 4096 +
                       (63 & $ascii1) * 64 +
                       (63 & $ascii2);
            $result .= "&#$unicode;";
            $i += 2;
        } else if ($ascii < 248) {
            // four-byte character
            $ascii1 = ord($utf8[$i+1]);
            $ascii2 = ord($utf8[$i+2]);
            $ascii3 = ord($utf8[$i+3]);
            $unicode = (15 & $ascii) * 262144 +
                       (63 & $ascii1) * 4096 +
                       (63 & $ascii2) * 64 +
                       (63 & $ascii3);
            $result .= "&#$unicode;";
            $i += 3;
        }
    }
    return $result;
}

// Set the content-type
header('Content-type: image/png');

// Create the image
$im = imagecreatetruecolor(400, 30);

// 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, 399, 29, $white);

// The text to draw
$text = utf8tohtml('ใช้ภาษาไทยดู', true);

// Replace path by your own font path
$font = 'ANGSAB.TTF';

// Add some shadow to the text
imagettftext($im, 20, 0, 11, 26, $grey, $font, $text);

// Add the text
imagettftext($im, 20, 0, 10, 25, $black, $font, $text);

// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im);
imagedestroy($im);
?>

utf8tohtml() สำหรับการแปลงตัวอักษร
และ ฟ้อนต์ True Type ภาษาไทย ANGSAB.TTF
ตัวอย่าง จากการทดสอบ แสดงผล สระได้ถูกต้องบน Windows นะครับ และในตัวอย่างนี้ยังสามารถแสดงตัวอักษรในลักษณะที่มีเงาได้อีกด้วย
ผู้เขียน goragod โพสต์เมื่อ 23 ส.ค. 2553 เปิดดู 18,419 ป้ายกำกับ PHP
^