Patrick Reck
Patrick Reck

Reputation: 11374

WideImage - Transparent areas of converted PNG should be white

Whenever a user uploads a photo using my script, WideImage converts it to JPEG. However, I just noticed that if I upload a PNG picture, with a transparent background, it makes it black instead.

Is there any way to make this white instead?

This is how i save the image:

$img->resizeDown('500', null)->saveToFile('annonce_billeder/'.$bnavn.'.jpeg', 70);

Upvotes: 1

Views: 3115

Answers (4)

Billy G
Billy G

Reputation: 698

With some changes (corrections) on Ricardo Gamba's solution code, it does the job...

// Load the original image
$original = WideImage::load("image.png");
$resized  = $original->resizeDown('500', null); // Do whatever resize or crop you need to do
$original->destroy(); // free some memory (original image not needed any more)

// Create an empty canvas with the resized image sizes
$img = WideImage::createTrueColorImage($resized->getWidth(), $resized->getHeight());
$bg  = $img->allocateColor(255,255,255);
$img->fill(0,0,$bg);

// Finally merge and do whatever you need...
$img->merge($resized)->saveToFile("image.jpg", 70);

Upvotes: 1

Ricardo Gamba
Ricardo Gamba

Reputation: 164

This is how to do it:

// Load the original image
$original = WideImage::load("image.png");
$original->resizeDown(1000); // Do whatever resize or crop you need to do

// Create an empty canvas with the original image sizes
$img = WideImage::createTrueColorImage($resized->getWidth(),$resized->getHeight());
$bg = $img->allocateColor(255,255,255);
$img->fill(0,0,$bg);

// Finally merge and do whatever you need...
$img->merge($original)->saveToFile("image.jpg");

Upvotes: 1

drzaus
drzaus

Reputation: 25004

Might be similar, but I was able to create an empty truecolor image and fill it with its own transparent color before doing any drawing:

$img = WideImage_TrueColorImage::create(100, 100);
$img->fill(0,0,$img->getTransparentColor());
// then text, watermark, etc
$img->save('...');

I assume you'll do something more like:

$img = WideImage::load(<source>);
if( <$img is png> ) {
   $img->fill(0,0, $img->getTransparentColor());
}
$img->resizeDown(500, null)->saveToFile('target.jpg', 70);

Upvotes: 1

arkascha
arkascha

Reputation: 42925

Not really directly. You wnt to read about how transparency is stored in pictures: it is an ordinary color value (any color) that has been marked especially as transparent.

So most likely the color specified in the example pictures you try actually is coded as black and the transparency gets lost whilst converting.

You might have a try to find out if you can detect if there is a color marked as transparent in the incoming picture and then manually change that color to non-transparcy and white before converting the picture.

Upvotes: 1

Related Questions