萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網路程式設計 >> ASP 程式設計

ASPJPEG 組件實作:圖片縮圖生成與水印添加完整教學

ASPJPEG 組件完整應用指南:縮圖、水印、格式轉換與效能優化

ASPJPEG (Persits.Jpeg) 是經典 ASP 處理圖片的標準元件。本文詳細說明縮圖生成邏輯、水印添加技巧、品質控制、錯誤處理與記憶體管理最佳實踐。

元件安裝與註冊

  • 下載:Persits Software ASPJPEG
  • 安裝後註冊:regsvr32 aspjpeg.dll
  • IIS 應用程式集區啟用 32 位元應用程式(若元件為 32 位)
  • 測試:Set obj = Server.CreateObject("Persits.Jpeg")

縮圖生成核心函數

<%
' ==========================================
' 縮圖生成:支援等比例、裁切、填充模式
' ==========================================
Function GenerateThumbnail(sourcePath, destPath, maxWidth, maxHeight, mode, quality)
    ' mode: "fit"=等比縮放, "crop"=居中裁切, "fill"=填充背景
    ' quality: 1-100 (JPEG 品質)
    
    On Error Resume Next
    Dim jpeg, srcW, srcH, targetW, targetH, ratio, x, y, bgColor
    
    Set jpeg = Server.CreateObject("Persits.Jpeg")
    jpeg.Open Server.MapPath(sourcePath)
    
    If Err.Number <> 0 Then
        GenerateThumbnail = "OPEN_FAILED:" & Err.Description
        Err.Clear
        Set jpeg = Nothing
        Exit Function
    End If
    
    srcW = jpeg.OriginalWidth
    srcH = jpeg.OriginalHeight
    
    ' 計算目標尺寸
    Select Case LCase(mode)
        Case "fit"  ' 等比縮放,完整顯示
            ratio = srcW / srcH
            If srcW > maxWidth Or srcH > maxHeight Then
                If ratio > maxWidth / maxHeight Then
                    targetW = maxWidth
                    targetH = Round(maxWidth / ratio)
                Else
                    targetH = maxHeight
                    targetW = Round(maxHeight * ratio)
                End If
            Else
                targetW = srcW
                targetH = srcH
            End If
            jpeg.Width = targetW
            jpeg.Height = targetH
            
        Case "crop"  ' 居中裁切填滿
            ratio = srcW / srcH
            If ratio > maxWidth / maxHeight Then
                ' 來源較寬:高度優先匹配,寬度裁切
                targetH = maxHeight
                targetW = Round(maxHeight * ratio)
                jpeg.Width = targetW
                jpeg.Height = targetH
                ' 裁切多餘寬度
                x = (targetW - maxWidth) / 2
                jpeg.Crop x, 0, maxWidth, maxHeight
            Else
                ' 來源較高:寬度優先匹配,高度裁切
                targetW = maxWidth
                targetH = Round(maxWidth / ratio)
                jpeg.Width = targetW
                jpeg.Height = targetH
                y = (targetH - maxHeight) / 2
                jpeg.Crop 0, y, maxWidth, maxHeight
            End If
            
        Case "fill"  ' 填充背景色
            ratio = srcW / srcH
            If ratio > maxWidth / maxHeight Then
                targetW = maxWidth
                targetH = Round(maxWidth / ratio)
            Else
                targetH = maxHeight
                targetW = Round(maxHeight * ratio)
            End If
            jpeg.Width = targetW
            jpeg.Height = targetH
            ' 畫布擴展至目標尺寸,背景色填充
            jpeg.CanvasWidth = maxWidth
            jpeg.CanvasHeight = maxHeight
            bgColor = RGB(255, 255, 255)  ' 白色背景
            ' 需在縮放前設定 Canvas,或縮放後再擴展畫布
    End Select
    
    ' 設定 JPEG 品質
    If quality > 0 And quality <= 100 Then
        jpeg.Quality = quality
    Else
        jpeg.Quality = 85  ' 預設
    End If
    
    ' 儲存
    jpeg.Save Server.MapPath(destPath)
    
    If Err.Number <> 0 Then
        GenerateThumbnail = "SAVE_FAILED:" & Err.Description
        Err.Clear
    Else
        GenerateThumbnail = "OK:" & targetW & "x" & targetH
    End If
    
    Set jpeg = Nothing
    On Error GoTo 0
End Function

' 使用範例
Dim result
result = GenerateThumbnail("/uploads/photo.jpg", "/thumbs/photo_thumb.jpg", 300, 300, "fit", 85)
Response.Write result
%>

水印添加:文字與圖片水印

