claviska / SimpleImage

A PHP class that makes working with images and GD as simple as possible.
MIT License
1.38k stars 382 forks source link

From PNG to JPEG = transparent to black #276

Closed Eugene-nsk closed 3 years ago

Eugene-nsk commented 3 years ago

When I insert this code ->fromFile('file.png')->toScreen('image/jpeg'); transparent parts of PNG image go to black.

How can I change fill color to white or another?

claviska commented 3 years ago

It's probably best to create a new image with the desired background and overlay the PNG onto it. Untested, but something like this should do it:

$image = new SimpleImage();
$image->fromFile('image.png');

$canvas = new SimpleImage();
$canvas
  ->fromNew($image->getWidth(), $image->getHeight(), '#ffffff')
  ->overlay($image)
  ->toScreen('image/jpeg');