Php 尝试使用base64_解码生成图像时出错

Php 尝试使用base64_解码生成图像时出错,php,Php,我正在尝试使用带有png扩展名的base64\u decode生成图像,但生成的png图像是错误的,它显示“无效图像” 我的代码在下面 $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); 在

我正在尝试使用带有png扩展名的
base64\u decode
生成图像,但生成的png图像是错误的,它显示“无效图像”

我的代码在下面

$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);
在搜索解决方案时,我还尝试在$result变量之前添加

    header("Content-type: image/png");
我也试过这句话

    $decoded_base_64 = base64_decode(base64_encode($base_64));
有人能帮我吗?

这是你的答案:

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

可能重复:此行$decoded_base_64=base64_decode(base64_encode($base_64));没有意义如果你
cat
一个图像返回base64或者错误或者其他什么吗?@JordanDoyle是的does@rafi_ccj它的回报是什么?编辑:让我们倒带一点,什么是
var_转储($this->input->post('base_64')返回?如果它太大,就把它粘起来。