<%
Function AddWatermark(sourcePath, destPath, watermarkText, watermarkImagePath, position, opacity, fontSize, fontColor)
    ' position: "TL"(左上), "TR"(右上), "BL"(左下), "BR"(右下), "CENTER", "TILE"(平鋪)
    ' opacity: 0-100
    ' fontSize: 字體大小 (pt)
    ' fontColor: RGB 色值
    
    On Error Resume Next
    Dim jpeg, w, h, x, y, markW, markH
    
    Set jpeg = Server.CreateObject("Persits.Jpeg")
    jpeg.Open Server.MapPath(sourcePath)
    w = jpeg.Width
    h = jpeg.Height
    
    ' 文字水印
    If watermarkText <> "" Then
        jpeg.Canvas.Font.Name = "Arial"
        jpeg.Canvas.Font.Size = fontSize
        jpeg.Canvas.Font.Color = fontColor
        jpeg.Canvas.Font.Bold = True
        ' 透明度需在繪製前設定(ASPJPEG 3.0+ 支援)
        ' jpeg.Canvas.Opacity = opacity / 100
        
        ' 計算位置
        Dim textW, textH
        textW = jpeg.Canvas.TextWidth(watermarkText)
        textH = jpeg.Canvas.TextHeight(watermarkText)
        
        Select Case UCase(position)
            Case "TL": x = 10 : y = 10 + textH
            Case "TR": x = w - textW - 10 : y = 10 + textH
            Case "BL": x = 10 : y = h - 10
            Case "BR": x = w - textW - 10 : y = h - 10
            Case "CENTER": x = (w - textW) / 2 : y = (h + textH) / 2
            Case "TILE"
                ' 平鋪邏輯:雙重迴圈繪製
                Dim tx, ty
                For ty = 0 To h Step textH + 20
                    For tx = 0 To w Step textW + 20
                        jpeg.Canvas.DrawText tx, ty, watermarkText
                    Next
                Next
        End Select
        
        If UCase(position) <> "TILE" Then
            jpeg.Canvas.DrawText x, y, watermarkText
        End If
    End If
    
    ' 圖片水印
    If watermarkImagePath <> "" Then
        Dim mark : Set mark = Server.CreateObject("Persits.Jpeg")
        mark.Open Server.MapPath(watermarkImagePath)
        markW = mark.Width
        markH = mark.Height
        
        ' 縮放水印圖(不超過原圖 30%)
        If markW > w * 0.3 Then
            mark.Width = w * 0.3
            mark.Height = mark.Height * (w * 0.3 / markW)
            markW = mark.Width
            markH = mark.Height
        End If
        
        Select Case UCase(position)
            Case "TL": x = 10 : y = 10
            Case "TR": x = w - markW - 10 : y = 10
            Case "BL": x = 10 : y = h - markH - 10
            Case "BR": x = w - markW - 10 : y = h - markH - 10
            Case "CENTER": x = (w - markW) / 2 : y = (h - markH) / 2
        End Select
        
        ' 繪製水印圖(支援透明 PNG)
        jpeg.Canvas.DrawImage x, y, markW, markH, mark, 0, 0, mark.OriginalWidth, mark.OriginalHeight, opacity
        Set mark = Nothing
    End If
    
    jpeg.Save Server.MapPath(destPath)
    If Err.Number <> 0 Then
        AddWatermark = "ERROR:" & Err.Description
    Else
        AddWatermark = "OK"
    End If
    
    Set jpeg = Nothing
    On Error GoTo 0
End Function

' 使用:先縮圖再加水印,或直接在原圖加水印
Call GenerateThumbnail("/uploads/orig.jpg", "/temp/step1.jpg", 800, 600, "fit", 90)
Call AddWatermark("/temp/step1.jpg", "/final/watermarked.jpg", "© MySite.com", "", "BR", 50, 24, RGB(255,255,255))
%>

批次處理與記憶體管理

<%
Sub BatchProcessImages(sourceFolder, destFolder, maxWidth, maxHeight)
    Dim fso, folder, file, fileName, destName, result
    Set fso = Server.CreateObject("Scripting.FileSystemObject")
    Set folder = fso.GetFolder(Server.MapPath(sourceFolder))
    
    For Each file In folder.Files
        fileName = file.Name
        ' 只處理圖片檔
        If LCase(Right(fileName, 4)) = ".jpg" Or LCase(Right(fileName, 4)) = ".png" Or LCase(Right(fileName, 5)) = ".jpeg" Then
            destName = destFolder & "/" & Left(fileName, InStrRev(fileName, ".") - 1) & "_thumb.jpg"
            result = GenerateThumbnail(sourceFolder & "/" & fileName, destName, maxWidth, maxHeight, "fit", 80)
            Response.Write fileName & " → " & result & "
" ' 釋放記憶體:每處理幾張釋放一次物件(函數內已處理) End If Next Set folder = Nothing Set fso = Nothing End Sub ' 呼叫 Call BatchProcessImages("/uploads", "/thumbs", 300, 300) %>

常見錯誤與排查

錯誤原因解決
800a01ad / 800401f3元件未註冊或版本不符重新註冊 DLL、檢查 32/64 位相容性
存取被拒絕資料夾寫入權限不足IUSR/IIS_IUSRS 給予寫入權限
Out of memory大圖處理耗盡記憶體分批處理、增加記憶體限制、用串流處理
PNG 透明變黑JPEG 不支援透明輸出 PNG 格式或合成白底
GIF 動畫只存第一幀ASPJPEG 不支援動畫使用 ImageMagick COM 或其他工具

替代方案:無元件純 ASP (VBScript) 圖片處理

若無法安裝 COM 元件,可考慮:

  • 呼叫外部 ImageMagick / GraphicsMagick 命令列工具 (WScript.Shell.Run)
  • 使用 .NET 組件 (ASP.NET 互通)
  • 前端 Canvas 處理後上傳 Base64
  • 雲端圖片服務
copyright © 萬盛學電腦網 all rights reserved