我让它工作的方法是修改 Cropit 的质量设置并创建一个读取 base64 字符串并将其转换为图像的 PHP 函数。
下面的代码适用于cropit。我设置了一个超时,因为如果你不这样做,它会在cropit创建base64之前将表单发送到php。 Base64 字符串仅存储在 php 可以基于该文本字段进行通信的文本字段中$_POST
//Initiate Cropper
$('.image-editor').cropit();
//Assign Export Function
$('.export').click(function() {
var imageData = $('.image-editor').cropit('export', {
type: 'image/jpeg',
quality: 0.33,
originalSize: true,
});
//Set value of hidden input to base64
$("#hidden_base64").val(imageData);
//Pause form submission until input is populated
window.setTimeout(function() {
document.upload_form.submit();
alert(imageData);
}, 1000);
});
下面的代码是针对php的。它的工作原理是解码 Base64,然后将其移动到指定的目录。
function decode ($code, $username) {
list($type, $code) = explode(';', $code);
list(, $code) = explode(',', $code);
$code = base64_decode($code);
file_put_contents('directory/filename.jpg', $code);
}