0

I want convert base64 jpeg or jpg (white background) to transparent png with my own code :

$img = str_replace(array('data:image/jpeg;base64', 'data:image/jpg;base64', 'data:image/bmp;base64', 'data:image/png;base64'), '', $img);
$img = str_replace(' ', '+', $img);
$data = base64_decode($img);
$file = UPLOAD_DIR . $fn . str_replace('image/', '.', $type); 
$success = file_put_contents($file, $data); //when its done it saves the image data

I'm following this post but image cannot save to dir, modified code:

    $img = str_replace(array('data:image/jpeg;base64', 'data:image/jpg;base64', 'data:image/bmp;base64', 'data:image/png;base64'), '', $img);
$img = str_replace(' ', '+', $img);
$data = base64_decode($img);
$file = UPLOAD_DIR . $fn . str_replace('image/', '.', $type); 
$success = file_put_contents($file, $data); //when its done it saves the image data

imagesavealpha($img, true);
$color = imagecolorallocatealpha($img, 0, 0, 0, 127);
imagefill($img, 0, 0, $color);
imagepng($img, 'test.png');

Also, following post but the image show error when opened, modified code:

    $img = str_replace(array('data:image/jpeg;base64', 'data:image/jpg;base64', 'data:image/bmp;base64', 'data:image/png;base64'), '', $img);
$img = str_replace(' ', '+', $img);
$data = base64_decode($img);
$file = UPLOAD_DIR . $fn . str_replace('image/', '.', $type); 
$success = file_put_contents($file, $data); //when its done it saves the image data

$image = imagealphablending($file, true);
$transparentcolour = imagecolorallocate($image, 255,255,255);
imagecolortransparent($image, $transparentcolour)

0 Answers0