Convert Image into base64 by JS

When we want to upload a image in on server in cordova/ionic some time we need to convert the image in base64 formate . I've write a function which will convert your image in base64.

function convertImgToBase64URL(url, callback, outputFormat) {
 var img = new Image();
 img.crossOrigin = 'Anonymous';
 img.onload = function() {
  var canvas = document.createElement('CANVAS'),
      ctx = canvas.getContext('2d'),
      dataURL;
  canvas.height = this.height;
  canvas.width = this.width;
  ctx.drawImage(this, 0, 0);
  dataURL = canvas.toDataURL(outputFormat);
  callback(dataURL);
  canvas = null;
 };
 img.src = url;
 return url;
}

How to use :

convertImgToBase64URL(img, function(base64Img) {
 console.log(base64Img);// this is your base64 converted image     
});

Post this image on server(IONIC)

$http.post(server_url,[base64Img])

Handel this image in PHP :

   
$params = json_decode(file_get_contents('php://input'), true); get the post image

$img = imageUploadByBase64($params[0]);// this is the saved image path with image name

function imageUploadByBase64($img) {
 $randomImgName = date('Y-m-d_H-i-s').'_'.rand(0, 6000).'.png';
 $image = base64_to_jpeg($img, 'uploads/' . $randomImgName);// you need to create a directory withh name 'uploads'
 return $image;
}

function base64_to_jpeg($base64_string, $output_file) {
 $ifp = fopen($output_file, "wb");
 $data = explode(',', $base64_string);
 fwrite($ifp, base64_decode($data[1]));
 fclose($ifp);
 return $output_file;
}

Android Runtime error E/AndroidRuntime: FATAL EXCEPTION: main



   java.lang.RuntimeException: Unable to get provider com.google.firebase.provider.FirebaseInitProvider: java.lang.IllegalStateException: Incorrect provider authority in manifest. Most likely due to a missing applicationId variable in application's build.gradle.
   at android.app.ActivityThread.installProvider(ActivityThread.java:5236)
   at android.app.ActivityThread.installContentProviders(ActivityThread.java:4828)
   at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4768)
   at android.app.ActivityThread.access$1600(ActivityThread.java:154)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1441)
   at android.os.Handler.dispatchMessage(Handler.java:102)
   at android.os.Looper.loop(Looper.java:152)
   at android.app.ActivityThread.main(ActivityThread.java:5497)
   at java.lang.reflect.Method.invoke(Native Method)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.IllegalStateException: Incorrect provider authority in manifest. Most likely due to a missing applicationId variable in application's build.gradle.
   at com.google.firebase.provider.FirebaseInitProvider.zza(Unknown Source)
   at com.google.firebase.provider.FirebaseInitProvider.attachInfo(Unknown Source)
   at android.app.ActivityThread.installProvider(ActivityThread.java:5233)
   at android.app.ActivityThread.installContentProviders(ActivityThread.java:4828) 
   at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4768) 
   at android.app.ActivityThread.access$1600(ActivityThread.java:154) 
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1441) 
   at android.os.Handler.dispatchMessage(Handler.java:102) 
   at android.os.Looper.loop(Looper.java:152) 
   at android.app.ActivityThread.main(ActivityThread.java:5497) 
   at java.lang.reflect.Method.invoke(Native Method) 
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 




