接收圖片路徑、裁切座標、目標尺寸參數,使用 Persits.Jpeg 開啟原圖、縮放至指定寬高、裁切指定區域,最後以二進位輸出 JPEG 圖片串流。
<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<%
Response.Expires = 0
Pic = Request("p")
PointX = CInt(Request("x"))
PointY = CInt(Request("y"))
CutWidth = CInt(Request("w"))
CutHeight = CInt(Request("h"))
PicWidth = CInt(Request("pw"))
PicHeight = CInt(Request("ph"))
Set Jpeg = Server.CreateObject("Persits.Jpeg")
Jpeg.Open Server.MapPath(Pic)
' 先縮放再裁切
Jpeg.Width = PicWidth
Jpeg.Height = PicHeight
Jpeg.Crop PointX, PointY, CutWidth + PointX, CutHeight + PointY
Response.ContentType = "image/jpeg"
Jpeg.SendBinary
%>
<img src="crop.asp?p=/images/photo.jpg&x=10&y=20&w=200&h=150&pw=800&ph=600" />
p:原圖相對路徑。x, y:裁切起始座標(縮放後座標系)。w, h:裁切寬高。pw, ph:縮放目標寬高(先縮放再裁切)。p 路徑限制在允許目錄,防止目錄遍歷讀取任意檔案。Cache-Control: max-age=86400)減少重複處理。