Code : Tout sélectionner
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <GDIPlus.au3>
#include <WinAPI.au3>
#Include <File.au3>
#include <array.au3>
#Region ### START Koda GUI section ### Form=
$Frm_param = GUICreate("Paramètrage", 320, 330, 193, 125)
$Label1 = GUICtrlCreateLabel("Taille des Images après traitement :", 80, 8, 178, 17)
$Group1 = GUICtrlCreateGroup("Taille de l'image désirée après traitement :", 18, 32, 289, 97)
$IMP_imgred = GUICtrlCreateInput("", 120, 72, 73, 21)
$label3 = GUICtrlCreateLabel("ex : 600",195,75,73,21)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Group2 = GUICtrlCreateGroup("taille des mignatures après traitement : :", 18, 144, 289, 97)
$IMP_imgmin = GUICtrlCreateInput("", 121, 185, 73, 21)
$label3 = GUICtrlCreateLabel("ex : 250",195,188,73,21)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Label2 = GUICtrlCreateLabel("La taille demandé est en pixel et doit être des entiers"&@LF&"L'image sera redimentionner en gardant les proportions la taille indiquer étant la plus grande", 20, 260, 178, 97)
$Btnsource = GUICtrlCreateButton("repertoire source", 200, 256, 107, 25, 0)
$Btnquit = GUICtrlCreateButton("quitter", 200, 288, 107, 25, 0)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
Global $longeur,$largeur,$ratio,$file
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $Btnquit
Exit
Case $Btnsource
$dossier = FileSelectFolder("choisir un dossier ? (Merci de sélectionnez le dossier) ", " ")
if $dossier ="" Then
MsgBox(0,"Attention","Aucun dossier source n'a été selectionné")
else
$listeFichiers = _FileListToArray($dossier,"*.jpg",1)
if @error = 4 Then
MsgBox(0,"erreur","Aucune image trouvé dans le repertoire source")
ExitLoop
Else
$file = $dossier&"\"&$listeFichiers
For $i=1 to $listeFichiers[0]
TrayTip("Information","Traitement en cours pour l'image : "&$listeFichiers[$i],1)
$file = $dossier&"\"&$listeFichiers[$i]
_appel($file)
GUISetState(@SW_hide)
Next
EndIf
EndIf
Exit
EndSwitch
WEnd
Func _appel($file)
$t = FileGetTime($file , 1)
$chemin= @MyDocumentsDir&"\photos\"&$t[0]&$t[1]&$t[2]
DirCreate($chemin)
if @error = 1 Then
MsgBox(0,"erreur","impossible de créer le repertoire : " &$chemin)
Exit
EndIf
DirCreate($chemin&"\thumbnail")
if @error = 1 Then
MsgBox(0,"erreur","impossible de créer le repertoire :" &$chemin&"\thumbnail")
Exit
EndIf
$tableau = StringSplit($file,"\")
$x = $tableau[0]
$nomimage = $tableau[$x]
$aSize = _GetJpegSize($file)
if @error then Exit
$longeur = $aSize[0]
$largeur = $aSize[1]
$dim = GUICtrlRead($IMP_imgred)
_nouvellesdimention($dim)
_ImageResize($file, $chemin &"\"&$nomimage,$longeur, $largeur)
if @error = 1 Then
MsgBox(0,'Erreur',"Le fichier cible n'existe pas")
ElseIf @error = 2 Then
MsgBox(0,"Erreur","le format du fichier n'est pas valide")
ElseIf @error = 3 Then
MsgBox(0,"Erreur","Le fichier de sortie n'existe pas")
ElseIf @error = 4 Then
MsgBox(0,"Erreur","Le format du fichier de sortie n'est pas valide")
ElseIf @error = 5 Then
MsgBox(0,"Erreur","La taille indiqué pour le fichier sortie n'est pas valide")
EndIf
$dim = GUICtrlRead($IMP_imgmin)
_nouvellesdimention($dim)
_ImageResize($file, $chemin&"\thumbnail" &"\TN-"&$nomimage,$longeur, $largeur)
if @error = 1 Then
MsgBox(0,'Erreur',"Le fichier cible n'existe pas")
ElseIf @error = 2 Then
MsgBox(0,"Erreur","le format du fichier n'est pas valide")
ElseIf @error = 3 Then
MsgBox(0,"Erreur","Le fichier de sortie n'existe pas")
ElseIf @error = 4 Then
MsgBox(0,"Erreur","Le format du fichier de sortie n'est pas valide")
ElseIf @error = 5 Then
MsgBox(0,"Erreur","La taille indiqué pour le fichier sortie n'est pas valide")
EndIf
EndFunc
;******************************** _GetJpegSize *******************************
Func _GetJpegSize($file)
Local $size[2]
Local $fs, $pos = 3
if not (_FileReadAtOffsetHEX ($file, 1, 2) = "FFD8") then
SetError(1); Not Jpeg
Return("")
endif
$fs = FileGetSize($file)
While $pos < $fs
$data = _FileReadAtOffsetHEX ($file, $pos, 4)
if StringLeft($data, 2) = "FF" then; Valid segment start
if StringInStr("C0 C2 CA C1 C3 C5 C6 C7 C9 CB CD CE CF", StringMid($data, 3, 2)) then; Segment with size data
$seg = _FileReadAtOffsetHEX ($file, $pos+5, 4)
$size[1] = Dec(StringLeft($seg, 4))
$size[0] = Dec(StringRight($seg, 4))
Return($size)
else
$pos = $pos + Dec(StringRight($data, 4)) + 2
endif
else
exitloop
endif
Wend
SetError(2); Segment not found
Return("")
EndFunc
Func _FileReadAtOffsetHEX ($file, $offset, $bytes)
Local $tfile = FileOpen($file, 0)
Local $tstr = ""
FileRead($tfile, $offset-1)
For $i = $offset To $offset + $bytes - 1
$tstr = $tstr & Hex(Asc(FileRead($tfile, 1)), 2)
Next
FileClose($tfile)
Return ($tstr)
Endfunc
; #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"
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)
;OutFile path, to use later on.
$sOP = StringLeft($sOutImage, StringInStr($sOutImage, "\", 0, -1))
If Not FileExists($sOP) Then Return SetError(3, 0, 0)
;OutFile name, to use later on.
$sOF = StringTrimLeft($sOutImage, StringInStr($sOutImage, "\", 0, -1))
;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)
If Not IsInt($iW) And Not IsInt($iH) Then Return SetError(5, 0, 0)
$hBitmap = _WinAPI_CreateCompatibleBitmapSection($iW,$iH)
;Start GDIPlus
_GDIPlus_Startup()
;Get the handle of blank bitmap you created above as an image
$hImage1 = _GDIPlus_BitmapCreateFromHBITMAP($hBitmap)
;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 & $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($hBitmap)
_GDIPlus_Shutdown()
Return SetError(0, 0, 1)
EndFunc ;==>_ImageResize
Func _WinAPI_CreateCompatibleBitmapSection($iWidth,$iHeight,$iBitCount=24)
$tBMI = DllStructCreate($tagBITMAPINFO)
DllStructSetData($tBMI, "Size", DllStructGetSize($tBMI) - 4)
DllStructSetData($tBMI, "Width", $iWidth)
DllStructSetData($tBMI, "Height", $iHeight)
DllStructSetData($tBMI, "Planes", 1)
DllStructSetData($tBMI, "BitCount", $iBitCount)
$aDIB = DllCall('gdi32.dll', 'ptr', 'CreateDIBSection', _
'ptr', 0, _
'ptr', DllStructGetPtr($tBMI), _
'uint', 1, _
'ptr*', 0, _
'ptr', 0, _
'uint', 0)
Return $aDib[0]
EndFunc
func _nouvellesdimention($dim)
if $longeur > $largeur Then
$ratio = $largeur / $longeur
$longeur = $dim
$largeur = $dim * $ratio
Else
$ratio = $longeur / $largeur
$largeur = $dim
$longeur = $dim * $ratio
EndIf
$largeur =Int($largeur)
$longeur =Int($longeur)
EndFunc