Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> PHP

PHP – ดึงความกว้าง Pixel ของอักขระใน Font ด้วย imagefontwidth()

ฟังก์ชัน imagefontwidth() ใช้ดึงความกว้าง (pixel width) ของอักขระใน font ที่ระบุ

รูปแบบการใช้งาน (Syntax)

int imagefontwidth(int $font)

พารามิเตอร์

  • $font – font identifier: 1-5 สำหรับ built-in fonts หรือ custom font จาก imageloadfont()

ค่าที่คืนกลับ

คืนความกว้างของอักขระใน pixel (integer)

ตัวอย่าง 1: Font size 3

<?php
echo 'Font width: ' . imagefontwidth(3);
?>

ผลลัพธ์

Font width: 7

ตัวอย่าง 2: Font size 1-5 ทั้งหมด

<?php
echo 'Font width for font value 1 is ' . imagefontwidth(1) . '
'; echo 'Font width for font value 2 is ' . imagefontwidth(2) . '
'; echo 'Font width for font value 3 is ' . imagefontwidth(3) . '
'; echo 'Font width for font value 4 is ' . imagefontwidth(4) . '
'; echo 'Font width for font value 5 is ' . imagefontwidth(5) . '
'; ?>

ผลลัพธ์

Font width for font value 1 is 5
Font width for font value 2 is 6
Font width for font value 3 is 7
Font width for font value 4 is 8
Font width for font value 5 is 9