Bilder verkleinern

  • Hallo,

    Ich steh vor einem Problem...
    Ich brauch für meine Webside Bilder in bestimmter größe und im jpg format.
    Es gibt zwar ziemlich viele Programme die dies für mich erledigen könnten,
    dennoch möchte ich das Programm ganz gerne zumindest zu einem gewissen grad
    selbst machen.

    Deswegen such ich n Scriptteil der mir ein Bild, oder mehrere in einem Ordner
    auf z.b. 640 x 500 px verkleinert und die Bilder in das Format .jpg convertiert.

    Ich hoffe ihr könnt mir helfen :)
    Danke im Vorraus :)

    MfG
    Rowdy

  • Spoiler anzeigen
    [autoit]

    #include <GDIPlus.au3>
    #include <WinAPI.au3>

    [/autoit] [autoit][/autoit] [autoit]

    _ImageResize(@DesktopDir & "\Chrysanthemum.jpg", @DesktopDir & "\Chrysanthemum_resized.jpg", @DesktopWidth, @DesktopHeight)

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    ; #FUNCTION# =========================================================================================
    ; Name...........: _ImageResize
    ; Description....: Resize an image and optionally convert it to the format you want.
    ; Syntax.........: _ImageResize($sInImage, $sOutImage, $iW, $iH)
    ; Parameters ....: $sInImage - Full path to the image to resize / convert.
    ; In types: *.bmp, *.gif, *.ico, *.jpg, *.jpeg, *.png, *.tif, *.tiff
    ; $sOutImage - Full path where to save the resized / converted image.
    ; Out types: *.bmp, *.gif, *.jpg, *.jpeg, *.png, *.tif, *.tiff
    ; $iW - Width to resize image to.
    ; $iH - Height to resize image to.
    ; Return values .: Success - Return 1 and @error 0
    ; Failure - Return 0 and @error 1~5
    ; @error 1 = In File does not exist
    ; @error 2 = In File format not supported
    ; @error 3 = Out File path does not exist
    ; @error 4 = Out file format not supported
    ; @error 5 = Resize Width or Height not an integer
    ; Author ........: smashly
    ; ====================================================================================================
    Func _ImageResize($sInImage, $sOutImage, $iW, $iH)
    Local $sOP, $sOF, $sInExt, $Ext, $hBitmap, $hImage1, $hImage2, $hGraphic, $CLSID, $i = 0
    Local $sType = "BMP|GIF|ICO|JPG|JPEG|PNG|TIF|TIFF"

    [/autoit] [autoit][/autoit] [autoit]

    If Not FileExists($sInImage) Then Return SetError(1, 0, 0)
    $sInExt = StringUpper(StringTrimLeft($sInImage, StringInStr($sInImage, ".", 0, -1)))
    If Not StringRegExp($sInExt, "\A(" & $sType & ")\z", 0) Then Return SetError(2, 0, 0)

    [/autoit] [autoit][/autoit] [autoit]

    ;OutFile path, to use later on.
    $sOP = StringLeft($sOutImage, StringInStr($sOutImage, "\", 0, -1))
    If Not FileExists($sOP) Then Return SetError(3, 0, 0)

    [/autoit] [autoit][/autoit] [autoit]

    ;OutFile name, to use later on.
    $sOF = StringTrimLeft($sOutImage, StringInStr($sOutImage, "\", 0, -1))

    [/autoit] [autoit][/autoit] [autoit]

    ;OutFile extension , to use for the encoder later on.
    $Ext = StringUpper(StringTrimLeft($sOutImage, StringInStr($sOutImage, ".", 0, -1)))
    If Not StringRegExp($Ext, "\A(" & $sType & ")\z", 0) Or $Ext = "ICO" Then Return SetError(4, 0, 0)

    [/autoit] [autoit][/autoit] [autoit]

    If Not IsInt($iW) And Not IsInt($iH) Then Return SetError(5, 0, 0)

    [/autoit] [autoit][/autoit] [autoit]

    ; WinAPI to create blank bitmap at the width and height to put your resized image on.
    $hBitmap = _WinAPI_CreateBitmap($iW, $iH, 1, 32)

    [/autoit] [autoit][/autoit] [autoit]

    ;Start GDIPlus
    _GDIPlus_Startup()

    [/autoit] [autoit][/autoit] [autoit]

    ;Get the handle of blank bitmap you created above as an image
    $hImage1 = _GDIPlus_BitmapCreateFromHBITMAP($hBitmap)

    [/autoit] [autoit][/autoit] [autoit]

    ;Load the image you want to resize.
    $hImage2 = _GDIPlus_ImageLoadFromFile($sInImage)

    [/autoit] [autoit][/autoit] [autoit]

    ;Get the graphic context of the blank bitmap
    $hGraphic = _GDIPlus_ImageGetGraphicsContext($hImage1)

    [/autoit] [autoit][/autoit] [autoit]

    ;Draw the loaded image onto the blank bitmap at the size you want
    _GDIPlus_GraphicsDrawImageRect($hGraphic, $hImage2, 0, 0, $iW, $iH)

    [/autoit] [autoit][/autoit] [autoit]

    ;Get the encoder of to save the resized image in the format you want.
    $CLSID = _GDIPlus_EncodersGetCLSID($Ext)

    [/autoit] [autoit][/autoit] [autoit]

    ;Generate a number for out file that doesn't already exist, so you don't overwrite an existing image.
    Do
    $i += 1
    Until (Not FileExists($sOP & $i & "_" & $sOF))

    [/autoit] [autoit][/autoit] [autoit]

    ;Prefix the number to the begining of the output filename
    $sOutImage = $sOP & $i & "_" & $sOF

    [/autoit] [autoit][/autoit] [autoit]

    ;Save the new resized image.
    _GDIPlus_ImageSaveToFileEx($hImage1, $sOutImage, $CLSID)

    [/autoit] [autoit][/autoit] [autoit]

    ;Clean up and shutdown GDIPlus.
    _GDIPlus_ImageDispose($hImage1)
    _GDIPlus_ImageDispose($hImage2)
    _GDIPlus_GraphicsDispose($hGraphic)
    _WinAPI_DeleteObject($hBitmap)
    _GDIPlus_Shutdown()
    Return SetError(0, 0, 1)
    EndFunc ;==>_ImageResize

    [/autoit]