首页 > 解决方案 > 使用干预图像从二进制数据调整图像大小

问题描述

我正在尝试使用 Intervention Image 包从从 mysql 表中检索到的数据调整图像的大小。不调整大小它工作正常。我被困在 Image::make 没有返回数据。这是我的代码:

$data = DB::table('accounts')
      ->leftjoin('coatypes','accounts.type','=','coatypes.typeid')
      ->leftjoin('attachments','attachments.vno','=','accounts.code')
      ->where('accounts.type','=',$request->type)
      ->where('accounts.branchid','=',$branch_id)
      ->where('attachments.branchid','=',$branch_id)
      ->where('attachments.vtype','=','Customer')
      ->select('accounts.*','coatypes.typename','attachments.docfile')
      ->latest()->get();

foreach($data as &$d) {
  $decoded_image = $d->docfile;
  $encoded_image = base64_encode($d->docfile);
  $img = Image::make($decoded_image)->resize(100, 100);
  info($img);
}

编码和解码的图像都在日志中返回空白数据

[2020-02-07 15:51:01] local.INFO:   
[2020-02-07 15:51:01] local.INFO:   
[2020-02-07 15:51:01] local.INFO:   

标签: laravel

解决方案


您的数据库中已经有一个 base64 字符串并且不需要base64_encode或者系统上有图像的路径,在这种情况下,您需要先获取图像内容(使用file_get_contents),然后再将其编码为 base64。

顺便说一句:
据我所知,干预图像也接受图像路径。


推荐阅读