尝试使用base64_decode生成Image时出错

时间:2013-05-19 12:48:10

标签: php

我正在尝试使用带有png扩展名的base64_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));

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:1)

这是你的答案:

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

您没有删除data:image/png;base64,

相关问题