summaryrefslogtreecommitdiffstats
path: root/solve/PhotoEditor/wwwroot/js/site.js
blob: 0e18c6e19f89c200a2f50f131051020aa54e6eec (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Please see documentation at https://learn.microsoft.com/aspnet/core/client-side/bundling-and-minification
// for details on configuring this project to bundle and minify static web assets.

// Write your JavaScript code.

function getBase64Image(img) {
    // Create an empty canvas element
    var canvas = document.createElement("canvas");
    canvas.width = img.width;
    canvas.height = img.height;

    // Copy the image contents to the canvas
    var ctx = canvas.getContext("2d");
    ctx.drawImage(img, 0, 0);

    // Get the data-URL formatted image
    // Firefox supports PNG and JPEG. You could check img.src to
    // guess the original format, but be aware the using "image/jpg"
    // will re-encode the image.
    var dataURL = canvas.toDataURL("image/png");

    return dataURL.replace(/^data:image\/(png|jpg);base64,/, "");
}

var loadFile = function(event) {
	var image = document.getElementById('output');
	image.src = URL.createObjectURL(event.target.files[0]);
};

function downloadImage() {
    var a = document.createElement("a"); //Create <a>
    a.href = "data:image/png;base64," + getBase64Image(document.getElementById("output")); //Image Base64 Goes here
    a.download = "Cool_modified_image.png"; //File name Here
    a.click(); //Downloaded file
}

function editImage(action, params, types) {
    $('#passwordsNoMatchRegister').slideDown();
    $.ajax({
        type: "POST",
        url: "/api/DynamicPhotoEditor/EditImage",
        data: JSON.stringify(
            {
                Base64Blob: getBase64Image(document.getElementById("output")), 
                DynamicAction: action, 
                Parameters:JSON.stringify(params),
                Types: (types == undefined) ? null : types
            }),
        dataType: "json",
        contentType: "application/json",

        success: function (msg) {
            document.getElementById("output").src = "data:image/png;base64," + msg.base64Blob;
            $('#lastException').hide();
        },
        error: function (req, status, error) {
            var error_obj = JSON.parse(req.responseText);
            document.getElementById("lastException").innerText = error_obj.error;
            $('#lastException').fadeIn(500);
        }
    });
}