Laravel:将 Base64 .png 文件从控制器保存到公共文件夹

新手上路,请多包涵

我通过 Ajax 将 png 图像文件发送到 base64 中的控制器。我已经测试并确保控制器已收到 id 但仍无法将其保存到公用文件夹。

这是我的控制器

public function postTest() {
        $data = Input::all();

        //get the base-64 from data
        $base64_str = substr($data->base64_image, strpos($data->base64_image, ",")+1);

        //decode base64 string
        $image = base64_decode($base64_str);
        $png_url = "product-".time().".png";
        $path = public_path('img/designs/' . $png_url);

        Image::make($image->getRealPath())->save($path);
        // I've tried using
        // $result = file_put_contents($path, $image);
        // too but still not working

        $response = array(
            'status' => 'success',
        );
        return Response::json( $response  );
}

原文由 Expl0de 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 426
1 个回答

在 public 文件夹 image 中存储或保存 base64 图像并返回文件路径。

     $folderPath = public_path() . '/' . 'images/';
    $image_parts = explode(";base64,", $image);
    $image_type_aux = explode("image/", $image_parts[0]);
    $image_type = $image_type_aux[1];
    $image_base64 = base64_decode($image_parts[1]);
    $uniqid = uniqid();
    $file = $folderPath . $uniqid . '.' . $image_type;
    file_put_contents($file, $image_base64);
    return $file;

原文由 Ali Raza 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题