title: Php | validate base64_decode as image | Not working
body: <p>I created a function that will return the filetype of the supplied base64_decode argument. If it's not an image, the function will return false. However, when passing in encoded images (via jquery ajax (FormData object)) the function returns false everytime. I have tried many things, but am unable to get it working.</p>
  
  <p>EDIT: I am now getting a Javascript error, here it is : <br>
  "Uncaught SyntaxError: Unexpected token � in JSON at position 0"</p>
  
  <pre><code>function base64_extension($base64){
      $base64 = str_replace("data:image/png;base64,", "", $base64);
      $base64 = base64_decode($base64);
  
      $base64 = imagecreatefromstring($base64);
  
      if(!$base64){
          return 0; /* Not image */
      }
  
  
      if(imagepng($base64)){
          imagedestroy($base64);
          return 1;
      }else if(imagejpeg($base64)){
          imagedestroy($base64);
          return 2;
      }
      else if(imagegif($base64)){
          imagedestroy($base64);
          return 3;
      }
      else{
          return 4; /* Not image */
      }
  }
  </code></pre>
  
  <h1>CLIENT</h1>
  
  <pre><code>var cropCanvas = $('#user-img').cropper('getCroppedCanvas');
  var cropImg = cropCanvas.toDataURL("image/jpeg");
  var formData = new FormData();
  formData.append('pic', cropImg);
  </code></pre>
  
  <p>Then I pass the formData into ajax here.</p>