title: Swift - uploading base64 encoded image to php and displaying the image
body: <p>Currently, I'm trying to upload a base64 encoded image to a php server which is then storing the base64 string in a MySQL database. Currently, the code is uploading the data and storing it into the MySQL database. However, when I attempt to retrieve the image by specifying the URL used to retrieve the image, a missing image link with the question mark is shown. I have no idea why this is happening since both uploading and displaying base64 encoded images seems to be working just fine with my Android app. </p>
  
  <p>Here is the Swift code I'm using to encode and upload to the server:</p>
  
  <pre><code>    let image: UIImage = imgProfilePic.image!
  
      let size = CGSizeApplyAffineTransform(image.size, CGAffineTransformMakeScale(0.3, 0.3))
      let hasAlpha = false
      let scale: CGFloat = 0.0 // Automatically use scale factor of main screen
  
      UIGraphicsBeginImageContextWithOptions(size, !hasAlpha, scale)
      image.drawInRect(CGRect(origin: CGPointZero, size: size))
  
      let scaledImage = UIGraphicsGetImageFromCurrentImageContext()
      UIGraphicsEndImageContext()
  
      var imageData = UIImageJPEGRepresentation(scaledImage, 0.9)
      var base64String = imageData.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0)) // encode the image
  
      var cd = CoreDataUser(pstrContext: "this")
  
      var params = "strUsername=" + cd.getUsername()
      params = params + "&amp;strPassword=" + cd.getPassword()
      params = params + "&amp;blbProfilePic=" + base64String
  </code></pre>
  
  <p>Here is the PHP code where the base64 string is being decoded and displayed in the browser. This is working fine for images that are uploaded by my Android code, but it just shows a broken image link for images uploaded by my Swift code. </p>
  
  <pre><code> if ($rows) { 
      foreach ($rows as $row) { 
      $data = base64_decode($row["fblbProfilePic"]);
      $image = imagecreatefromstring($data);
      header('Content-Type: Image/jpeg');
      imagejpeg($image);
  //file_put_contents("test.jpg", $data);
  //var_dump($data);
  
      //echo base64_decode($row["fblbPicture"]);
      /    /echo '&lt;img src="data:image/jpg;base64,' . $row["fblbPicture"]     . '" /&gt;';
       }
  </code></pre>