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

如何使用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条)

报告相同问题?

悬赏问题

  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于#flink#的问题:关于docker部署flink集成hadoop的yarn,请教个问题flink启动yarn-session.sh连不上hadoop
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题