How to set a single pixel using imagesetpixel() function in PHP?

The imagesetpixel() function is an inbuilt PHP function used to set a single pixel at a specified coordinate in an image. It's part of the GD library extension and requires the image to be an image resource.

Installation: Ensure the GD extension is enabled in your PHP installation. Most PHP installations include it by default.

Syntax

bool imagesetpixel(resource $image, int $x, int $y, int $color)

Parameters

The imagesetpixel() function accepts four parameters:

  • $image − The image resource to work on, created by functions like imagecreate() or imagecreatefromjpeg().

  • $x − The x-coordinate of the pixel (horizontal position).

  • $y − The y-coordinate of the pixel (vertical position).

  • $color − The color identifier created by imagecolorallocate().

Return Value

Returns TRUE on success or FALSE on failure.

Example 1: Drawing a Horizontal Line

This example creates an image and draws a horizontal line using multiple pixels ?

<?php
    // Create a blank image
    $img = imagecreate(400, 200);
    
    // Allocate colors
    $white = imagecolorallocate($img, 255, 255, 255); // Background
    $blue = imagecolorallocate($img, 0, 0, 255);      // Blue line
    
    // Draw horizontal line by setting individual pixels
    for ($i = 50; $i < 350; $i++) {
        imagesetpixel($img, $i, 100, $blue);
    }
    
    // Output the image
    header('Content-type: image/png');
    imagepng($img);
    
    // Clean up memory
    imagedestroy($img);
?>

Example 2: Creating a Simple Pattern

This example demonstrates creating a diagonal pattern using imagesetpixel() ?

<?php
    // Create a 300x300 image
    $img = imagecreate(300, 300);
    
    // Allocate colors
    $white = imagecolorallocate($img, 255, 255, 255);
    $red = imagecolorallocate($img, 255, 0, 0);
    $green = imagecolorallocate($img, 0, 255, 0);
    
    // Create diagonal pattern
    for ($i = 0; $i < 300; $i++) {
        // Main diagonal in red
        imagesetpixel($img, $i, $i, $red);
        
        // Anti-diagonal in green
        imagesetpixel($img, $i, 299-$i, $green);
    }
    
    // Output image
    header('Content-type: image/png');
    imagepng($img);
    
    // Free memory
    imagedestroy($img);
?>

Key Points

  • Coordinates start from (0,0) at the top-left corner

  • Colors must be allocated using imagecolorallocate() before use

  • Always call imagedestroy() to free memory after image processing

  • The function is useful for pixel-level image manipulation and creating custom graphics

Conclusion

The imagesetpixel() function provides precise control over individual pixels in PHP images. While it's typically used for drawing lines, patterns, or custom graphics, remember to properly manage image resources and use appropriate headers when outputting images to browsers.

Updated on: 2026-03-15T09:51:36+05:30

545 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements