Cordova文件上传插件:在base64上传图像格式

时间:2016-06-23 10:05:31

标签: cordova ionic-framework cordova-plugins

即时通讯使用IONIC并安装ngCordova

我还使用cordova插件安装文件传输插件add cordova-plugin-file-transfer

当我上传图片然后检查它到服务器base64中的图像格式,我怎么能把它转换成.jpg

这是我的上传功能

$scope.uploadMoment = function() {      

             $ionicLoading.show({
              template: '<p>Mengupload ...</p><ion-spinner icon="android"></ion-spinner>'
            });


            var win = function (r) {
                console.log("Code = " + r.responseCode);
                console.log("Response = " + r.response);
                console.log("Sent = " + r.bytesSent);
                $ionicLoading.hide();
                alert("Success: " + r.response);
            }

            var fail = function (error) {
                alert("An error has occurred: Code = " + error.code);
                console.log("upload error source " + error.source);
                console.log("upload error target " + error.target);
                $ionicLoading.hide();
                /* alert("Error: " + error); */
            }

            var fileURL = $scope.pictureUrl;

            var options = new FileUploadOptions();
            options.fileKey = "photo";
            options.httpMethod = "post";
            options.fileName = fileURL.substr(fileURL.lastIndexOf('/') + 1);
            options.mimeType = "image/jpeg";
            options.chunkedMode = true;

            var params = {};
            params.value1 = "test";
            params.value2 = "param";

            options.params = params;

            var ft = new FileTransfer();
            ft.upload(fileURL, encodeURI("http://somewhere.com/api/uploadMoment.php"), win, fail, options);
        }

这是我的uploadMoment.php

<?php
   header('Access-Control-Allow-Origin: *'); 
    $target_dir = "upload/";
    $target_file = $target_dir . basename($_FILES["photo"]["name"]);
    $uploadOk = 1;
    $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
    // Check if image file is a actual image or fake image

        $check = getimagesize($_FILES["photo"]["tmp_name"]);
        if($check !== false) {
            echo "File is an image - " . $check["mime"] . ".";
            $uploadOk = 1;
            if (move_uploaded_file($_FILES["photo"]["tmp_name"], $target_file)) {
                echo "The file ". basename( $_FILES["photo"]["name"]). " has been uploaded.";
            } else {
                echo "Sorry, there was an error uploading your file.";
            }
        } else {
            echo "File is not an image.";
            $uploadOk = 0;
        }

?>

1 个答案:

答案 0 :(得分:1)

$imageData = base64_decode($imageData);
$source = imagecreatefromstring($imageData);
$rotate = imagerotate($source, $angle, 0); // if want to rotate the image
$imageSave = imagejpeg($rotate,$imageName,100);
imagedestroy($source);

上面的代码从base64创建文件。

问候。

相关问题