I am trying to send pictures and videos to my online mySQL database and store them as blobs. First I compress them and store them as base64 encoded strings. To do so I have the following two functions :
FOR PHOTOS :
func compressImage(image:UIImage) -> NSData {
// Reducing file size to a 10th
var actualHeight : CGFloat = image.size.height
var actualWidth : CGFloat = image.size.width
let maxHeight : CGFloat = 1136.0
let maxWidth : CGFloat = 640.0
var imgRatio : CGFloat = actualWidth/actualHeight
let maxRatio : CGFloat = maxWidth/maxHeight
var compressionQuality : CGFloat = 0.5
if (actualHeight > maxHeight || actualWidth > maxWidth){
if(imgRatio < maxRatio){
//adjust width according to maxHeight
imgRatio = maxHeight / actualHeight;
actualWidth = imgRatio * actualWidth;
actualHeight = maxHeight;
}
else if(imgRatio > maxRatio){
//adjust height according to maxWidth
imgRatio = maxWidth / actualWidth;
actualHeight = imgRatio * actualHeight;
actualWidth = maxWidth;
}
else{
actualHeight = maxHeight;
actualWidth = maxWidth;
compressionQuality = 1;
}
}
FOR VIDEOS :
func compressVideo(inputURL: NSURL, outputURL: NSURL, handler:(session: AVAssetExportSession)-> Void)
{
let urlAsset = AVURLAsset(URL: inputURL, options: nil)
let exportSession = AVAssetExportSession(asset: urlAsset, presetName: AVAssetExportPresetMediumQuality)
exportSession!.outputURL = outputURL
exportSession!.outputFileType = AVFileTypeQuickTimeMovie
exportSession!.shouldOptimizeForNetworkUse = true
exportSession!.exportAsynchronouslyWithCompletionHandler { () -> Void in
handler(session: exportSession!)
}
}
I then execute the following lines :
FOR PHOTOS :
let compressedImageNSData = self.compressImage(currImage)
let base64encodedImage = compressedImageNSData.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0))
FOR VIDOES :
let data = NSData(contentsOfURL: self.uploadUrl)
let base64encodedVideo = data!.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0))
Finally I place the encoded strings as a parameter in a POST request using NSURLSession
. I have tested this using my iPad2 and photos seem to post fine, videos up to 5 seconds seem to work fine as well, however anything longer than that results in the INSERT INTO
SQL query in my PHP script failing. Am I doing this correctly or is there a way to further reduce the size of the string, or maybe a different method I should use completely.
UPDATE :
I have found the following limitation in phpmyAdmin :
Is there a way I can further reduce the file size of the videos?