Base64 裁剪图像
Base64 clipping images
以下代码采用名为 cropit 的插件提供的 base64 字符串并将其转换为图像。
list($type, $base64) = explode(';', $base64);
list(, $base64) = explode(',', $base64);
$base64 = str_replace("data:image/jpeg;base64,", "", $base64);
$base64 = base64_decode($base64);
file_put_contents($directory, $base64);
我还将提供我的 javascript,它通过使用输入将 base64 发送到 php 函数。我知道问题是由 PHP 引起的,因为当我将 imageData
发送到新的 window 时,图像将完美显示,没有任何问题。
$('.export_upload').click(function() {
$("#upload_form_identifier").val("upload_form");
var imageData = $('.image-editor-upload').cropit('export', {
type: 'image/jpeg',
quality: 0.3,
originalSize: true
});
//Set value of hidden input to base64
$("#hidden_base64_upload").val(imageData);
//Pause form submission until input is populated
window.setTimeout(function() {
document.upload_form.submit();
}, 1000);
});
我遇到的问题是,如果我输入图像,它会将其剪裁到随机位置。 PHP 会不会是 运行 内存不足?我对 base64 不是很有经验,所以我真的不知道是什么原因造成的。任何帮助都会很棒。
原图:
PHP 处理 base64 后:
虽然这不是最好的解决方案,但它对我很有效,可能会满足您的需要。我发现的问题是 originalSize: true
这将导出图像的裁剪部分而不进行任何压缩,从而导致非常大的 base64。我解决它的方法是将 originalSize
设置为 false,然后将预览调整为我将使用的大小。下面的代码应该可以工作。
$('.export_upload').click(function() {
$("#upload_form_identifier").val("upload_form");
$('.image-editor-upload').cropit('previewSize', {width:1024, height:1024});
var imageData = $('.image-editor-upload').cropit('export', {
type: 'image/jpeg',
quality: .75,
originalSize: false
});
//Set value of hidden input to base64
$("#hidden_base64_upload").val(imageData);
//Pause form submission until input is populated
window.setTimeout(function() {
window.open(imageData);
document.upload_form.submit();
}, 1000);
});
关键是$('.image-editor-upload').cropit('previewSize', {width:1024, height:1024});
。这会在将图像发送到 php 函数之前调整图像大小。唯一真正的问题是,如果用户修改 javascript,他们将能够更改图像的输出大小,但如果您使用 php 验证上传到确保宽度和高度与括号内的内容匹配。
我今天想出了一个基本的验证功能。根据图像的尺寸是否正确,它将 return 真或假。您可以将此应用于设置图像的初始表单,并检查它是否匹配并相应地抛出错误。
/**
* Checks the dimensions of the provided image
* @param string $base64_image Base64 string of the image
* @param string $width Desired width of the image
* @param string $height Desired height of the image
* @return bool True if dimensions match, false if dimensions do not match
*/
public function checkImageDimensions ($base64_image, $width, $height) {
list($type, $base64_image) = explode(';', $base64_image);
list(, $base64_image) = explode(',', $base64_image);
$base64_image = base64_decode($base64_image);
$dimensions = getimagesizefromstring($base64_image);
if ($dimensions[0] == $width && $dimensions[1] == $height) {
return true;
} else if ($dimensions[0] !== $width && $dimensions[1] !== $height) {
return false;
}
}
以下代码采用名为 cropit 的插件提供的 base64 字符串并将其转换为图像。
list($type, $base64) = explode(';', $base64);
list(, $base64) = explode(',', $base64);
$base64 = str_replace("data:image/jpeg;base64,", "", $base64);
$base64 = base64_decode($base64);
file_put_contents($directory, $base64);
我还将提供我的 javascript,它通过使用输入将 base64 发送到 php 函数。我知道问题是由 PHP 引起的,因为当我将 imageData
发送到新的 window 时,图像将完美显示,没有任何问题。
$('.export_upload').click(function() {
$("#upload_form_identifier").val("upload_form");
var imageData = $('.image-editor-upload').cropit('export', {
type: 'image/jpeg',
quality: 0.3,
originalSize: true
});
//Set value of hidden input to base64
$("#hidden_base64_upload").val(imageData);
//Pause form submission until input is populated
window.setTimeout(function() {
document.upload_form.submit();
}, 1000);
});
我遇到的问题是,如果我输入图像,它会将其剪裁到随机位置。 PHP 会不会是 运行 内存不足?我对 base64 不是很有经验,所以我真的不知道是什么原因造成的。任何帮助都会很棒。
原图:
PHP 处理 base64 后:
虽然这不是最好的解决方案,但它对我很有效,可能会满足您的需要。我发现的问题是 originalSize: true
这将导出图像的裁剪部分而不进行任何压缩,从而导致非常大的 base64。我解决它的方法是将 originalSize
设置为 false,然后将预览调整为我将使用的大小。下面的代码应该可以工作。
$('.export_upload').click(function() {
$("#upload_form_identifier").val("upload_form");
$('.image-editor-upload').cropit('previewSize', {width:1024, height:1024});
var imageData = $('.image-editor-upload').cropit('export', {
type: 'image/jpeg',
quality: .75,
originalSize: false
});
//Set value of hidden input to base64
$("#hidden_base64_upload").val(imageData);
//Pause form submission until input is populated
window.setTimeout(function() {
window.open(imageData);
document.upload_form.submit();
}, 1000);
});
关键是$('.image-editor-upload').cropit('previewSize', {width:1024, height:1024});
。这会在将图像发送到 php 函数之前调整图像大小。唯一真正的问题是,如果用户修改 javascript,他们将能够更改图像的输出大小,但如果您使用 php 验证上传到确保宽度和高度与括号内的内容匹配。
我今天想出了一个基本的验证功能。根据图像的尺寸是否正确,它将 return 真或假。您可以将此应用于设置图像的初始表单,并检查它是否匹配并相应地抛出错误。
/**
* Checks the dimensions of the provided image
* @param string $base64_image Base64 string of the image
* @param string $width Desired width of the image
* @param string $height Desired height of the image
* @return bool True if dimensions match, false if dimensions do not match
*/
public function checkImageDimensions ($base64_image, $width, $height) {
list($type, $base64_image) = explode(';', $base64_image);
list(, $base64_image) = explode(',', $base64_image);
$base64_image = base64_decode($base64_image);
$dimensions = getimagesizefromstring($base64_image);
if ($dimensions[0] == $width && $dimensions[1] == $height) {
return true;
} else if ($dimensions[0] !== $width && $dimensions[1] !== $height) {
return false;
}
}