I am trying to convert my base64 image string to an image file. This is my Base64 string:
Using following code to convert it into an image file:
function base64_to_jpeg( $base64_string, $output_file ) {
$ifp = fopen( $output_file, "wb" );
fwrite( $ifp, base64_decode( $base64_string) );
fclose( $ifp );
return( $output_file );
}
$image = base64_to_jpeg( $my_base64_string, 'tmp.jpg' );
But I am getting an error of invalid image
, whats wrong here?
That's an old thread, but in case you want to upload the image having same extension-
You can create 'public_feeds' in laravel's filesystem.php-
This code worked for me.
You need to remove the part that says
data:image/png;base64,
at the beginning of the image data. The actual base64 data comes after that.Just strip everything up to and including
base64,
(before callingbase64_decode()
on the data) and you'll be fine.The problem is that
data:image/png;base64,
is included in the encoded contents. This will result in invalid image data when the base64 function decodes it. Remove that data in the function before decoding the string, like so.