PHP display image BLOB from MySQL

PHP display image BLOB from MySQL

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Why Your PHP BLOB Image Isn't Rendering in Chrome

You have set the Content-Type header correctly to image/jpeg. You are echoing the raw binary data without any whitespace before the output starts. The database query returns a string of 60,766 bytes that begins with ÿØÿà, which is the standard JPEG magic number. Yet, Chrome displays an icon instead of the image.

The issue lies in how the binary data is being fetched and interpreted by the PHP runtime. The var_dump output you provided shows a string containing valid JPEG headers, but there is a critical detail often missed when dealing with BLOBs: character encoding.

While your var_dump suggests the content looks like a standard string, PHP's default handling of binary data can be fragile depending on the database connection and driver version. If the database connection interprets the result set using UTF-8 encoding (which is common), it might attempt to convert binary bytes into characters or alter them if they don't map cleanly to the expected character set. This can silently corrupt the header bytes of the image, causing browsers like Chrome to reject the payload as invalid, even though the MIME type is correct.

The most robust solution is to ensure the database connection explicitly treats the column as binary data and prevents any automatic character set conversion. In MySQLi, you must use PDO::MYSQL_ATTR_FOUND_ROWS or, more importantly, set the client character set to UTF-8 while ensuring the specific query result handles binary correctly. However, the definitive fix usually involves adjusting the PDO attribute for fetching the column or using a raw fetch method that bypasses driver-side buffering issues.

A safer and more explicit approach is to use PDO::PARAM_LOB if you are working with prepared statements, though for simple SELECT queries, ensuring the connection charset matches the file encoding without altering binary data is key. If you are using MySQLi, you can force the driver to return binary data by setting the appropriate fetch options or by ensuring the SQL query does not implicitly cast the column to a string type that might be affected by locale settings.

Another common pitfall involves whitespace handling in the PHP script itself. You mentioned checking for trailing whitespace before and after the <?php tags, which is good. However, sometimes output buffering interferes with binary streams. If ob_start() has been called globally or by another part of your application, it might try to compress or encode the binary stream before sending it to the client. Ensure that no output buffers are active when you execute the header and echo statement. You can flush any existing buffers immediately after setting the header:

header("Content-Type: image/jpeg");
ob_flush();
flush();
echo $image;

If the issue persists, inspect the raw bytes of the variable before echoing it using a hex dump tool or a script that prints the first 8 bytes in hexadecimal. If the magic number FF D8 FF is missing or altered to something like FF D8 FF E0, the data has been corrupted during retrieval.

In many modern PHP versions, simply casting the blob to a string ((string)$image) can sometimes work, but it depends on the internal representation of that string. A more reliable pattern for displaying BLOBs is to use PDO's fetchColumn() with an explicit type cast or to read the data directly into a binary-safe variable.

If you are using Laravel, which is built on top of these core PHP functionalities, you can leverage its specific helpers. For instance, if you are storing images as base64 strings in your database rather than raw BLOBs, you should decode them before outputting, though that changes your storage strategy entirely. If you stick to raw BLOBs, ensure your Eloquent model casts the attribute correctly or fetch it using ->raw():

$image = $product->image; // Ensure this returns binary data, not a string cast

Sometimes the simplest solution is to verify that the database driver isn't silently converting the binary blob into a UTF-8 string representation. If you are using MySQLi, try fetching with fetch_column() instead of fetch_assoc(), as column-level fetching often avoids row-to-array conversion issues that might introduce encoding artifacts.

Finally, if you need to store and retrieve images dynamically in a Laravel application without relying on filesystem paths, consider generating temporary URLs or serving the image through a route that explicitly handles binary output. This ensures that no intermediate rendering engine (like Blade) tries to convert your raw bytes into text before sending them to the browser.

For further guidance on handling binary data streams in PHP applications, including best practices for file uploads and storage within Laravel, you might want to review the resources available at https://laravelcompany.com.

Our network