How to convert Base64 string from IOS to image in PHP

Posted on March 31, 2016 by Suresh Kamrushi in PHP

Today I learn how you can store images in folder, posted using IOS or Android application to PHP server.
APP need to registered a user and I am able to get and insert all user data pass using JSON format except profile image. APP sending image data as string encoding in base 64 which is looking something like below:

Binary data of image

Binary data of image

APP converted image to string and than encode it into base64 and than send it to the server using RESTFul api.
I need to store image in folder and image name in database table. i cant do this if i am getting image as string, so after googling i got PHP function “imagecreatefromstring()” which will convert the string to resource and than using “imagepng()” function we can save the image as PNG to our web server and insert the file name to table.

Below I am sharing working code of image conversion from string:

$data = base64_decode($userObj->image); 
$im = imagecreatefromstring($data); 
if ($im !== false) 
    $fileName = date('Ymdhis') .".png";
    $resp = imagepng($im, $_SERVER['DOCUMENT_ROOT'].'/citest/assets/uploads/'.$fileName);
} else {
    echo 'An error occurred.'; 


Tagged as , , ,