From ebb26ae709570a84004c27f34e9307c33ac6b000 Mon Sep 17 00:00:00 2001 From: Louis Burda Date: Fri, 19 Apr 2024 00:55:07 +0200 Subject: Add Solution --- solve/PhotoEditor/wwwroot/js/site.js | 62 ++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 solve/PhotoEditor/wwwroot/js/site.js (limited to 'solve/PhotoEditor/wwwroot/js/site.js') diff --git a/solve/PhotoEditor/wwwroot/js/site.js b/solve/PhotoEditor/wwwroot/js/site.js new file mode 100644 index 0000000..0e18c6e --- /dev/null +++ b/solve/PhotoEditor/wwwroot/js/site.js @@ -0,0 +1,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.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); + } + }); +} \ No newline at end of file -- cgit v1.2.3-71-gd317