Core PHP

Drawing Individual Pixels with GD

The GD library allows programmers to draw simple geometric primitives.

IndividualPixels.php

<?php
	// Create the image object
	$qImage = ImageCreateTrueColor(320, 240);

	// The color values are 0-255 for R, G, B.
	// The last value is alpha, which is zero for opaque and 127 for transparent
	// Specify a totally opaque orange color
	$qOrangePixel = ImageColorExactAlpha($qImage, 0xFF, 0x80, 0x00, 0);
	// Specify a semi-transparent yellow color
	$qSemiTransYellowPixel = ImageColorExactAlpha($qImage, 0xFF, 0xFF, 0x00, 64);

	// We will draw a few orange pixels, near (20, 20)
	ImageSetPixel($qImage, 20, 20, $qOrangePixel);
	ImageSetPixel($qImage, 20, 25, $qOrangePixel);
	ImageSetPixel($qImage, 25, 20, $qOrangePixel);

	// We will draw a few yellow pixels, near (100, 100)
	ImageSetPixel($qImage, 100, 100, $qSemiTransYellowPixel);
	ImageSetPixel($qImage, 110, 100, $qSemiTransYellowPixel);
	ImageSetPixel($qImage, 105, 100, $qSemiTransYellowPixel);

	Header('Content-Type: image/png');
    ImagePNG($qImage);
    ImageDestroy($qImage);
?>
 

Output

 
 

© 2007–2024 XoaX.net LLC. All rights reserved.