Skip to main content

Re size captures image from Web before Uploading.

1) Add a input file tag in your html page.

<input type="button" value="Capture" onclick="document.getElementById('image').click(); return false;" />

<input type="file" accept="image/*" capture="camera" id="image" style="visibility: hidden;">

2) Add below jquery script:

var image= document.getElementById('image');

image.onchange = function() {
  var file = image.files[0];
  var img = document.createElement("img");
  var reader = new FileReader();
  reader.onload = function(e) {
        img.src =;
        img.onload = function () {
           var canvas = document.createElement("canvas");
           var ctx = canvas.getContext("2d");
           ctx.drawImage(img, 0, 0);
           var MAX_WIDTH = 400;
           var MAX_HEIGHT = 300;
           var width = this.width;
           var height = this.height;
           if (width > height) {
              if (width > MAX_WIDTH) {
                 height *= MAX_WIDTH / width;
                 width = MAX_WIDTH;
           } else {
              if (height > MAX_HEIGHT) {
                 width *= MAX_HEIGHT / height;
                 height = MAX_HEIGHT;
           canvas.width = width;
           canvas.height = height;
           var ctx = canvas.getContext("2d");
           ctx.drawImage(img, 0, 0, width, height);
           var dataurl = canvas.toDataURL("image/png");
        image.onerror= function() {
           alert('Invalid file type: '+ file.type);


/* function to upload the image to server */

function upload(file) {
     var form = new FormData();
     form.append('image', file);     
 url: 'server.php',
 data: form,
 processData: false,
 contentType: false,
 type: 'POST',
 success: function(data){

3) in the server side : (server.php)

$filename = "image_".uniqid().".png";
$dest = __DIR__.$filename;

$imgData = str_replace(' ','+',$_POST['image']);
$imgData =  substr($imgData,strpos($imgData,",")+1);
$imgData = base64_decode($imgData);

$file = fopen($dest, 'w');
fwrite($file, $imgData);


Popular posts from this blog

Implementing Client Side WebRTC using Sipml5 javascript

Step 1:

Download and require Sipml5 library function.

Step 2:

Initialize sipml5 Engine in your web page :

var readyCallback = function(e) {
// function called when sipml is successfully initialised.
createSipStack(); // calling this function to create sip stack(see below)

var errorCallback = function(e) {
// function called when error occured during sipml initialisation.

SIPml.init(readyCallback, errorCallback);

Step 3:

Create Sip Stack :-
Sip Stack is an object that must be created before making/receiving call and sms. Creating Sip stack is an asynchronous process, so you need to create an event listener function to get state change notification.

var sipStack;

function EventListener(e) {

* e.type ;type of event listener
* e.session ; current event session
* e.getSipResponseCode() ; event response code
* e.description ; event description

if(e.type == 'started'){
// successfully started the stack.
} else if(e.type == 'i_new_call'){
// when new incoming call comes.

Start an android activity from cordova plugin

We all need to start an android activity from a cordova plugin. Here is a small and complete code to start an activity from cordova plugin
1. Install plugman to create cordova plugin npm install -g plugman2. Create cordova plugin using plugman plugman create --name PluginName --plugin_id com.example.sample.plugin --plugin_version 0.0.1
N.B : plugin id never start with Uppercase Now PluginName directory will be created. Plugin structure will be
> PluginName/

> |- plugin.xml

> |- src/

> |- www/PluginName.js

3. Add android platform to plugin
plugman platform add --platform_name android
Now plugin structure will be

> PluginName/

> |- plugin.xml

> |- src/android/

> |- www/PluginName.js

4. Now create a java file named in src/android diretory
This activity will be shown using our plugin. Write sample activity in
package com.example.sample.plugin; import; import android.os.Bundle;…

Setup Asterisk 13 with FreePBX 13 in CentOS 7

Launch CentOS 7 AWS Ec2 InstanceLog in to your aws consoleGo to ec2 management page and click Launch Instance on Instance pageIn Choose AMI step, go to AWS MarketPlace tab and search CentOS 7 on search field. List of centos 7 ami's will be available.Select CentOS 7 (x86_64) ami which is free tier eligible. Select your instance type and click configure instance. Keep default values from Configure Instance to Add Tags stepsIn configure security group, create a security group which allow minimum ports openSSH : port 22HTTP : port 80Custom UDP Rule : 10000-20000 (if you are giving RTP ports 10000-20000)Custom UDP Rule : 5060 (ChanSIP port)Custom TCP Rule : 5060 (ChanSIP Port)Click Launch Instance. Select your key file and accept terms to launch instance.
Setup CentOS Server SSH into newly created CentOS server with username centos and your key file.Update all existing packagessudo yum update -yNow start http service(you can access your server via browser by going to your server ip addres…