-2

I am developing a website using Cakephp and I am uploading an image in the database as a BLOB. Now when I am retrieving the BLOB data from my Cakephp code I am receiving a text "Resource id #245". How can I convert this into base64?

Displaying image code

foreach($foods as $food)
{
   echo $food->image;
}

Result in

Resource id #245 (I need this as a base64)

CakePhp Version 3.8

  • 1
    Show your code. Vague descriptions are notoriously difficult to debug. – Sammitch Jan 15 '20 at 21:26
  • This is the only code that I am doing just a simple for each to display the image – i_love_code Jan 15 '20 at 21:32
  • 1
    Welcome to StackOverflow, please always mention your _exact_ CakePHP version (last line in `vendor/cakephp/cakephp/VERSION.txt` or `lib/Cake/VERSION.txt`), that makes it easier for people to help you with your questions - thanks! – ndm Jan 15 '20 at 21:34

1 Answers1

2

Binary column types will automatically be associated with the \Cake\Database\Type\BinaryType database type, which will return the data as a stream.

So you can use Filesystem and Stream functions to read the data, and turn it into whatever you want, like:

$binaryData = stream_get_contents($food->image);
$base64Data = base64_encode($binaryData);
ndm
  • 59,784
  • 9
  • 71
  • 110
  • So in my case, I just type `$binaryData = stream_get_contents($food->image)` or I need to add something else :) ? – i_love_code Jan 15 '20 at 21:40
  • @i_love_code I didn't see your update, I've updated the example. For retrieving the data, yes, that's what you'd do. If you want it as base64, then you of course also need to encode it accordingly, just saying in case you weren't just referring to your variable/entity. – ndm Jan 15 '20 at 21:43
  • Yep, it worked perfectly :D. Thank you @ndm. I implemented that code however I did a stupid mistake in the base64_encode but now it is working. – i_love_code Jan 15 '20 at 21:48