Closed lingtalfi closed 2 years ago
Hi lingtalfi, unfortunately is not top left pixel size (border is bordered with black background). Do you know where is problem with your script?
JS cropped:
Server-side cropped:
Hi, I don't know what might be wrong with this script, but anyway I've switched to using js blob directly (and just use php's file_put_contents method on the server side).
In my case it was much simpler: because now the file can be modified in any ways the js gui can provide (for instance cropping, or image filtering), and it will always be pixel perfect.
Hope this helps.
I am sending values from cropper.getData() and that values I used within GD functions imagerotate and imagecrop. I try to do pixel perfect solution, but rotation destroy accuracy. Do you have some working example of your solution with js blob?
Well, server side I just use a basic file_put_contents, you get the data from the $_FILES super array (but of course you should add security checkings...).
On the client side (js), it's based on the XMLHttpRequest object. There is a good resource here: https://developer.mozilla.org/en-US/docs/Web/API/File/Using_files_from_web_applications You might be interested in the section " Handling the upload process for a file ", and the whole page in general if you're not yet familiar with this js api.
I don't know if it will help you, but here is a snippet I use:
/**
* Note: at the time of writing (2020-04-06), the growing fetch api doesn't have a support
* for upload progress monitoring yet (at least not that I know of), so this method uses
* the older XMLHttpRequest api.
*/
uploadFileProgress: async function (url, data, onProgress, decorator) {
return new Promise((resolve, reject) => {
let formdata;
if (data instanceof FormData) {
formdata = data;
} else {
formdata = new FormData();
for (let i in data) {
formdata.append(i, data[i]);
}
}
var ajax = new XMLHttpRequest();
// ajax.overrideMimeType("application/json");
if (this.isFunction(decorator)) {
decorator(ajax);
}
ajax.upload.addEventListener("progress", function (e) {
var percent = Math.round((e.loaded / e.total) * 100, 2);
onProgress(e, percent, e.loaded, e.total);
}, false);
ajax.addEventListener("load", function (e) {
e.stopPropagation();
e.preventDefault();
}, false);
ajax.addEventListener("error", function (e) {
e.stopPropagation();
e.preventDefault();
if ("onError" in options) {
reject(e);
}
}, false);
ajax.addEventListener("abort", function (e) {
e.stopPropagation();
e.preventDefault();
if ("onAbort" in options) {
reject(e);
}
}, false);
ajax.open("POST", url);
ajax.onreadystatechange = function () {
if (ajax.readyState === 4) {
resolve(ajax);
}
};
ajax.send(formdata);
});
},
As mentioned in the comments, I was looking for an upload with progress bar, if you don't need the progress bar, you can just use fetch which is more straightforward:
This page should be helpful: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch
An example from this page is this:
const formData = new FormData();
const fileField = document.querySelector('input[type="file"]');
formData.append('username', 'abc123');
formData.append('avatar', fileField.files[0]);
fetch('https://example.com/profile/avatar', {
method: 'PUT',
body: formData
})
.then(response => response.json())
.then(result => {
console.log('Success:', result);
})
.catch(error => {
console.error('Error:', error);
});
So basically, you want to get your hand on the blob one way or the other. Note that a File object is a Blob in javascript (File extends Blob), so get the file/blob, do all your manipulations on it (jquery cropper, or other manipulations), then send the blob to your server and save it directly (that's what I do, no need for calculus server side).
Good luck.
Here is an early PHP example for reference.
Hi, I love your cropperjs tool. I want to use it in my php projects, so I had to find a php handler script. Here is the function I would use as a starting point, I thought it might be useful to point to it from your documentation (maybe in this section: https://github.com/fengyuanchen/cropperjs#getdatarounded):
Hopefully this might save some php developers some time. Cheers.