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


convert a base64 data into an image file, rename it and move it to a specified location

我正在尝试将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);

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

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

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 );
}

因为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当您尝试在非对象(如字符串、int、...

base64_decode()返回一个从 PHP 文档中确认的字符串。您正在尝试访问字符串上的方法move(),该字符串不是对象,并且没有任何方法。

现在,您已经解码了图像的内容,因此您将能够调用file_put_contents(base_path().'/public/images/uploads/'.$fileName.'_thumbnail.'.$extension, $image);,它将解码的内容写入指定的文件。