Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

derrickrozay's avatar

Image blob Malformed UTF-8 characters, possibly incorrectly encoded

I am trying to retrieve an image saved as a blob but I keep getting this error "Malformed UTF-8 characters, possibly incorrectly encoded" when I try to return it as JSON using Laravel 5.4.

database.php mysql configuration

'charset'   => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix'    => '',
public function getpic()
{
    $pic = DB::connection('mysql')
            ->table('itempictures')
            ->select('picture')
            ->get();

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

The collation of the table is latin1_swedish_ci

The collation of the picture column itself is null

print_r of the image

var_dump of the image

0 likes
10 replies
derrickrozay's avatar

The images are already in the database I only retrieve them. I have updated the original post with my code

derrickrozay's avatar

Like this you mean

        //return response()->json(['test' => $test], 200,['Content-type'=> 'application/json; charset=utf-8']);

Doesnt work :(

Cronix's avatar

Your collation is latin1 and you're sending it out saying it's UTF8, which is why it's complaining since it isn't UTF8.

You should probably convert your database tables/fields to UTF8.

Even better yet is to only store the path to the image in the database, and not the image itself. Databases are for data. Images (files) belong in the filesystem. You're putting an extra load on the db where it shouldn't be. It's a lot more work and overhead for the server to retrieve an image from the database at the php/database level and then output it than it is to just put the src="/path/to/file.jpg" in an image tag, which then is loaded separately by the browser and doesn't touch php/database layers.

Cronix's avatar

If you fix your database like I suggested it would just work without a hacky workaround of having to re-encode. There's no reason to use the outdated latin1 charset when you could just use utf8 and have everything encoded properly to begin with.

derrickrozay's avatar

@Cronix I have no control over how the database is structured. Maybe I could convince my boss to change this particular table and its fields to UTF-8 but I've never done that so I'm not really comfortable recommending it because I don't know how hard it will be or if it the data could get corrupted.

Cronix's avatar

@derrickrozay Understood, but it's pretty easy to do. Of course, backup all data first.

change the encoding for the database and set the charset

ALTER DATABASE db_name DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;

convert the existing table data to utf8 (execute for each table)

ALTER TABLE db_table CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;

An issue can arise if you are already storing utf8 data in a non-utf8 table when it tries to convert to utf8 (the 2nd query). I don't think that's your case, but something to be aware of.

Please or to participate in this conversation.