Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GD! Converting a png image to jpeg and making the alpha by default white and not black

Tags:

I tried something like this but it just makes the background of the image white, not necessarily the alpha of the image. I wanted to just upload everything as jpg's so if i could somehow "flatten" a png image with some transparently to default it to just be white so i can use it as a jpg instead. Appreciate any help. Thanks.

$old = imagecreatefrompng($upload); $background = imagecolorallocate($old,255,255,255); imagefill($old, 0, 0, $background); imagealphablending($old, false); imagesavealpha($old, true);
like image 680
Shawn Avatar asked Apr 03 '10 01:04

Shawn


People also ask

How do I make a PNG image normal?

Open the image you want to convert into PNG by clicking File > Open. Navigate to your image and then click “Open.” Once the file is open, click File > Save As. In the next window make sure you have PNG selected from the drop-down list of formats, and then click “Save.”

How do I convert a PNG image to JPEG?

Go to File > Save as and open the Save as type drop-down menu. You can then select JPEG and PNG, as well as TIFF, GIF, HEIC, and multiple bitmap formats. Save the file to your computer and it will convert.


1 Answers

<?php $input_file = "test.png"; $output_file = "test.jpg";  $input = imagecreatefrompng($input_file); $width = imagesx($input); $height = imagesy($input); $output = imagecreatetruecolor($width, $height); $white = imagecolorallocate($output,  255, 255, 255); imagefilledrectangle($output, 0, 0, $width, $height, $white); imagecopy($output, $input, 0, 0, 0, 0, $width, $height); imagejpeg($output, $output_file); 
like image 150
Alex Jasmin Avatar answered Sep 19 '22 17:09

Alex Jasmin