tags:

views:

45

answers:

1

I have an image: image.png (could be jpg or gif)

How can I draw an 8x8 square at $x, $y and then save the image to $file?

+3  A: 

You are looking for imagerectangle():

// Load the image
$img = imagecreatefrompng("image.png"); // or imagecreatefromjpeg(), etc.

// Set a colour for the sides of the rectangle
$color = imagecolorallocate($img, 255, 255, 255); // for a white rectangle

// Draw an 8x8 recangle at coordinates ($x, $y) from top left
imagerectangle($img, $x, $y, $x+8, $y+8, $color);

// Or, use imagefilledrectangle() with the same arguments if you want it filled

// Save the image
imagepng($img, "image.png"); // or imagejpeg(), etc.
Ben James