尝试使用base64_decode生成Image时出错

I am trying to generate an image using base64_decode with png extension, but generated png image is wrong, it is showing "invalid image".

my code is below

$base_64 = $this->input->post('base_64');
$decoded_base_64 = base64_decode($base_64);
$result = file_put_contents('directory/toSaveImage/' . $id . '.png', $decoded_base_64);

While searching for the solution i also tried adding before the $result variable.

    header("Content-type: image/png");

I also did try this line though

    $decoded_base_64 = base64_decode(base64_encode($base_64));

Can anyone please help me?

Here's your answer:

<?php
    $base64 = $this->input->post('base_64');
    $base64 = str_replace('data:image/png;base64,', '', $base64);
    file_put_contents("directory/toSaveImage/{$id}.png", base64_decode($base64));

You weren't removing data:image/png;base64,