doukanwa6872 2014-12-11 06:43
浏览 65
已采纳

如何使用PHP从base64编码的数据/字符串创建和保存图像到网站文件夹

I have the following code on my php file which is working fine, Its decoding a base64 string and showing that as a image on the webpage, but i want it to save it on a folder too.

<?php
$base = $_POST['encoded'];
$base = base64_decode($base);

$im = imagecreatefromstring($base);
if ($im !== false) {
    header('Content-Type: image/png');
    imagepng($im);
    imagedestroy($im);
}
else {
    echo 'An error occurred.';
}
?>

i have already tried the solution described on these links but none of these are worked for me

How to save a PNG image server-side, from a base64 data string

How to create GD Image from base64 encoded jpeg?

what will be the extra line of code which could save this image to a folder

  • 写回答

3条回答 默认 最新

  • douxiexie3574 2014-12-11 07:00
    关注

    The file permissions was not set to writing mode. set it to 777 and tested with the following code.

    <?php
    $base = $_POST['encoded'];
    $base = base64_decode($base);
    
    $im = imagecreatefromstring($base);
    if ($im !== false) {
        header('Content-Type: image/png');
    
        imagepng($im,'image.png'); // saving image to the same directory
    
        imagedestroy($im);
    
    }
    else {
        echo 'An error occurred.';
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?