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

brouckaertd
  • 354
  • 2
  • 4
  • 15
  • check your content type header. for more details check this https://stackoverflow.com/questions/2633908/php-display-image-with-header – Sooraj N Raju Jan 26 '19 at 08:54
  • Thx for the suggestion, but unfortunately, stil the same result. My headers are now: http://prntscr.com/mcehqy – brouckaertd Jan 26 '19 at 09:49

3 Answers3

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.

Difster
  • 3,264
  • 2
  • 22
  • 32
  • 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������������ – brouckaertd Jan 26 '19 at 18:29
  • 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? – Difster Jan 26 '19 at 20:18
  • 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 – brouckaertd Jan 27 '19 at 09:04
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().

rob006
  • 21,383
  • 5
  • 53
  • 74
  • 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) – brouckaertd Jan 27 '19 at 09:10
  • Are you sure you're not adding any excessive headers? Or output before file sending? – rob006 Jan 27 '19 at 10:19
  • 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 – brouckaertd Jan 28 '19 at 18:21
  • You have two `Content-Type` headers. You need to figure out source of `Content-Type: text/html; charset=UTF-8` header. – rob006 Jan 28 '19 at 18:29
  • 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 – brouckaertd Jan 28 '19 at 19:12
0

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

brouckaertd
  • 354
  • 2
  • 4
  • 15