0

My url looks like ..../image/view?id=6

Navigating to this url should give an image from my database. But currently I'm getting a bunch of weird characters as a response.

When I output (see below) I'm seeing my image.

<img src="data:image/jpeg;base64,'.base64_encode( $image->Data ).'"/>

I'm using this piece of code to generate my HTTP response, but the response is just the blob data:

 header('Content-Type :'.$image->Extension);
 header('Content-Disposition: filename='.$image->Name.'.'.$image->Extension);
 header('Content-Length: ' . strlen($image->Data));
 $response->format = Response::FORMAT_RAW;
 $response->data = $image->Data;

Current output begins with:

����JFIF��>CREATOR: gd-jpeg v1.0 (using IJG JPEG v90), default quality ��C     $.' ",#(7),01444'9=82<.342��C     2!!22222222222222222222222222222222222222222222222222���"��    ���}!1AQa"q2���#B��R��$3br� 

Headers used:

   Accept-Ranges: bytes
   Cache-Control: private
   Content-Disposition: inline;        filename="FYdsl67l4PWJQ7QFFeo14Ena76gr0pEP.jpg"
   Content-Length: 320135
   Content-Type: image/jpeg
   Date: Mon, 28 Jan 2019 19:05:11 GMT
   Expires: 0
   Pragma: public
   Server: Microsoft-IIS/8.5
   X-Powered-By: PHP/5.6.24, ASP.NET

Any help would be appreciated

2

3 Answers 3

2

If your image is base64 encoded when it goes in to the database, to display it, you either need to

  1. Use base64_decode($img) and then display it. OR
  2. Do this: echo '<img src="data:image/jpeg;base64,' . $image->Data . '"/>;

What you seem to be doing is encoding it again.

Sign up to request clarification or add additional context in comments.

3 Comments

Hi, the data isn't base64 encoded. When I print the data to the browser it starts with: ����JFIF��>CREATOR: gd-jpeg v1.0 (using IJG JPEG v90), default quality ��C $.' ",#(7),01444'9=82<.342��C 2!!22222222222222222222222222222222222222222222222222���"�� ���}!1AQa"q2���#B��R��$3br� %&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz������������
Does it have the jpeg extension? Is it just this one image or all of them? What is the data type it's being stored as in the database?
It is all of them :(. Installed a new database, and restored a backup of my previous database. Everything works fine. Except all my images and files in the database are having the same issue. I've added a new image to my database, but still the same problem :( I'm saving a binary string to the database, the database field has the type mediumblob
0

You should probably use Response::sendContentAsFile() to send this file:

return Yii::$app->response->sendContentAsFile(
    $image->Data, 
    $image->Name . '.' . $image->Extension, 
    ['mimeType' => FileHelper::getMimeTypeByExtension($image->Name . '.' . $image->Extension)]
);

Note that Conent-Type header is not the same as file extension - refer to FileHelper::getMimeTypeByExtension().

5 Comments

Tested the code, the image is downloaded immediately to my pc. Unfortunately, the image seems to be "damaged / corrupted or is too large". While I'm able to display my image in an img tag by using base64 (but this isn't my preferred solution)
Are you sure you're not adding any excessive headers? Or output before file sending?
Hi, I've changed my code back to the original and added the headers to the question. The data is a binary string, which is in my database of type medium blob
You have two Content-Type headers. You need to figure out source of Content-Type: text/html; charset=UTF-8 header.
Changed my code to your sample, but added the parameter 'inline' => true. The headers are more correct (question has been updated). But the image is still not visible on the link I'm using
0

Issue has been fixed, seems that my file was in an incorrect encoding ... File needed to be iso instead of utf-8

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.