Convert base64 to image file and write To folder in PHP with example

Convert base64 to image file and write To folder in PHP with example

In this post we will give you information about Convert base64 to image file and write To folder in PHP with example. Hear we will give you detail about Convert base64 to image file and write To folder in PHP with exampleAnd how to use it also give you demo for it if it is necessary.

In this tutorial, I will let you know how to handle the images encoded with Base64 and write the images to folder.

While working with API for app, You will notice that they will send the format of images in Base64 encoded. So in that case, you will need to move the Base64 encoded images to server as a image file.

In PHP, Its very easy to get image file from Base64 encoded.

While working with canvas, you will have base64 encoded string in the form, then you will send the form data to the server using POST method and on the server, you convert them into an image file.

  1. <?php
  2.     define('UPLOAD_DIR','images/');
  3. $image_parts=explode(";base64,",$_POST['image']);
  4.     $image_type_aux=explode("image/",$image_parts[]);
  5.     $image_type=$image_type_aux[1];
  6.     $image_base64=base64_decode($image_parts[1]);
  7.     $file= UPLOAD_DIR .uniqid().'.png';
  8.     file_put_contents($file,$image_base64);
  9. ?>    
<?php
	define('UPLOAD_DIR', 'images/');
    $image_parts = explode(";base64,", $_POST['image']);
	$image_type_aux = explode("image/", $image_parts[0]);
	$image_type = $image_type_aux[1];
	$image_base64 = base64_decode($image_parts[1]);
	$file = UPLOAD_DIR . uniqid() . '.png';
	file_put_contents($file, $image_base64);
?>	



Laravel Intervention Summernote Editor to get upload File(image) URL instead of Base64

Try this..

Hope this code and post will helped you for implement Convert base64 to image file and write To folder in PHP with example. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

40  +    =  41

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US