技术笔记
一名已转行的程序员KindEditor Asp版 上传图片自动压缩
2023-11-27 / 代码笔记 / 644 次围观 / 0 次吐槽今天给老客户修复网站,因为网站年数长久了,调试中发现上传图片20M,像素6000多,上传成功后并未压缩,导致上传后图片特别大,而且一个图片20M还特别占用空间。
决定给上传插件添加压缩图片功能,修改后从20M多,压缩到几百或几十KB,图片根据网页大小降低了像素。这样打开网页加载图片的速度也会提升。
废话不多说,直接上代码。前提是:服务器上必需支持AspJpeg组件。
upload_json.asp 修改 增加 自动压缩代码。
服务器没有安装的下载安装上。
....... 以上省略......... If Not fso.FileExists(filePath) Then showError("上传文件失败。") else '缩小大图 call ysbigpic(filePath) End If Response.AddHeader "Content-Type", "text/html; charset=UTF-8" Set hash = jsObject() hash("error") = 0 hash("url") = fileUrl hash.Flush Response.End Function showError(message) Response.AddHeader "Content-Type", "text/html; charset=UTF-8" Dim hash Set hash = jsObject() hash("error") = 1 hash("message") = message hash.Flush Response.End End Function Sub ysbigpic(FileName) Dim bigpic,bigpicPath,fss,n_MaxWidth Set bigpic = Server.CreateObject("Persits.Jpeg") Set fss=createobject("scripting.filesystemobject") '设置图片质量 bigpic.Interpolation=2 bigpic.Quality=90 ' 图片位置 If fss.fileExists(FileName) then bigpic.Open FileName '下面是按比例缩放 n_MaxWidth=1920 '按比例取得缩略图宽度和高度 Dim n_OriginalWidth, n_OriginalHeight '原图片宽度、高度 Dim n_BuildWidth, n_BuildHeight '缩略图宽度、高度 Dim n '缩小比例 '修改Jpeg n_OriginalWidth = bigpic.Width n_OriginalHeight = bigpic.Height n = 0 If n_OriginalWidth > n_MaxWidth Then n = n_OriginalWidth / n_MaxWidth '指定宽度和高度生成 bigpic.Width = n_MaxWidth bigpic.Height = n_OriginalHeight / n End If ' 保存文件 bigpic.Save (FileName) ' 注销对象 Set bigpic = Nothing End if End Sub
推荐您阅读更多有关于“asp,”的文章
Powered By Z-BlogPHP
Copyright 技术笔记 Rights Reserved.
额 本文暂时没人评论 来添加一个吧
发表评论