$fontsize = 30;
$text = "ทดสอบการใช้ภาษาไทยให้ถูกต้องสมบูรณ์ Hello World";
$font = "/path/to/fonts/thsarabunnew-webfont.ttf";
$bbox = imageftbbox($fontsize, 0, $font, $text);
$imageWidth = $bbox[2] - $bbox[0];
$imageHeight = $bbox[3] - $bbox[5];
$y = abs($bbox[5]);
$img = imagecreatetruecolor($imageWidth, $imageHeight);
imagealphablending($img, false);
imagesavealpha($img, true);
$transparent = imagecolorallocatealpha($img, 255, 255, 255, 127);
imagefill($img, 0, 0, $transparent);
$color = imagecolorallocate($img, 255, 0, 0);
imagettftext($img, $fontsize, 0, 0, $y, $color, $font, $text);
imagepng($img, 'temp.png');