0

I have been reading alot on how to remove the background colour from a .png image. I found some code on StackOverflow but I can't seem to get it to work. When the code is run the result is a blank screen. The aim is to remove the all the white background from the image.

Does anyone know how what is happening or is there a better way to make the image have a transparent background.

$image = imagecreatetruecolor("logo.png");

// Transparent Background
imagealphablending($image, false);
$transparency = imagecolorallocatealpha($image, 0, 0, 0, 127);
imagefill($image, 0, 0, $transparency);
imagesavealpha($image, true);

// Drawing over
//$black = imagecolorallocate($image, 0, 0, 0);
//imagefilledrectangle($image, 25, 25, 75, 75, $black);

header('Content-Type: image/png');
imagepng($image);
DCJones
  • 3,121
  • 5
  • 31
  • 53

1 Answers1

0

imagecreatetruecolor accepts two parameters (width and height) Can you try this code

$image = imagecreatefromstring($your_img); //or another loading function you need
$white = imagecolorallocate($image, 255, 255, 255);
imagecolortransparent($image, $white);
imagepng($image, $output_file_name);
Theepag
  • 301
  • 2
  • 16
  • If I run your code I get an error:" imagecreatefromstring(): Data is not in a recognized format" If I echo $image i see "logo.png". Any ideas why "imagecreatefromstring()" does not recognize the image file. – DCJones Feb 07 '21 at 16:38
  • Can you add these code before that ? https://stackoverflow.com/a/42228538/7904389 – Theepag Feb 08 '21 at 02:19