Javascript 来自cropit的Base64图像在使用PHP解码时被剪裁
我正在使用cropit jquery插件来管理我网站上的图像裁剪。我设置它的方式是cropit将给我一个base 64字符串,我将传递给PHP,PHP将对其进行解码并将其放入文件夹中。问题是,当我解码字符串时,它只会生成图像的1/10,其余部分将是白色/透明的。我的代码如下: jQuery:Javascript 来自cropit的Base64图像在使用PHP解码时被剪裁,javascript,php,jquery,Javascript,Php,Jquery,我正在使用cropit jquery插件来管理我网站上的图像裁剪。我设置它的方式是cropit将给我一个base 64字符串,我将传递给PHP,PHP将对其进行解码并将其放入文件夹中。问题是,当我解码字符串时,它只会生成图像的1/10,其余部分将是白色/透明的。我的代码如下: jQuery: var username = "<?php echo $userData['username']; ?>"; $('#image-cropper').cropit({
var username = "<?php echo $userData['username']; ?>";
$('#image-cropper').cropit({
imageState:{
src:'users/'+username+'/profile_picture.jpg'
},
});
$('#image-cropper').cropit('previewSize', {width:500, height:500});
$('.export').click(function() {
var imageData = $('#image-cropper').cropit('export');
//$("#code").val(imageData);
window.open(imageData);
});
当我的宽度/高度为$(“#图像裁剪器”).cropit('previewSize',{width:500,height:500})时,这里的代码正在工作代码>设置为250。我不得不改变它,因为如果没有更大的宽度/高度,它将保存一个非常低分辨率的图像,这仍然是一个问题,但没有那么重要。任何帮助都会很好。谢谢
在浏览器中查看base64:
使用PHP解码时的base64:
使用上述函数使用base64编码值创建图像,这里需要将参数传递给函数decode('YOUR\u image\u encoded\u STRING')
我的输出:
导出功能用作大小限制的数据URI方案(取决于浏览器)
由于cropit导出功能允许调整图像格式和压缩因子,您可以尝试以jpeg格式保存,并在数据URI限制内调整质量以获得最佳结果:
// Returns the cropped image in Data URI format.
// The second argument `options` takes the following keys:
// - [type='image/png'] exported image type.
// - [quality=.75] exported image quality, only works when type is
// 'image/jpeg' or 'image/webp'.
// - [originalSize=false] when set to true, export cropped part in
// original size, overriding exportZoom.
// - [fillBg='#fff'] if `type` is 'image/jpeg', this color will be
// filled as the background of the exported image.
$imageCropper.cropit('export', {
type: 'image/jpeg',
quality: .9,
originalSize: true
});
仍然给出同样的问题。我的代码现在是随机发生的。有时它会工作得很好,但有时它不会工作。你的代码对我根本不起作用,但在我这一头。。工作很好。。好像。。在显示html时,出现了一些问题。检查那里..我把质量调低到了.1,它的剪辑效果比以前更差。不过,似乎用.9质量将originalSize
设置为false修复了它。你可以尝试降低质量,通常0.7就足够了,你必须检查几个浏览器。看起来有时候不管我做什么,图像都不会被检测到。jpg或png。“检测到”,在哪一步?在哪个浏览器上?
function decode ($base64) {
$explodeBase64 = explode(";base64,", $base64);
$code = base64_decode($explodeBase64[0]);
file_put_contents('users/' . $userData['username'] . '/profile_picture.'.basename(@$explodeBase64[0]), $code);
}
// Returns the cropped image in Data URI format.
// The second argument `options` takes the following keys:
// - [type='image/png'] exported image type.
// - [quality=.75] exported image quality, only works when type is
// 'image/jpeg' or 'image/webp'.
// - [originalSize=false] when set to true, export cropped part in
// original size, overriding exportZoom.
// - [fillBg='#fff'] if `type` is 'image/jpeg', this color will be
// filled as the background of the exported image.
$imageCropper.cropit('export', {
type: 'image/jpeg',
quality: .9,
originalSize: true
});