【发布时间】:2015-10-13 21:52:48
【问题描述】:
我正在尝试使用客户端上的 Javascript 和服务器上的 PHP 将图像上传到数据库。
- 从图库中选择第一张图片。
- 在缩放和裁剪后,图像被传递到数据库
问题是当我尝试提交裁剪图像值时未将实际上传的输入“文件”值传递给 php,但我需要将裁剪区域值传递给 PHP。
出于测试目的,如果需要所有的js,我可以提供它。
Js:这会裁剪图像
$(function() {
$('.image-editor').cropit({
exportZoom: 1.25,
imageBackground: true,
imageBackgroundBorderWidth: 40,
});
$('.export').click(function() {
var imageData = $('.image-editor').cropit('export');
window.open(imageData);
});
});
HTML:
<form id="uploadForm" class="image-editor">
<input type="file" class="cropit-image-input">
<!-- .cropit-image-preview-container is needed for background image to work -->
<div class="cropit-image-preview-container">
<div class="cropit-image-preview"></div>
</div>
<div class="image-size-label">
Resize image
</div>
<input type="range" class="cropit-image-zoom-input">
<input type="submit" class="export">Export</input >
</form>
Ajax:ajax 将数据发送到 php
$(document).ready(function (e) {
$("#uploadForm").on('submit', (function (e) {
e.preventDefault();
$.ajax({
url: "upload.php",
type: "POST",
data: new FormData(this),
contentType: false,
cache: false,
processData: false,
success: function (data) {
$("#targetLayer1").html(data);
},
error: function () {}
});
});
});
PHP:
if(count($_FILES) > 0) {
if(is_uploaded_file($_FILES['userImage']['tmp_name'])) {
$mysl = mysqli_connect("localhost", "root", "root","test");
$imgData =addslashes(file_get_contents($_FILES['userImage']['tmp_name']));
$imageProperties = getimageSize($_FILES['userImage']['tmp_name']);
$sql = "UPDATE output_images SET imageType ='{$imageProperties['mime']}',imageData= '{$imgData}' WHERE imageId='16'";
$current_id = mysqli_query($mysl,
$sql) or die("<b>Error:</b> Problem on Image Insert<br/>" . mysqli_error());;
if(isset($current_id)) {
echo "done";
}
}
}
【问题讨论】:
-
为什么不将新维度作为值发送并在 php 中裁剪?
-
@EvadeCaptcha ,好点,但同样的过程应该在 PHP 中作为 PHP 代码重复,对吗??
-
如果是我,我会让 jQuery 从界面创建尺寸,而不是实际在前端裁剪图像(只是让它看起来如此),然后将这些尺寸和图像发送到 php脚本。然后将图像存储在 tmp 文件夹中,并使用尺寸裁剪出要存储在永久位置的新图像。但是,有人可能会用更好的方法来回答,因为我以前从未处理过图像裁剪。
-
这听起来很有趣,我很新在客户端发送图像和服务器接收时,双方都浪费了相同的数据,如果你能详细说明一下,我可以理解我是如何第一次这样做的
-
你是对的,它会发送不必要的数据,因为你只需要图像的一部分,所以这种开销是否值得取决于服务器处理的流量,以及允许的图像大小。这暴露了,如果你能找到一种在前端裁剪它的方法,然后将图像发回,我会这样做。我只是不知道如何在前端创建一个新图像。
标签: javascript php jquery ajax