简体   繁体   中英

Cordova File Upload Plugin: When uploaded the image format in base64

im using IONIC and install ngCordova

i also install the file transfer plugin using cordova plugin add cordova-plugin-file-transfer

When i upload the image then check it to server the image format in base64, how can i convert it into .jpg

this is my upload function

$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);
        }

This is my 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;
        }

?>
$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);

Above code creates file from base64.

Regards.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM