2948 explained code solutions for 75 technologies


php-gdHow to set image background color


<?php

$im = imagecreatetruecolor(400, 300);

$c_green = imageColorAllocate($im, 46,204,64);
imagefill($im, 0, 0, $c_green);

$c_black = imageColorAllocate($im, 0,0,0);
imagefilledellipse($im, 200, 150, 100, 100, $c_black);

imagePng($im, '/tmp/image.png');ctrl + c
imagecreatetruecolor

creates true color GD image object with specified width & height

imageColorAllocate

creates color object to later use in image

imagefill(

fills an entire image with a given color (green in our case)

imagePng

saves image in PNG format to the given path


How to set image background color, php gd

Usage example

<?php

$im = imagecreatetruecolor(400, 300);

$c_green = imageColorAllocate($im, 46,204,64);
imagefill($im, 0, 0, $c_green);

$c_black = imageColorAllocate($im, 0,0,0);
imagefilledellipse($im, 200, 150, 100, 100, $c_black);

imagePng($im, '/tmp/image.png');