Add a single line in your build.gradle(yourProject/platforms/android/build.gradle) file 

   below   defaultConfig {

com.your.app <= Replace this with Your app bundle identifier which you can fine in you config.xml file


    defaultConfig {     
     applicationId "com.your.app" //<== Just add this line in you build.gradle file   



Transfer Google Play App form one account to account






Google provide you the facility to transfer you apps to one account to another account, For transfer you google play apps you can follow the below steps : 

Transfer apps to a different developer account(you need to fill this form) :
https://support.google.com/googleplay/android-developer/contact/appt

Get Transaction ID form here :
https://wallet.google.com/manage/?referer=https://support.google.com/googleplay/android-developer/answer/6230247?hl%3Den#transactions:filter=ALL

Need More Details about transfer you google play apps? Please find this url:
https://support.google.com/googleplay/android-developer/answer/6230247?hl=en

Sample php file for send push notification to Android devices

I've write a php file for send push notification to Android devices :

$registration_ids = array(
"fZWd8yT7uLQ:APA91bFywoAAWmXDRLkovcrR9cnIlk_ezTFSlu2nWVMv6LAffE6OC0Ox-gI_n-zPIYeLEzQ4DSKe2bowruSqNgxU3srzcMoyNAwgqmbT2IxjbgNsk7kNRm_ULrebJkcjHUGAfvIhQ3Ze"
);
$api_key = "AIzaSyCdsdFPkRPgkOL8ramcn4J2O1UEr56hMnY";

$data =  array(
            "message" => 'This is test',
            'title' => 'this is title',
            'notId'=>mt_rand(1, 115),  
            //"style" => "inbox", // if you want to show notification in inbox style
    "summaryText" => "There are %n% notifications", // if you want to show summary text in inbox style            
        );

// URL to POST to
$gcm_url = 'https://android.googleapis.com/gcm/send';

// data to be posted
$fields = array('registration_ids' => $registration_ids, 'data' => $data, );

// headers for the request
$headers = array('Authorization: key=' . $api_key, 'Content-Type: application/json');

$curl_handle = curl_init();

echo 'Notification PhP file';

// set CURL options
curl_setopt($curl_handle, CURLOPT_URL, $gcm_url);

curl_setopt($curl_handle, CURLOPT_POST, true);
curl_setopt($curl_handle, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);

curl_setopt($curl_handle, CURLOPT_POSTFIELDS, json_encode($fields));

// send
$response = curl_exec($curl_handle);

echo $response;


curl_close($curl_handle);

Sample php file for send push notification to IOS devices


I've write a php file for send push notification to IOS devices


$deviceToken = "2efacf3609f7e6c59a27d0135cdce7a5e4acfe653e0b73181cc4664c3ef2bb34";
$passphrase = '123456';
$ctx = stream_context_create();

stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);

$fp = stream_socket_client(
                           //'ssl://gateway.sandbox.push.apple.com:2195', $err, // for development
                           'ssl://gateway.push.apple.com:2195', $err, // for production
                           $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);

if (!$fp) {
    exit("Failed to connect: $err $errstr" . PHP_EOL);
    }
  
  echo 'connecting to APNS' . PHP_EOL;
   
    // payload
 $body['aps'] = array(
                 'alert' => "This is a Message",
                 'title' => "This is a Title"
                 );
 
 
// convert payload to json
$payload = json_encode($body);

$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
$result = fwrite($fp, $msg, strlen($msg));

if (!$result) {
   echo 'Message not delivered' . PHP_EOL;
} else
    echo 'Message successfully delivered' . PHP_EOL;
}

fclose($fp);

PHP Database Connection

PHP Database Connection :

$DB  = array();
$DB['hostname'] = "localhost";
$DB['username'] = "ccodes_indihire";
$DB['password'] = "indihire@1234";
$DB['name'] = "ccodes_indihire";

function connect(){
global $DB,$CONN;
//print_array($DB);
$CONN = new mysqli($DB['hostname', $DB['username'], $DB['password'], $DB['name']);
if ($CONN->connect_errno) {
echo "Failed to connect to MySQL: (" . $CONN->connect_errno . "" . $CONN->connect_error;
}
return $CONN;

}

How to use :

$CONN = connect();


Set Current timezone in PHP :

date_default_timezone_set('Asia/Kolkata');

Get Current time in PHP :

$current_date = date('Y-m-d H:i:s');

Remove Junk characters from php array before convert array in JSON

When you creating APIs then the array contains Junk characters and this array will not convert into JSON so if you want to convert this into JSON then you've to remove the Junk characters first. You can use the following function :

function clear_junk_data($row = array()){
$array = array();
foreach($row as $key => $value){
if(is_array($value) || is_object($value)){
$array[$key] = clear_junk_data($value);
}else{
$array[$key] = preg_replace('/[^a-zA-Z0-9_ %:@\[\]\.\(\)%&<\/!#\$\^+>*=,-]/s', '', $value);
}
}
return $array;

}

How to use :

 $arr = clear_junk_data($arr);