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

PHP – วาดข้อความแนวนอนด้วย imagestring()

ฟังก์ชัน imagestring() ใช้วาดข้อความ (string) แนวนอน (horizontal) บนรูปภาพ

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

bool imagestring(GdImage $image, int $font, int $x, int $y, string $string, int $color)

พารามิเตอร์

  • $image – GdImage จาก imagecreatetruecolor() หรือ imagecreate()
  • $font – font size 1-5 สำหรับ built-in fonts
  • $x – ตำแหน่งแนวนอน (X-axis) มุมซ้ายบน
  • $y – ตำแหน่งแนวตั้ง (Y-axis) มุมบนสุด
  • $string – ข้อความที่ต้องการเขียน
  • $color – สีของข้อความ

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

คืน true เมื่อสำเร็จ false เมื่อล้มเหลว

ตัวอย่าง 1: หลายบรรทัด font size ต่างกัน

<?php
$img = imagecreate(700, 300);
$background_color = imagecolorallocate($img, 0, 0, 255);
$text_color = imagecolorallocate($img, 255, 255, 255);
imagestring($img, 50, 180, 150, "Tutorialspoint", $text_color);
imagestring($img, 30, 160, 120, "Simply Easy Learning", $text_color);
header("Content-Type: image/png");
imagepng($img);
imagedestroy($img);
?>

ผลลัพธ์

PHP – วาดข้อความแนวนอนด้วย imagestring()

ตัวอย่าง 2: ข้อความบรรทัดเดียว

<?php
$img = imagecreate(700, 300);
$background_color = imagecolorallocate($img, 122, 122, 122);
$text_color = imagecolorallocate($img, 255, 255, 0);
imagestring($img, 10, 30, 60, "Tutorialspoint:Simply Easy Learning", $text_color);
header("Content-Type: image/png");
imagepng($img);
imagedestroy($img);
?>

ผลลัพธ์

PHP – วาดข้อความแนวนอนด้วย imagestring()