Jump to content



Photo

Image fixed size crop


  • Please log in to reply
3 replies to this topic

#1 AndyMunTM

AndyMunTM

    Seeker

  • New Members
  • 2 posts

Posted 11 June 2011 - 08:39 PM

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.





#2 picea892

picea892

    Universalist

  • Active Members
  • PipPipPipPipPipPip
  • 689 posts

Posted 11 June 2011 - 10:57 PM

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

Attached Files



#3 AndyMunTM

AndyMunTM

    Seeker

  • New Members
  • 2 posts

Posted 12 June 2011 - 07:51 AM

thanks that was exactly what i was looking for!

#4 UEZ

UEZ

    Never say never

  • MVPs
  • 3,606 posts

Posted 12 June 2011 - 07:34 PM

Here my version:


AutoIt         
#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, 03 September 2011 - 02:58 PM.

 
The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users