imagesetpixel () là một hàm có sẵn trong PHP được sử dụng để đặt một pixel duy nhất tại tọa độ được liệt kê.
Cú pháp
bool imagesetpixel(resource $image, int $x, int $y, int $color)
Tham số
imagesetpixel () chấp nhận bốn tham số: $ image , $ x , $ y và $ color .
-
$ hình ảnh - Chỉ định tài nguyên hình ảnh để làm việc.
-
$ x - Chỉ định tọa độ x của pixel.
-
$ y - Chỉ định tọa độ y của pixel.
-
$ màu - Chỉ định màu của pixel.
Giá trị trả lại -
imagesetpixel () trả về Đúng khi thành công và Sai khi thất bại.
Ví dụ 1
<?php
// Load the png image using imagecreatefromjpeg() function
$img = imagecreatefromjpeg('C:\xampp\htdocs\test\29.jpg');
// Draw the line using imagesetpixel() function
$blue = imagecolorallocate($img, 255, 255, 0);
for ($i = 0; $i < 1000; $i++) {
imagesetpixel($img, $i, 100, $blue);
}
// Show the output image to the browser
header('Content-type: image/png');
imagepng($img);
?> Đầu ra
Ví dụ 2
<?php
$x = 700;
$y = 300;
$gd = imagecreatetruecolor($x, $y);
$corners[0] = array('x' => 100, 'y' => 10);
$corners[1] = array('x' => 0, 'y' => 170);
$corners[2] = array('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);
?> Đầu ra