get the file name from base64_decode image

Viewed 6114

Image will send from mobile api with base64 type and I am trying to get the image name from base64_decoded image file.how can I get it?

$data = 'iVBORw0KGgoAAAANSUhEUgAAABwAAAASCAMAAAB/2U7WAAAABl'
       . 'BMVEUAAAD///+l2Z/dAAAASUlEQVR4XqWQUQoAIAxC2/0vXZDr'
       . 'EX4IJTRkb7lobNUStXsB0jIXIAMSsQnWlsV+wULF4Avk9fLq2r'
       . '8a5HSE35Q3eO2XP1A1wQkZSgETvDtKdQAAAABJRU5ErkJggg==';
$data = base64_decode($data);

$im = imagecreatefromstring($data);

here is my fuction trying to get image name written with laravel

   public function completeProfile($session_id,$username,$fullname,$position,$image){
      $user = JWTAuth::toUser($session_id);
      $userid = $user->id;
      $img = base64_decode($image);
      if(file_put_contents(public_path().'/images/users',$img)){
          $result = $this->repository->completeProfile($userid,$username,$fullname,$position,$image);
      }
      return false;
}
2 Answers

It is not possible to get the filename from base64 format, as this only contains the data that makes up the image, not it's metadata.

I have used this code in slim php framwork to save image to desired folder

$img= base64_decode($val_pic);
$image_name=  "test.jpg"; /* name image*/


if( $file = fopen("folder/".$image_name, 'wb')){
            fwrite($file, $img);
            fclose($file);
}
Related