Exp*_*0de 15 php laravel laravel-4
我通过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 );
}
Run Code Online (Sandbox Code Playgroud)
Moh*_*yab 15
干预图像使用file_get_content函数获取二进制数据:参考:http://image.intervention.io/api/make
您的控制器应如下所示:
public function postTest() {
$data = Input::all();
$png_url = "product-".time().".png";
$path = public_path().'img/designs/' . $png_url;
Image::make(file_get_contents($data->base64_image))->save($path);
$response = array(
'status' => 'success',
);
return Response::json( $response );
}
Run Code Online (Sandbox Code Playgroud)
小智 6
$data = Input::all();
$png_url = "perfil-".time().".jpg";
$path = public_path() . "/img/designs/" . $png_url;
$img = $data['fileo'];
$img = substr($img, strpos($img, ",")+1);
$data = base64_decode($img);
$success = file_put_contents($path, $data);
print $success ? $png_url : 'Unable to save the file.';
Run Code Online (Sandbox Code Playgroud)
这是一个容易犯的错误.
您正在错误地使用public_path.它应该是:
$path = public_path() . "/img/designs/" . $png_url;
Run Code Online (Sandbox Code Playgroud)
另外,我会避免你发送图像的方法.查看表单中的正确上载并使用Laravel的Input :: file方法.
小智 5
$file = base64_decode($request['image']);
$folderName = 'public/uploads/';
$safeName = str_random(10).'.'.'png';
$destinationPath = public_path() . $folderName;
$success = file_put_contents(public_path().'/uploads/'.$safeName, $file);
print $success;
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
53878 次 |
最近记录: |