Wednesday, March 16, 2022

[FIXED] Retrieving image (Resource id #245) instead of base64

Issue

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


Solution

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);


Answered By - ndm

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.