skip to Main Content

Base64 encoded image is stored in table but when i m trying to display its not showing in edit page. Below is the code of controller and blade file.

   $base64Image = null;
   if ($request->hasFile('profile_picture')) {
     $image = $request->file('profile_picture');
     $base64Image = base64_encode(file_get_contents($image));
   }

   $teacher->profile_picture = $base64Image;```


 <div class="form-group">
   <label>Upload Profile Picture</label>
   <input type="file" class="dropify" name="profile_picture" id="dropify-event" data-default-file="data:image/jpeg;base64,{{ $teacher->profile_image }}">
  <img src="data:image/png;base64,{{ base64_encode($teacher->profile_image) }}" alt="Profile Picture">
</div>

2

Answers


  1. You can try this code to decode any base64 file like image, pdf, mp3, etc. without knowing and having actual file details inside base64 encoded string.

    $encodedString = explode(',', $base64String, 2);
    $decodedImage = str_replace(' ', '+', $encodedString[1]);
    
    $decodedImage = base64_decode($decodedImage);
    $mime_type = finfo_buffer(finfo_open(), $decodedImage, FILEINFO_MIME_TYPE);
    $extension = $this->mime2ext($mime_type);
    
    $imageName = Str::random().'.'.$extension;
    Storage::disk('upload')->put($imageName, $decodedImage);
    return $imageName;
    
    

    add below function to the same class or use trait for identifying file format, you can also add more mime types to this array if you are dealing with any other file formats as well.

    
    public function mime2ext($mime)
    {
        $all_mimes = '{
        "png":["image/png","image/x-png"],
        "bmp":["image/bmp","image/x-bmp","image/x-bitmap","image/x-xbitmap","image/x-win-bitmap","image/x-windows-bmp","image/ms-bmp","image/x-ms-bmp","application/bmp","application/x-bmp","application/x-win-bitmap"],
        "gif":["image/gif"],
        "jpeg":["image/jpeg","image/pjpeg", "image/x-citrix-jpeg"]
        }';
    
        $all_mimes = json_decode($all_mimes,true);
        foreach ($all_mimes as $key => $value) {
            if(array_search($mime,$value) !== false) return $key;
        }
        return false;
    }
    
    

    by using above method you can store base64 image string into database.
    and you can display this as usual we do in laravel inside src attr of image tag.

    Login or Signup to reply.
  2. It seems that you have issues in your controller and view. Add image source as variable to your controller and change the way you are showing base64 encoded image in your view as below:

    $base64Image_src = null;
      if ($request->hasFile('profile_picture')) {
       $image = $request->file('profile_picture');
       $base64Image = base64_encode(file_get_contents($image));
       $base64Image_src = 'data:'.mime_content_type($image).';base64,'.$base64Image;
               }
          
      <!-- View -->
      <div class="form-group">
      <label>Upload Profile Picture</label>
      <input type="file" class="dropify" name="profile_picture" id="dropify-event" data-default-file="{{ $base64Image_src }}">
      <img src="{{ $base64Image_src }}" alt="Profile Picture">
      </div>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search