2017-05-31 39 views
0

我正在写一个android应用程序的web服务。在手机上,我发送了需要解码并保存为jpg/jpeg/png(任意一种)格式的图像。如何解码并保存作为base64字符串数组发送到服务器目录的多个图像?

将发送的数据将是一个base64数组,我需要在后端处理它,并将其保存到服务器目录中供将来参考。

要保存图片我的代码看起来像这样。 (这只是一个测试方法)

public function testing(Request $request){ 
    $i=0; 
    foreach($request['images'] as $name) 
    { 
     $i +=1; 

     $imagename= 'Image'.$i.'.jpeg'; 
     $destinationPath = public_path('images'); 
     $path = 'images/'.$imagename; 
     $res = file_put_contents($path, base64_decode($name)); 
    } 
} 

然而,此代码将创建JPEG扩展的图像时,我尝试打开它只是给我这个: enter image description here

我想我失去了一些东西。

[上面的代码是刚刚从上传应用正确的文件的演示。]

回答

0

对不起,打扰。我通过添加一些代码来修复它。

$i=0; 
    foreach($request['images'] as $name) 
    { 

     list($type, $name) = explode(';', $name); 
     list(, $name)  = explode(',', $name); 
     $data = base64_decode($name); 
     $i +=1; 

     $imagename= 'Image'.$i.'.jpeg'; 
     $destinationPath = public_path('images'); 
     $path = 'images/'.$imagename; 
     $res = file_put_contents($path, $data); 

     /*$saveInlistingimages = DB::insert('INSERT INTO listingimages (Listing, Image) values (?, ?)', [$lastId, $path]);*/ 

    } 
相关问题