master
cody 6 months ago
parent 621d1d556c
commit c7c2eea6a6

@ -1958,22 +1958,18 @@ class OrdersController extends CommonController
// 1. 创建临时文件路径 // 1. 创建临时文件路径
$options = [ $options = [
'format' => 'png', 'format' => 'png',
// 'width' => 400,
// 'height' => 200
]; ];
$tempPath = storage_path('app/temp/' . uniqid() . '.' . $options['format']); $tempPath = storage_path('app/temp/' . uniqid() . '.' . $options['format']);
// 确保临时目录存在 // 确保临时目录存在
Storage::makeDirectory('temp'); Storage::makeDirectory('temp');
// 2. 生成图片到临时文件需要两个参数html内容和输出路径 // 2. 生成图片到临时文件需要两个参数html内容和输出路径
SnappyImage::setOptions($options) SnappyImage::setOptions($options)->generateFromHtml($html, $tempPath);
->generateFromHtml($html, $tempPath);
// 3. 读取临时文件内容并转换为Base64 // 3. 读取临时文件内容并转换为Base64
$imageContent = file_get_contents($tempPath); $imageContent = file_get_contents($tempPath);
$base64Image = 'data:image/' . $options['format'] . ';base64,' . base64_encode($imageContent); $base64Image = 'data:image/' . $options['format'] . ';base64,' . base64_encode($imageContent);
// 4. 删除临时文件(清理资源) // 4. 删除临时文件(清理资源)
// unlink($tempPath); unlink($tempPath);
return response()->json([ return response()->json([
"errorcode" => "200", "errorcode" => "200",
"errormsg" => "success", "errormsg" => "success",

Loading…
Cancel
Save