如何使用jquery裁剪插件最小化请求的图像
How to minimize requested image using jquery cropit plugin?
我的代码正在裁剪图像,但裁剪的图像非常大。我正在使用Cropit jQuery插件。
"我的视图"在图像上有一个div 蒙版。当表单发布时,隐藏的输入已通过以下脚本裁剪了图像。
<div class="image-editor" style="margin-left:120px; margin-bottom:25px; margin-top:25px;">
<input id="fileinput" type="file" name="filex" class="cropit-image-input">
<div class="cropit-image-preview" style="background-image:url()"></div>
<input type="range" class="cropit-image-zoom-input">
<input type="hidden" name="image-data" value="" class="hidden-image-data" />
<button type="submit" class="export">Crop</button>
</div>
<script>
$(function () {
$('.image-editor').cropit({});
$('.export').click(function () {
var imageData = $('.image-editor').cropit('export');
$('.hidden-image-data').val(imageData);
});
});
</script>
在我的控制器中创建函数通过请求和写入文件获取裁剪图像,但新文件太大。例如,输入文件为 50kb,裁剪后的输出文件为 600kb。通常,裁剪后的图像必须小于输入图像。如何解决问题?
[HttpPost]
public ActionResult Create()
{
var dataurl = Request["image-data"];
var data = dataurl.Substring(dataurl.IndexOf(",") + 1);
var newfile = Convert.FromBase64String(data);
var layoutfilename = Guid.NewGuid().ToString() + "_imgage.jpg";
var dataFile = Server.MapPath(@"~/Img/" + layoutfilename);
System.IO.File.WriteAllBytes(dataFile, newfile);
}
查看 Cropit 的源代码,以下是与输出数据大小相关的代码位:
Cropit.prototype.getCroppedImageData = function(exportOptions) {
var canvas, canvasContext, croppedSize, exportDefaults, exportZoom;
if (!this.imageSrc) {
return null;
}
exportDefaults = {
type: "image/png",
quality: .75,
originalSize: false,
fillBg: "#fff"
};
exportOptions = $.extend({}, exportDefaults, exportOptions);
这是最终由cropit('export')
调用的函数的开始。
发生的事情是,输入数据的压缩产生的输入比质量(7、8,这里不确定)PNG 图像输出的输入要小。
您可以在cropit()
调用中指定选项 - 来自插件的文档:
$imageCropper.cropit('export', {
type: 'image/jpeg',
quality: .9,
originalSize: true
});
相关文章:
- 画布:逐像素绘制图像并请求动画帧计时
- 将phonegap图像发布到php/mysql-图像字段从发布请求中消失
- 防止“;jQuery(html)"防止触发浏览器请求图像和其他参考内容
- 如何缓存AJAX请求的图像
- 在检索数据时是否可以停止图像加载请求
- jQuery对请求图像的外部文本进行查询
- 延迟加载背景图像时防止双重请求
- 如何将 xmlhttp 请求发送到 php 文件以上传图像
- 如何使用 HTML 图像请求将数据发送到服务器,或返回数据作为响应
- 在 post() 请求后在 jquery 中显示图像
- 如何使用jquery裁剪插件最小化请求的图像
- 如何获取 CasperJS 请求的图像的二进制文件
- 如何在Node.js中请求图像并输出图像
- 遵循重定向的图像HTTP请求,客户端
- node.js从http请求列表中将图像下载到服务器
- 使用JavaScript同步请求图像
- 使用iMACROS在运行Javascript后监控/记录HTTP请求(图像请求)
- 在浏览器中缓存图像后,通过请求图像来检查互联网连接不起作用
- 区分当前域和请求图像的域
- 不断请求图像,直到找到为止