Shaik
Shaik

Reputation: 930

Save Image to MySQL After crop Using ajax And PHP

Below code crops the image after chosen from the local then provides a copy.

Is there a possible way to save the cropped image to MySQL as I have seen many articles but they use PHP for both server side and client side but as am using this to a mobile app where that allows JavaScript or Jquery in client side. And on server side am using PHP.

JSfiddel

HTML:

 <body ng-app="app" ng-controller="Ctrl">
      <div>Select an image file: <input type="file" id="fileInput" /></div>
      <div class="cropArea">
        <img-crop image="myImage" result-image="myCroppedImage"></img-crop>
      </div>
      <div>Cropped Image:</div>
      <div><img ng-src="{{myCroppedImage}}" /></div>
    </body>

JS:

angular.module('app', ['ngImgCrop'])
  .controller('Ctrl', function($scope) {
    $scope.myImage='';
    $scope.myCroppedImage='';

    var handleFileSelect=function(evt) {
      var file=evt.currentTarget.files[0];
      var reader = new FileReader();
      reader.onload = function (evt) {
        $scope.$apply(function($scope){
          $scope.myImage=evt.target.result;
        });
      };
      reader.readAsDataURL(file);
    };
    angular.element(document.querySelector('#fileInput')).on('change',handleFileSelect);
  });

Upvotes: 1

Views: 1346

Answers (1)

vanarajcs
vanarajcs

Reputation: 978

Try this.

 $data = json_decode(file_get_contents('php://input'), true);

 $img = str_replace('data:image/png;base64,', '', $data['img']);

 $img = str_replace(' ', '+', $img);

 $data = base64_decode($img);

 $file = "images/4.jpg";

 file_put_contents($file, $data);

Upvotes: 1

Related Questions