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

วิธีสร้างภาพสีจริงใหม่ใน PHP โดยใช้ imagecreatetruecolor ()


imagecreatetruecolor( ) เป็นฟังก์ชัน inbuilt ใน PHP ที่ใช้สร้างภาพสีจริงใหม่ ส่งคืนรูปภาพเปล่าตามขนาดที่กำหนด

ไวยากรณ์

resource imagecreatetruecolor($width, $height)

พารามิเตอร์

imagecreatetruecolor() ใช้สองพารามิเตอร์ $width และ $height .

  • $ความกว้าง − พารามิเตอร์ $width ใช้เพื่อกำหนดความกว้างของรูปภาพ

  • $ความสูง − พารามิเตอร์ $height ใช้เพื่อกำหนดความสูงของรูปภาพ

คืนค่า

imagecreatetruecolor() ส่งคืนตัวระบุทรัพยากรรูปภาพเมื่อสำเร็จหรือคืนค่าเท็จสำหรับข้อผิดพลาด

ตัวอย่างที่ 1

<?php
   // Set the vertices of polygon
   $values = array(
      150, 50, // Point 1 (x, y)
      50, 250, // Point 2 (x, y)
      250, 250 // Point 3 (x, y)
   );
   // Create the size of image or blank image
   $image = imagecreatetruecolor(700, 350);

   // Set the background color of image
   $background_color = imagecolorallocate($image, 122, 122, 122);

   // Fill background with above selected color
   imagefill($image, 0, 0, $background_color);

   // Allocate a color for the polygon
   $image_color = imagecolorallocate($image, 0, 255, 255);

   // Draw the polygon
   imagepolygon($image, $values, 3, $image_color);

   // Output the picture to the browser
   header('Content-type: image/png');

   imagepng($image);
?>

ผลลัพธ์

วิธีสร้างภาพสีจริงใหม่ใน PHP โดยใช้ imagecreatetruecolor ()

ตัวอย่างที่ 2 - ด้านล่างโค้ด PHP จะสร้างสตรีมรูปภาพ GD ใหม่

<?php
   header ('Content-Type: image/gif');
   $img = @imagecreatetruecolor(550, 220)
   or die('Cannot Initialize new GD image stream');
   $text_color = imagecolorallocate($img, 255, 255, 0);
   
   imagestring($img, 10, 50, 50, 'A Simple PHP Example', $text_color);
   imagepng($img);
   imagedestroy($img);
?>

ผลลัพธ์

วิธีสร้างภาพสีจริงใหม่ใน PHP โดยใช้ imagecreatetruecolor ()