Sign in to follow this  
Followers 0
AndyMunTM

Image fixed size crop

4 posts in this topic

Hello!

I wanna make a script that crops an image to a fixed size, let`s say 600px.

I`ve already done that in php (have it installed on my pc), but i`m looking for a simpler way with AutoIt and i understood that you must use Gdi+.

To code that it takes a while and it`s not verry fast, and it should be because i need it to crop 30+ images at once, so i found something like this:

$hImage = DllCall($ghGDIPDll, 'int', 'GdipGetImageThumbnail', 'ptr', $hPic, 'int', $Size[0], 'int', $Size[1], 'ptr*', 0, 'ptr', 0, 'ptr', 0)

As an alternative, i could use something that makes thumbnails with specific size.

I ask someone to explain what parameters should i fill in, and where can i find documentation about functions inside a dll.

Thanks.

*Sorry for my bad english, but it`s not my native language.

Share this post


Link to post
Share on other sites



Hi AndyMunTM

First post so I'll forgive you that you didn't really try prior to asking for help. In the future you will get better results on this forum if you provide more than 1 line of code.

Here this is what you are looking for. Makes all pictures in the same directory 500 pixels by something depending on if landscape or portrait. Funny enough....I used it to change images for my webpages until my PHP got good enough :huh2: Now I have it automated using PHP

Picea892

resizer.au3

Share this post


Link to post
Share on other sites

thanks that was exactly what i was looking for!

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Here my version:

#include <File.au3>
#include <GDIPlus.au3>

_GDIPlus_Startup()

Global $folder = FileSelectFolder("Select folder with pictures", "", 4) & "\"
Global $outfolder = @ScriptDir & "\Resized"
If @error Then Exit MsgBox(0, "Information", "Nothing selected - closing program", 10)
Global $aFiles = _FileListToArray($folder, "*.???", 0)
For $i = 1 To $aFiles[0]
    If StringRegExp($aFiles[$i], "(?i).*\.png|.*\.jpg|.*\.bmp", 0) Then ResizeImage($folder & $aFiles[$i], 32, 32, $outfolder, -1)
Next
_GDIPlus_Shutdown()
ShellExecute($outfolder)
Exit

;======================================================================================
; Function Name:        ResizeImage
; Description:          Loads an image and scales it to desired width or height
;
; Parameters:           $fImage:                image file to be loaded
;                               $iW:                    new image width. If $iW = 0 then default values is 96
;                               $iH:                        new image height. If $iH = 0 then default values is 96
;                               $fDestFolder:       destination folder where the scaled image should be saved to
;                               $fExt:                  image output format (can be jpg, png, bmp, gif, tiff) - default is PNG, -1 to keep extension
;                               $fPart:                 filename part to separate it from orignal name. Default is .resized. -> filename.resized.ext
;
; Requirement(s):       GDIPlus.au3
; Return Value(s):  Success: True, Error: see below
; Error codes:          1: no filename given
;                               2:  filename doesn't exist
;                               3: image couldn't be resized
;                               4: resized image couldn't be saved
;
; Author(s):                UEZ
; Version:                  v0.80 Build 2011-08-29 Beta
;=======================================================================================
Func ResizeImage($fImage, $iW, $iH, $fDestFolder, $fExt = "png", $fPart = ".resized.")
    If $fImage = "" Then Return SetError(1, 0, 0)
    If Not FileExists($fImage) Then Return SetError(2, 0, 0)
    Local $iOutputFormats = "jpg,png,bmp,gif,tif"
    If Not StringInStr($iOutputFormats, $fExt)  Then
        If $fExt <> -1 Then
            $fExt = "png"
        Else
            If Not StringInStr($iOutputFormats, StringRight($fImage, 3)) Then
                $fExt = "png"
            Else
                $fExt = StringRight($fImage, 3)
            EndIf
        EndIf
    EndIf
    Local $fName = StringRegExpReplace($fImage, ".*\\(.*).{4}", "$1")
    Local $declared = True
    If Not $ghGDIPDll Then
        _GDIPlus_Startup()
        $declared = False
    EndIf
    Local $hImageFromFile = _GDIPlus_ImageLoadFromFile($fImage)
    Local $iWidth = _GDIPlus_ImageGetWidth($hImageFromFile)
    Local $iHeight = _GDIPlus_ImageGetHeight($hImageFromFile)
    Local $x, $y
    If Not $iW And Not $iH Then
        $iW = 96
        $iH = 96
    ElseIf $iW And Not $iH Then
        $x = $iW / $iWidth
        $iH = Int($iHeight * $x)
    ElseIf Not $iW And $iH Then
        $y = $iH / $iHeight
        $iW = Int($iWidth * $y)
    EndIf
    Local $hImageThumbnail = DllCall($ghGDIPDll, "uint", "GdipGetImageThumbnail", "handle", $hImageFromFile, "uint", $iW, "uint", $iH, "int*", 0, "ptr", 0, "ptr", 0)
    If @error Then
        _GDIPlus_ImageDispose($hImageFromFile)
        If Not $declared Then _GDIPlus_Shutdown()
        Return SetError(3, 0, 0)
    EndIf
    $hImageThumbnail = $hImageThumbnail[4]
    If Not FileExists($fDestFolder) Then DirCreate($fDestFolder)
    If $fPart = "" Then $fPart = "."
    If Not _GDIPlus_ImageSaveToFile($hImageThumbnail, $fDestFolder & "\" & $fName & $fPart & $fExt) Then
        _GDIPlus_ImageDispose($hImageFromFile)
        _GDIPlus_ImageDispose($hImageThumbnail)
        If Not $declared Then _GDIPlus_Shutdown()
        Return SetError(4, 0, 0)
    EndIf
    _GDIPlus_ImageDispose($hImageFromFile)
    _GDIPlus_ImageDispose($hImageThumbnail)
    If Not $declared Then _GDIPlus_Shutdown()
    Return True
EndFunc

Br,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0