C#/JS 压缩到指定大小的图片
//因为浏览器安全问题,无法获取上传图片地址,需要先存一遍然后再获取地址作参数上传 var des = CompressImage(@"C:\Users\PC\Pictures\测试\1652344981(1).jpg", @"C:\Users\PC\Pictures\测试\ok.jpg"); Console.WriteLine(des); Console.ReadLine(); /// <summary> /// 无损压缩图片 /// </summary> /// <param name="sFile">原图片地址</param> /// <param name="dFile">压缩后保存图片地址</param> /// <param name="flag">压缩质量(数字越小压缩率越高)1-100</param> /// <param name="size">压缩后图片的最大大小</param> /// <param name="sfsc">是否是第一次调用</param> /// <returns></returns> bool CompressImage(string sFile, string dFile, int flag = 90, int size = 200, bool sfsc = true) { //如果是第一次调用,原始图像的大小小于要压缩的大小,则直接复制文件,并且返回true FileInfo firstFileInfo = new FileInfo(sFile); if (sfsc == true && firstFileInfo.Length < size * 1024) { firstFileInfo.CopyTo(dFile); return true; } Image iSource = Image.FromFile(sFile); ImageFormat tFormat = iSource.RawFormat; int dHeight = iSource.Height / 2; int dWidth = iSource.Width / 2; int sW = 0, sH = 0; //按比例缩放 Size tem_size = new Size(iSource.Width, iSource.Height); if (tem_size.Width > dHeight || tem_size.Width > dWidth) { if ((tem_size.Width * dHeight) > (tem_size.Width * dWidth)) { sW = dWidth; sH = (dWidth * tem_size.Height) / tem_size.Width; } else { sH = dHeight; sW = (tem_size.Width * dHeight) / tem_size.Height; } } else { sW = tem_size.Width; sH = tem_size.Height; } Bitmap ob = new Bitmap(dWidth, dHeight); using (Graphics g = Graphics.FromImage(ob)) //原代码再服务器上经常爆内存不足 { g.Clear(Color.WhiteSmoke); g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; g.DrawImage(iSource, new Rectangle((dWidth - sW) / 2, (dHeight - sH) / 2, sW, sH), 0, 0, iSource.Width, iSource.Height, GraphicsUnit.Pixel); g.Dispose(); } //以下代码为保存图片时,设置压缩质量 EncoderParameters ep = new EncoderParameters(); long[] qy = new long[1]; qy[0] = flag; //设置压缩的比例1-100 EncoderParameter eParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qy); ep.Param[0] = eParam; try { ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders(); ImageCodecInfo jpegICIinfo = null; for (int x = 0; x < arrayICI.Length; x++) { if (arrayICI[x].FormatDescription.Equals("JPEG")) { jpegICIinfo = arrayICI[x]; break; } } if (jpegICIinfo != null) { ob.Save(dFile, jpegICIinfo, ep);//dFile是压缩后的新路径 FileInfo fi = new FileInfo(dFile); if (fi.Length > 1024 * size) { flag = flag - 10; CompressImage(sFile, dFile, flag, size, false); } } else { ob.Save(dFile, tFormat); } return true; } catch { return false; } finally { iSource.Dispose(); ob.Dispose(); } }
//JS版本
<input class="upload-input" name="uploadify1" type="file" id="uploadify1" value="上传" onchange="imgUp()" />
//人脸压缩上传
function imgUp() {
var f = $('#uploadify1')[0].files[0];
var formData = new FormData();
var maxSize = 0.2 * 1024 * 1024; // 200K 定义好图片大小区间,JS插件已经做好大小压缩
var minSize = 0.02 * 1024 * 1024; // 20K
var photoCompress = new PhotoCompress(minSize, maxSize);
photoCompress.compress(f, function (file) {
var r = new FileReader()
r.readAsDataURL(file)
r.onload = function (e) {
var reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = function (e) {
var imgBase64Data = e.target.result;
var fileObj = base64ImgtoFile(imgBase64Data, "file");
console.log("压缩后大小:" + (fileObj.size / 1024)+"K");
formData.append('imgFile', fileObj);
formData.append('dir', "image");
$.ajax({
url: '/ajax/cUserHandler.aspx?action=cUserUploadImage',
type: "post",
dataType: "json",
data: formData,
cache: false,
contentType: false,
processData: false,
success: function (data) {
if (data.error === 0) {
var url = data.url;
//多图
//$("#file2").append("<li style=\"text-align:center;\"><img src=\"" + url + "\" style=\"width:80px;height:80px;\"><br><span><a onclick=\"del(this)\"></a></span></li>");
//$("#img_url1").val($("#img_url1").val() + url + "|");
//单图
$("#file2").html("<li style=\"text-align:center;\"><img src=\"" + url + "\" style=\"width:80px;height:80px;\"><br><span><a onclick=\"del(this)\"></a></span></li>");
$("#img_url1").val(url);
} else { alert(data.message); }
}
});
}
}
});
}
// dataurl 为传进来的base64格式的图片地址, return 返回的为file格式
function base64ImgtoFile(dataurl, filename) {
let arr = dataurl.split(',')
let mime = arr[0].match(/:(.*?);/)[1]
let suffix = mime.split('/')[1]
let bstr = atob(arr[1])
let n = bstr.length
let u8arr = new Uint8Array(n)
while (n--) {
u8arr[n] = bstr.charCodeAt(n)
}
return new File([u8arr], `${filename}.${suffix}`, {
type: mime
})
}
//compressUtil.js 引用
/**
* 图片压缩类
* @param minSize
* @param maxSize
* @constructor
*/
var PhotoCompress = function (minSize, maxSize) {
var nextQ = 0.5; // 压缩比例
var maxQ = 1;
var minQ = 0;
/**
* 将base64转换为文件
* @param base64Codes base64编码
* @param fileName 文件名称
* @returns {*}
*/
PhotoCompress.prototype.dataUrlToFile = function (base64Codes, fileName) {
var arr = base64Codes.split(','),
mime = arr[0].match(/:(.*?);/)[1],
bStr = atob(arr[1]),
n = bStr.length,
u8arr = new Uint8Array(n);
while (n--) {
u8arr[n] = bStr.charCodeAt(n);
}
return new File([u8arr], fileName, { type: mime });
}
/**
* 图片压缩
* @param file 文件
* @param callback 回调函数
*/
PhotoCompress.prototype.compress = function (file, callback) {
var self = this;
self.imgBase64(file, function (image, canvas) {
var base64Codes = canvas.toDataURL(file.type, nextQ); // y压缩
var compressFile = self.dataUrlToFile(base64Codes, file.name.split('.')[0]); // 转成file文件
var compressFileSize = compressFile.size; // 压缩后文件大小 k
console.log("图片质量:" + nextQ);
console.log("压缩后文件大小:" + compressFileSize / 1024);
if (compressFileSize > maxSize) { // 压缩后文件大于最大值
maxQ = nextQ;
nextQ = (nextQ + minQ) / 2; // 质量降低
self.compress(file, callback);
} else if (compressFileSize < minSize) { // 压缩以后文件小于最小值
minQ = nextQ;
nextQ = (nextQ + maxQ) / 2; // 质量提高
self.compress(file, callback);
} else {
callback(compressFile);
}
});
}
/**
* 将图片转化为base64
* @param file 文件
* @param callback 回调函数
*/
PhotoCompress.prototype.imgBase64 = function (file, callback) {
// 看支持不支持FileReader
if (!file || !window.FileReader) return;
var image = new Image();
// 绑定 load 事件处理器,加载完成后执行
image.onload = function () {
var canvas = document.createElement('canvas')
var ctx = canvas.getContext('2d')
ctx.clearRect(0, 0, canvas.width, canvas.height);
canvas.width = image.width * nextQ;
canvas.height = image.height * nextQ;
ctx.drawImage(image, 0, 0, canvas.width, canvas.height);
callback(image, canvas);
};
if (/^image/.test(file.type)) {
// 创建一个reader
var reader = new FileReader();
// 将图片将转成 base64 格式
reader.readAsDataURL(file);
// 读取成功后的回调
reader.onload = function () {
// self.imgUrls.push(this.result);
// 设置src属性,浏览器会自动加载。
// 记住必须先绑定事件,才能设置src属性,否则会出同步问题。
image.src = this.result;
}
}
}
};

浙公网安备 33010602011771号