Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

eliekhazzaka's avatar

Laravel Api return image as base 64 string

i create a helper called: FaceImageHeller:

    $type = pathinfo($path, PATHINFO_EXTENSION);
    $data = file_get_contents($path);
    $base64 = 'data:face_image/' . $type . ';base64,' . base64_encode($data);

    return $base64;
}

and in Users modal i put those:

protected $appends = ['imageString'];

public function getImageStringAttribute() {
    $type = pathinfo($this->image, PATHINFO_EXTENSION);
    $data = file_get_contents($this->image);
    $base64 = 'data:face_image/' . $type . ';base64,' . base64_encode($data);
    return $base64;

and then put in the validation Face_image should i put something else ?? are they correct?? what should i do next??

0 likes
16 replies
automica's avatar

@eliekhazzaka the accessor you've added should give you the extra field on your user model.

you'd access it via:

$image = $user->imageString;

what are you using the helper for?

eliekhazzaka's avatar

where should i put it ?? or with what should i replace it??

i am using the helper to convert it to binary

eliekhazzaka's avatar

honestly i only want to retrieve an image from database with base64 binary can you please help me by telling me what should i put and where i should put them please because tmr is the deadline at work and didn't finished it yet please

automica's avatar

@eliekhazzaka I already told you how to retrieve it..

$image = $user->imageString;

just load imageString field on your $user, whether you are requesting the data. Creating an accessor makes it available as a field.

you may also need to add field to your $appends array in your model

 protected $appends = ['imageString'];

Post your controller method if you want me to do it for you?

eliekhazzaka's avatar

that's my registration controller i put a post method on it in the route i need to make a get method to retrieve all data and make the image return base64 binary

$validator = Validator::make($request->all(),[ 'name' =>'required', 'email' =>'required|email', 'password' =>'required', 'c_password' =>'required|same:password', 'face_image=>'required' ]);

    if ($validator->fails()) {
        return $this->sendError('Please validate error' ,$validator->errors() );
    }

    $input = $request->all();
    $input['imei'] = $input['password'];
    $input['password'] = Hash::make($input['password']);
    $user = User::create($input);
    $success['token'] = $user->createToken('Elie')->accessToken;
    $success['name'] = $user->name;
eliekhazzaka's avatar

add it under??

$success['name'] = $user->name;

and keep evrything i posted on the website the same?? and what should i keep and delete from all posts about this subject??

automica's avatar

@eliekhazzaka

add it under?? $success['name'] = $user->name;

yes.

and keep evrything i posted on the website the same??

yes.

I cant see where you are using the FaceImageHeller. in the current usecase, you don't need it.

and what should i keep and delete from all posts about this subject??

not sure what you mean?

1 like
eliekhazzaka's avatar

ok thank you bro very much i deleted the the faceimagehelper will keep evrything the same and delete only the helper

eliekhazzaka's avatar

@automica can i put the the get method in the register controller (register method is post)??

i make a get route and call them all from the register method right??

$validator = Validator::make($request->all(),[ 'name' =>'required', 'email' =>'required|email', 'password' =>'required', 'c_password' =>'required|same:password',

    ]);

    if ($validator->fails()) {
        return $this->sendError('Please validate error' ,$validator->errors() );
    }

    $input = $request->all();
    $input['imei'] = $input['password'];//put imei inside password and delete imei from validation
    $input['password'] = Hash::make($input['password']);
    $user = User::create($input);
    $success['token'] = $user->createToken('Elie')->accessToken;
    $success['name'] = $user->name;
    $success['imageString'] = $user->imageString;
automica's avatar
automica
Best Answer
Level 54

@eliekhazzaka GET method being GET user ?

eg:

/user/{user}

if so, sure.

You can access

$user->imageString

from anywhere you have the user object.

eliekhazzaka's avatar

@automica yes i mean get user

ok thank you so much bro i really appreciated so much

UserModel:

protected $fillable = [ 'name', 'email', 'password', 'imei', 'face_image', ];

/**
 * The attributes that should be hidden for arrays.
 *
 * @var array
 */

protected $appends = ['imageString'];

public function getImageStringAttribute() {
    $type = pathinfo($this->image, PATHINFO_EXTENSION);
    $data = file_get_contents($this->image);
    $base64 = 'data:face_image/' . $type . ';base64,' . base64_encode($data);
    return $base64;
}

protected $hidden = [
    'password',
    'remember_token',
];

with this code and the code i added in the registercontroller and the route get user return all() i can return user info with face_image right?? amd thank you in advence bro

Please or to participate in this conversation.