如何将 BLOB 图像存储到临时文件中?

How can i store a BLOB image into a temporary file?

我目前正在将图像存储到数据库中,我可以使用以下代码将它们显示在网页上:

<?php 
$type = "image/png"; //or the actual mime type of the file
$base64blob = base64_encode($portal['image']); //encode to base64
$datauri = "data:$type;base64,$base64blob";
?>

问题是我需要将它保存为临时文件,以便它可以在以下代码中使用 phpspreadsheet,其中 setpath 行将是临时文件:

$drawing = new \PhpOffice\PhpSpreadsheet\Worksheet\Drawing();
$drawing->setName('Logo');
$drawing->setDescription('Logo');
$drawing->setPath('./images/officelogo.jpg');
$drawing->setHeight(36);

我是新手,请问有什么办法吗,谢谢

$ext = explode("/",$type);
$filename = './images/'.uniqid(rand(), true) . '.'.$ext[1];
file_put_contents($filename,$portal['image']);
$drawing->setPath($filename);
// make Spreadsheet
unlink($filename);