[英]Choose an image in browser, resize it and send it to server with JS, and let PHP save it on server
I'm working on this classical feature: choose a file in the browser ("Browse"), let JavaScript resize it (max width / height = 500 pixels) and upload it to server, and then let PHP save it to disk. 我正在研究这种经典功能:在浏览器中选择一个文件(“浏览”),让JavaScript调整其大小(最大宽度/高度= 500像素)并上传到服务器,然后让PHP将其保存到磁盘。
Here is what I currently have (based on this other question ) 这是我目前拥有的(基于另一个问题 )
$("#fileupload").change(function(event) { var file = event.target.files[0]; var reader = new FileReader(); reader.onload = function (readerEvent) { var image = new Image(); image.onload = function (imageEvent) { var canvas = document.createElement('canvas'), max_size = 500, width = image.width, height = image.height; if (width > height) { if (width > max_size) { height *= max_size / width; width = max_size; } } else { if (height > max_size) { width *= max_size / height; height = max_size; } } canvas.width = width; canvas.height = height; canvas.getContext('2d').drawImage(image, 0, 0, width, height); var dataUrl = canvas.toDataURL('image/jpeg'); var resizedImage = dataURLToBlob(dataUrl); $.ajax({type: "POST", url: "index.php", success: function(data, status) { }, data: { content: resizedImage, filename: ?? }}); } image.src = readerEvent.target.result; } reader.readAsDataURL(file); } var dataURLToBlob = function(dataURL) { var BASE64_MARKER = ';base64,'; if (dataURL.indexOf(BASE64_MARKER) == -1) { var parts = dataURL.split(','); var contentType = parts[0].split(':')[1]; var raw = parts[1]; return new Blob([raw], {type: contentType}); } var parts = dataURL.split(BASE64_MARKER); var contentType = parts[0].split(':')[1]; var raw = window.atob(parts[1]); var rawLength = raw.length; var uInt8Array = new Uint8Array(rawLength); for (var i = 0; i < rawLength; ++i) { uInt8Array[i] = raw.charCodeAt(i); } return new Blob([uInt8Array], {type: contentType}); }
<input id="fileupload" type="file">
The PHP part would be: PHP部分将是:
<?php
if (isset($_POST['content'])) file_put_contents($_POST['filename'], $_POST['content']);
?>
This currently doesn't work, how should I do the AJAX part to make it work (how to send the data + filename)? 当前这不起作用,我应该如何做AJAX部分以使其起作用(如何发送数据+文件名)? Should a FormData
be used, if so why and how? 是否应该使用FormData
?如果是,为什么以及如何使用?
You need to use $_FILES array to retrieve the image info: 您需要使用$ _FILES数组来检索图像信息:
if (is_uploaded_file($_FILES['new_img']['tmp_name'])) {
$newimg = $_FILES['new_img']['name'];
move_uploaded_file($_FILES['new_img']['tmp_name'], "../images/{$newimg}");
}
file_put_contents
is a function to write files. file_put_contents
是写入文件的功能。 When you upload a file it is stored in a temp dir, so you can check with is_uploaded_file
that it is uploaded with the $_FILES['new_img']['tmp_name']
. 上载文件时,该文件存储在临时目录中,因此可以使用is_uploaded_file
来检查该文件是否已使用$_FILES['new_img']['tmp_name']
。
You can use it's original name with $_FILES['new_img']['name']
or rename it to prevent injections. 您可以将其原始名称与$_FILES['new_img']['name']
配合使用,也可以对其重命名以防止注入。 Then you have to move the image to it's final location with move_uploaded_file
. 然后,您必须使用move_uploaded_file
将图像移动到最终位置。
I almost forgot the form: 我几乎忘记了表格:
<form action="yourpage.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="new_img" id="new_img">
<input type="submit" value="Upload Image" name="submit">
</form>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.