如何解码AngularJS中编码的base64 .jpeg图像?

时间:2016-01-30 21:04:51

标签: php angularjs image ionic-framework

我的离子应用程序中存在一些问题,我想在将图像发送到服务器之前更新图像,因此我捕获了图像并以base64 jpeg格式编码。我希望将这种格式解码成" name.jpg"它应该与php文件解码完全相同,这里是AngularJS中的编码代码和php中的解码代码

角度代码:

$scope.takePicture = function(source)
{
    var options = { 
        quality : 85, 
        destinationType : Camera.DestinationType.DATA_URL, 
        sourceType : source,
        allowEdit : true,
        encodingType: Camera.EncodingType.JPEG,
        targetWidth: 180,
        targetHeight: 180,
        popoverOptions: CameraPopoverOptions,
        saveToPhotoAlbum: false
    };

    $cordovaCamera.getPicture(options).then(function(imageData) {

        $scope.imgURI= "data:image/jpeg;base64," + imageData;


    }, function(err) {
        // An error occured. Show a message to the user
    });

}

php代码:

if (array_key_exists('picture', $data))
{
    define('UPLOAD_DIR','../images/user_img/'); // Change your path location here and change permission to 777 ***
    $img = $picture;
    $img = str_replace('data:image/jpeg;base64,', '', $img);
    $img = str_replace(' ', '+', $img);
    $dataimg = base64_decode($img);
    $filename = uniqid().'.jpeg';
    $file = UPLOAD_DIR.$filename;
    $success = file_put_contents($file, $dataimg);
    $picture = $filename;
    //log_message('info', $success ? $file : 'Unable to save the file.');
}

0 个答案:

没有答案
相关问题