【问题标题】:Image blob Malformed UTF-8 characters, possibly incorrectly encoded图像 blob 格式错误的 UTF-8 字符,可能编码不正确
【发布时间】:2017-11-27 04:59:36
【问题描述】:

我正在尝试检索保存为 blob 的图像,但是当我尝试使用 Laravel 5.4 将其作为 JSON 返回时,我不断收到此错误“UTF-8 字符格式错误,可能编码不正确”。出了什么问题,我该如何解决?

database.phpmysql配置

'charset'   => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix'    => '',

检索图像

public function getpic()
{
    $pic = DB::connection('mysql')
            ->table('itempictures')
            ->select('picture')
            ->get();

    return response()->json($pic);
}

表的collationlatin1_swedish_ci

列本身的collationnull

图片的print_r

var_dump of the image

图片的dd

【问题讨论】:

  • 你有什么问题?
  • 我想知道怎么回事
  • 我认为您对编码的假设是错误的,编码是关于人类可读的文本,而不是关于二进制(大)对象。我的意思是转码不应该发生在链中的任何地方。排序是关于使用编码对文本进行排序,它也与 blob 无关。
  • @Code4R7 对不起,我没有关注。你是说数据没有错吗?
  • 问题的原因不在您的数据库中。图像是未经编码的二进制数据,不得将其解析为编码数据。当您尝试将其返回为 JSON (Unicode) 时,这似乎正在发生。也许您可以分享您如何将图像数据放入 JSON 字符串?

标签: php laravel encoding utf-8


【解决方案1】:

您正在尝试对二进制文件进行 json 解码,而应使用原始图像二进制数据进行响应,并使用某些图像类型修改响应内容标头,例如:

public function getImage($id)
{
    if($ehtufile = EhtuFile::find($id))
    {
        return response($ehtufile->filedata, 200)->header('Content-Type', 'image/jpeg');
    } else
    {
        // TODO: No image found:...
        $arRes = ['res' => 'Error', 'text' => 'Slide Show Image not found'];
        return "nope";
    }
}

我希望这会有所帮助!,

大卫·里昂

【讨论】:

    猜你喜欢
    • 2017-02-17
    • 2019-06-30
    • 2018-02-28
    • 2018-09-01
    • 2018-05-24
    • 2019-03-01
    • 1970-01-01
    相关资源
    最近更新 更多