-
-
Save crazy4groovy/6454481 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <GUIConstantsEx.au3> | |
#include <WindowsConstants.au3> | |
#include <ButtonConstants.au3> | |
#include <StaticConstants.au3> | |
#include <GDIPlus.au3> | |
#include <File.au3> | |
#include <Array.au3> | |
; Creating GUI and controls GUICreate ( "title" [, width [, height [, left [, top [, style [, exStyle [, parent]]]]]]] ) | |
$hGui = GUICreate("Resize JPEG", 320, 130, @DesktopWidth / 2 - 192, _ | |
@DesktopHeight / 2 - 235, -1, $WS_EX_ACCEPTFILES) | |
GUICtrlCreateLabel("Folder: ", 10, 15) ; File | |
GUICtrlCreateLabel("Width: ", 10, 45) ; Size | |
GUICtrlCreateLabel("Height will be auto adjusted", 10, 70) ; Size | |
$hFile = GUICtrlCreateInput(@MyDocumentsDir &"", 45, 15, 230, 16, -1, $WS_EX_STATICEDGE) | |
$hFileSel = GUICtrlCreateButton("...", 280, 14, 26, 18) | |
$hSel=GUICtrlCreateCombo("600", 45, 40,80) ; create first item | |
GUICtrlSetData($hSel, "700|800|900", "800") | |
GUICtrlCreateLabel("px ", 130, 40) ; Size | |
$hOk = GUICtrlCreateButton("Ok", 250, 90, 60, 24, $BS_FLAT) | |
GUISetState() | |
While 1 | |
$iMsg = GUIGetMsg() | |
Select | |
Case $iMsg = $hFileSel | |
$sTmpFile = FileSelectFolder("Select Folder:","") | |
If @error Then ContinueLoop | |
GUICtrlSetData($hFile, $sTmpFile); GUI will be updated at next iteration | |
Case $iMsg = $hOk | |
$work_folder = GUICtrlRead($hFile) | |
$pic_width = GUICtrlRead($hSel) | |
processResize($work_folder,$pic_width) | |
Case $iMsg = $GUI_EVENT_CLOSE | |
Exit | |
EndSelect | |
WEnd | |
Func processResize($folder,$width) | |
If $width <0 then | |
MsgBox(4096,"", "Invalid width") | |
return -1 | |
EndIf | |
;MsgBox(4096,"", $width) | |
FileChangeDir($folder) | |
; Declare array | |
Dim $Images[1] | |
; Gets all JPG files in the current directory ($folder). | |
Local $search = FileFindFirstFile("*.jpg") | |
; Check if the search was successful | |
If $search = -1 Then | |
MsgBox(0, "Error", "No JPG files could be found.") | |
return -1 | |
EndIf | |
; Resize array | |
While 1 | |
If IsArray($Images) Then | |
Local $Bound = UBound($Images) | |
ReDim $Images[$Bound+1] | |
EndIf | |
$Images[$Bound] = FileFindNextFile($search) | |
If @error Then ExitLoop | |
WEnd | |
; Close the search handle | |
FileClose($search) | |
; Create directory "resized" if not there yet | |
If NOT FileExists($folder & "\resized\") Then | |
DirCreate($folder & "\resized\") | |
EndIf | |
Dim $count=0 | |
; Loop for JPGs - gets dimension of JPG and calls resize function to resize to 50% width and 50% height | |
For $i = 1 to Ubound($Images)-1 | |
If $Images[$i] <> "" AND FileExists($folder & "\" & $Images[$i]) Then | |
Local $ImagePath = $folder & "\" & $Images[$i] | |
_GDIPlus_Startup() | |
Local $hImage = _GDIPlus_ImageLoadFromFile($ImagePath) | |
Local $ImageWidth = _GDIPlus_ImageGetWidth($hImage) | |
Local $ImageHeight = _GDIPlus_ImageGetHeight($hImage) | |
_GDIPlus_ImageDispose($hImage) | |
_GDIPlus_Shutdown() | |
;MsgBox(0,"DEBUG", $ImageWidth & " x " & $ImageHeight) | |
If $ImageWidth > $width then | |
Local $NewImageHeight = ($width/$ImageWidth) * $ImageHeight | |
;MsgBox(0,"DEBUG: " & $i,$ImagePath & " height:"& $ImageHeight & "new height:"& $NewImageHeight) | |
_ImageResize($ImagePath, $folder & "\resized\" & $Images[$i], $width, $NewImageHeight) | |
$count+=1 | |
EndIf | |
EndIf | |
Next | |
MsgBox(0, "Msg", $count & " files resized.") | |
EndFunc | |
;below code are copy from http://www.autoitscript.com/forum/topic/75022-image-resize-how/ | |
Func _ImageResize($sInImage, $sOutImage, $iW, $iH) | |
;MsgBox(0,"DEBUG: ",$sInImage&"-"& $sOutImage&"-"& $iW&"-"&$iH) | |
Local $hWnd, $hDC, $hBMP, $hImage1, $hImage2, $hGraphic, $CLSID, $i = 0 | |
;OutFile path, to use later on. | |
Local $sOP = StringLeft($sOutImage, StringInStr($sOutImage, "\", 0, -1)) | |
;OutFile name, to use later on. | |
Local $sOF = StringMid($sOutImage, StringInStr($sOutImage, "\", 0, -1) + 1) | |
;OutFile extension , to use for the encoder later on. | |
Local $Ext = StringUpper(StringMid($sOutImage, StringInStr($sOutImage, ".", 0, -1) + 1)) | |
; Win api to create blank bitmap at the width and height to put your resized image on. | |
$hWnd = _WinAPI_GetDesktopWindow() | |
$hDC = _WinAPI_GetDC($hWnd) | |
$hBMP = _WinAPI_CreateCompatibleBitmap($hDC, $iW, $iH) | |
_WinAPI_ReleaseDC($hWnd, $hDC) | |
;Start GDIPlus | |
_GDIPlus_Startup() | |
;Get the handle of blank bitmap you created above as an image | |
$hImage1 = _GDIPlus_BitmapCreateFromHBITMAP ($hBMP) | |
;Load the image you want to resize. | |
$hImage2 = _GDIPlus_ImageLoadFromFile($sInImage) | |
;Get the graphic context of the blank bitmap | |
$hGraphic = _GDIPlus_ImageGetGraphicsContext ($hImage1) | |
;Draw the loaded image onto the blank bitmap at the size you want | |
_GDIPLus_GraphicsDrawImageRect($hGraphic, $hImage2, 0, 0, $iW, $iH) | |
;Get the encoder of to save the resized image in the format you want. | |
$CLSID = _GDIPlus_EncodersGetCLSID($Ext) | |
;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)) | |
;Prefix the number to the begining of the output filename | |
$sOutImage = $sOP & $i & "_" & $sOF | |
;Save the new resized image. | |
_GDIPlus_ImageSaveToFileEx($hImage1, $sOutImage, $CLSID) | |
;Clean up and shutdown GDIPlus. | |
_GDIPlus_ImageDispose($hImage1) | |
_GDIPlus_ImageDispose($hImage2) | |
_GDIPlus_GraphicsDispose ($hGraphic) | |
_WinAPI_DeleteObject($hBMP) | |
_GDIPlus_Shutdown() | |
EndFunc |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment