是否可以返回使用 Laravel API 资源动态创建的图像而不保存它?在控制器中尝试过
$sum = Product::where('slug', $product)->first();
$img = Image::make('image_path')->resize(400, 400);
$img->text(name, 205, 138, function($font) {
$font->file('fonts/BostonHeavy.woff');
$font->size(42);
$font->color('#ffffff');
$font->align('center');
$font->valign('top');
});
return (new ProductResource($sum))->foo($img);
Laravel API 资源
<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\JsonResource;
class ProductResource extends JsonResource
{
protected $image;
public function foo($value){
$this->image = $value;
return $this;
}
public function toArray($request)
{
return [
'id' => $this->id,
'name' => $this->name,
'image' => $this->image,
];
}
}
什么被退回
{
"data": {
"id": 1,
"name": "Such a pickle",
"image": {
"encoded": "",
"mime": "image/jpeg",
"dirname": null,
"basename": null,
"extension": null,
"filename": null
}
}
}
这显然在它说使用 return $img->response('jpg'); 的文档中不起作用。它是自己的作品,但我想将它添加到响应中,而不是执行两个获取请求。