简体   繁体   English

在HTML表单的文本区域中控制图像上传大小

[英]Control image upload size in textarea of HTML Form

I'm using the following WIZWIG editor " http://js.nicedit.com/nicEdit-latest.js " posting into the below HTML form. 我正在使用下面的WIZWIG编辑器“ http://js.nicedit.com/nicEdit-latest.js ”发布到下面的HTML表单中。 I want to control the size of images that are uploaded. 我想控制上传图像的大小。 Image size should be something like 200px height, 300px width. 图片大小应为200px高,300px宽。 I've tried CSS, HTML & JavaScript to no avail. 我尝试了CSS,HTML和JavaScript都无济于事。 Can't figure this out... 无法解决这个问题...

<style>
.nicEdit-main{
background-color: white;
}
</style>

<script type="text/javascript" src="http://js.nicedit.com/nicEdit-  latest.js"></script> 
<script type="text/javascript">
bkLib.onDomLoaded(function() { nicEditors.allTextAreas({buttonList : ['bold','italic','underline','strikeThrough','html','forecolor','link','upload','unlink','removeformat']}) });
</script>

<form name="comments" action="<?php echo $_SERVER['PHP_SELF']; ?>"method="post" onsubmit="return checkForm();">
<?php if ($isGuest) { ?>
Comment:<br><br>
<div style="background: #fff;">
<textarea style="width: 100%;" name="comment" cols="111px" rows="4">disabled for guest account</textarea>
</div>
<input type="submit" name="action" disabled value="Add Comment" />
<?php } else { ?>
Comment:<br><br>
<div style="background: #fff;">
<textarea style="width: 100%;" name="comment" cols="111px" rows="4"></textarea>
</div>
<input type="submit" name="action" value="Add Comment" />  
<?php //header("Location: test.php"); 
} ?>
</form>

Although this was not possible few years ago(you had to send the file to the server side to be able to process file information) now it is do possible with the FileReader() object and pure JS. 尽管几年前这是不可能的(您必须将文件发送到服务器端才能处理文件信息),但是现在可以使用FileReader()对象和纯JS实现。

You use the .files[0] to get the file then create a temporary img object on the DOM(to be able to do the process) and then assign the file to that DOM object(with the help of FileReader() )and then you just compare the file width or height. 您使用.files[0]来获取文件,然后在DOM上创建一个临时img对象(以便执行此过程),然后(在FileReader()的帮助下FileReader()将文件分配给该DOM对象。您只需比较文件的宽度或高度即可。 This is an example how you can do it: 这是一个示例如何执行此操作:

 imgfile.onchange = function(e) { var oFile = document.getElementById("imgfile").files[0]; //Get the selected file by the user var img = document.getElementById("imgdom"); //get the reserve image element on the dom to be able do all our processing with this object type document.getElementById("imgdom").style.display = "none";//This is optional if you want to show or not the image to the user var reader = new FileReader(); //This is the magic object that allows you to process a file on the client side reader.onload = function (e) { console.log(e.total); // file size img.src = e.target.result; // putting file in dom without server upload. alert(img.width); //Do what ever condition you want here //if img.width > 200 do this, else do that }; reader.readAsDataURL(oFile ); //return false; }; 
 <form method="post" enctype="multipart/form-data" onsubmit="return false;"> <input type="file" name="imgfile" id="imgfile"> </form> <div> <img id="imgdom" src="" /> 

This issue has been resolved with the following CSS Code: 以下CSS代码已解决了此问题:

.nicEdit-main img {
width: 350px; 
}

img[src*="http://somedomain.com"] {
width: 350px;
}

Thanks to all who commented... 感谢所有发表评论的人...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM