圖片上傳成功後,點擊圖片彈出裁剪介面,支援自訂尺寸裁剪,確定後儲存並替換原圖。
System.Drawing / ImageSharp / SkiaSharp 裁剪原圖,覆蓋正式儲存路徑,刪除暫存檔。| 庫 | 特點 | 適用框架 |
|---|---|---|
| Cropper.js | 原生 JS、功能完整、無依賴 | jQuery, Vue, React, 原生 |
| Vue-Cropper | Vue 2/3 封裝、雙向綁定 | Vue |
| React-Cropper | React Hooks API | React |
| Jcrop | jQuery 依賴、較老舊 | jQuery 專案 |
// ASP.NET Core + ImageSharp
[HttpPost("crop")]
public IActionResult CropImage([FromBody] CropRequest req) {
string tempPath = Path.Combine(_env.WebRootPath, "temp", req.TempFileName);
string finalPath = Path.Combine(_env.WebRootPath, "uploads", req.FinalFileName);
using (var image = Image.Load(tempPath)) {
var rect = new Rectangle(req.X, req.Y, req.Width, req.Height);
image.Mutate(x => x.Crop(rect));
image.Save(finalPath);
}
System.IO.File.Delete(tempPath);
return Ok(new { url = "/uploads/" + req.FinalFileName });
}
public class CropRequest {
public string TempFileName { get; set; }
public string FinalFileName { get; set; }
public int X { get; set; }
public int Y { get; set; }
public int Width { get; set; }
public int Height { get; set; }
}