1

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);

4 Answers 4

1

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.

1

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);
1

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");
1
  • Transparent PNG images were appearing with black transparent bits, but this worked a charm for adding a white background to transparent images.
    – Luke
    Apr 23, 2014 at 9:29
1

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);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.