将base64数据转换为图像文件,重命名并将其移动到指定位置

时间:2016-03-09 05:58:28

标签: php laravel laravel-5

我正在尝试将base64数据存入图像文件,然后重命名并将其存储到指定位置,下面是我的代码

//the first image to be saved, first, we get the extension
$extension = $request->file('image')->getClientOriginalExtension();
//second we rename the file
$fileName = rand(11111,99999).'_'.$request->id;
//and then move the file to a specified location with the new name
$request->file('image')->move(base_path().'/public/images/uploads/', $fileName.'.'.$extension);
//second image, this one is on a base64 format so first we decode it
$image = base64_decode($request->thumbnail);
//and then store it to the same location of the first image with its new name and extension same to the first image
$image->move(base_path().'/public/images/uploads/', $fileName.'_thumbnail.'.$extension);

请从上面的代码中读取每个代码行的注释行,无论如何,它会引发错误

  

在非对象上调用成员函数move()   并指向这一行

$image->move(base_path().'/public/images/uploads/', $fileName.'_thumbnail.'.$extension);

任何帮助,线索,想法,建议和建议?

3 个答案:

答案 0 :(得分:0)

因为base64_decode只是返回一个你必须执行此操作的字符串。

file_put_contents(base_path().'/public/images/uploads/' . $fileName.'_thumbnail.'.$extension, $image)

它只是将图像保存在base_path().'/public/images/uploads/', $fileName.'_thumbnail.'.$extension位置。

当您尝试在非对象上调用方法时,总是会发生错误call to a member function on a non-object,如string,int,......

答案 1 :(得分:0)

Dim LessThanTen As Double Dim CheckTimeBegin As DateTime = Convert.ToDateTime(txttimebegin.Text) Dim CheckTimeEnd As DateTime = Convert.ToDateTime(txttimeend.Text) Dim Duration As TimeSpan = CheckTimeEnd - CheckTimeBegin If Duration.TotalMinutes < 10 Then LessThanTen = True Else LessThanTen = False End If 返回PHP Documentation确认的字符串。您正在尝试访问字符串上的方法base64_decode(),该字符串不是对象,并且没有任何方法。

您现在已经解码了图像的内容,因此您可以调用move()来将解码的内容写入指定的文件。

答案 2 :(得分:0)

我目前正在使用此功能将base64string(数据)转换为图像并重命名...

public function image_upload($filename, $uploadedfile) 
{
    $save_file_path = "/var/www/html/uploads/";
    $save_file_path .= $filename;
    $image_file = base64_decode($uploadedfile);

    //DELETES EXISTING
    if (file_exists($save_file_path)) 
        unlink($save_file_path);

    //CREATE NEW FILE
    file_put_contents($save_file_path, $image_file); 

    //DOUBLE CHECK FILE IF EXIST 
    return ((file_exists($save_file_path)) ? true : false );
}