ฟังก์ชัน imagesetpixel() ใช้ตั้งค่าสีของ pixel เดียวที่พิกัดที่ระบุ
รูปแบบการใช้งาน (Syntax)
bool imagesetpixel(GdImage $image, int $x, int $y, int $color)
พารามิเตอร์
- $image – GdImage resource
- $x – x-coordinate ของ pixel
- $y – y-coordinate ของ pixel
- $color – สีของ pixel (จาก imagecolorallocate())
ค่าที่คืนกลับ
คืน true เมื่อสำเร็จ false เมื่อล้มเหลว
ตัวอย่าง 1: วาดเส้นแนวนอนด้วย pixel
<?php
$img = imagecreatefromjpeg('C:\xampp\htdocs\test\29.jpg');
$blue = imagecolorallocate($img, 255, 255, 0);
for ($i = 0; $i < 1000; $i++) {
imagesetpixel($img, $i, 100, $blue);
}
header('Content-type: image/png');
imagepng($img);
?>
ผลลัพธ์

ตัวอย่าง 2: Sierpinski Triangle Pattern
<?php
$x = 700;
$y = 300;
$gd = imagecreatetruecolor($x, $y);
$corners[0] = ['x' => 100, 'y' => 10];
$corners[1] = ['x' => 0, 'y' => 170];
$corners[2] = ['x' => 190, 'y' => 170];
$blue = imagecolorallocate($gd, 255, 0, 0);
for ($i = 0; $i < 100000; $i++) {
imagesetpixel($gd, round($x), round($y), $blue);
$a = rand(0, 2);
$x = ($x + $corners[$a]['x']) / 2;
$y = ($y + $corners[$a]['y']) / 2;
}
header('Content-Type: image/png');
imagepng($gd);
?>
ผลลัพธ์
