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

ฟังก์ชัน imagecreate() ใน PHP

คำอธิบาย

ฟังก์ชัน imagecreate() ใช้สร้างภาพใหม่ แต่วิธีที่แนะนำคือใช้ imagecreatetruecolor() แทน เนื่องจากการประมวลผลภาพจะเกิดขึ้นบนภาพคุณภาพสูงสุด ซึ่งสามารถสร้างได้โดยใช้ imagecreatetruecolor()

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

imagecreate($width, $height)

พารามิเตอร์

  • width: ความกว้างของภาพ

  • height: ความสูงของภาพ

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

ฟังก์ชัน imagecreate() คืนค่าตัวระบุทรัพยากรภาพ (image resource identifier) เมื่อสำเร็จ หรือ FALSE เมื่อเกิดข้อผิดพลาด

ตัวอย่าง

<?php
   $img = imagecreate(500, 300);
   $bgcolor = imagecolorallocate($img, 150, 200, 180);
   $fontcolor = imagecolorallocate($img, 120, 60, 200);
   imagestring($img, 12, 150, 120, "Demo Text1", $fontcolor);
   imagestring($img, 3, 150, 100, "Demo Text2", $fontcolor);
   imagestring($img, 9, 150, 80, "Demo Text3", $fontcolor);
   imagestring($img, 12, 150, 60, "Demo Text4", $fontcolor);
   header("Content-Type: image/png");
   imagepng($img);
   imagedestroy($img);
?>

ผลลัพธ์

ภาพตัวอย่างจะแสดงข้อความหลายบรรทัดบนพื้นหลังสี