imagesetpixel

(PHP 4, PHP 5, PHP 7)
Set a single pixel
bool imagesetpixel ( resource $image, int $x, int $y, int $color )

imagesetpixel() draws a pixel at the specified coordinate.

Parameters:
image

An image resource, returned by one of the image creation functions, such as imagecreatetruecolor().

x

x-coordinate.

y

y-coordinate.

color

A color identifier created with imagecolorallocate().

Returns:

Returns TRUE on success or FALSE on failure.

Examples:
imagesetpixel() example

A random drawing that ends with a regular picture.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?php
 
$x = 200;
$y = 200;
 
$gd = imagecreatetruecolor($x$y);
  
$corners[0] = array('x' => 100, 'y' =>  10);
$corners[1] = array('x' =>   0, 'y' => 190);
$corners[2] = array('x' => 200, 'y' => 190);
 
$red = imagecolorallocate($gd, 255, 0, 0); 
 
for ($i = 0; $i < 100000; $i++) {
  imagesetpixel($gdround($x),round($y), $red);
  $a = rand(0, 2);
  $x = ($x $corners[$a]['x']) / 2;
  $y = ($y $corners[$a]['y']) / 2;
}
  
header('Content-Type: image/png');
imagepng($gd);
 
?>

The above example will output something similar to:

Output of example : imagesetpixel()
See also:

imagecreatetruecolor() -

imagecolorallocate() -

imagecolorat() -

doc_php
2025-01-10 15:47:30
Comments
Leave a Comment

Please login to continue.