Jump to content

Simple Image Resizer


Madza91
 Share

Recommended Posts

Hello! I created this for my own needs, and I meant maybe will be useful for someone, and I decide to post it :)

You can Resize your digital photo images in several easy steps!

1. Select the image you would like to resize with [browse...]

2. Select the destination where you would like to save it...

3. Select size for your image (Small, Medium, Large or Custom)

4. Click on Resize button and you're done!

Here's my little Simple Image Resizer:

Simple_Image_Resizer.au3

Updated:

Simple_Image_Resizer.au3

Edited by n3nE

[quote name='dbzfanatic' post='609696' date='Nov 26 2008, 08:46 AM']This is a help forum not a "write this for me" forum.[/quote](Sorry for bad English) :)

Link to comment
Share on other sites

Oh, sorry. There was mistake in Line 101.

I fixed it in updated version. Enjoy :)

[quote name='dbzfanatic' post='609696' date='Nov 26 2008, 08:46 AM']This is a help forum not a "write this for me" forum.[/quote](Sorry for bad English) :)

Link to comment
Share on other sites

thanx, will try right away ^^

ok, it works now, thanx a lot :lmao:

No problem, and thank you :)

[quote name='dbzfanatic' post='609696' date='Nov 26 2008, 08:46 AM']This is a help forum not a "write this for me" forum.[/quote](Sorry for bad English) :)

Link to comment
Share on other sites

This is a great little script. I was going to add one function to it but right now I don't have the time so maybe you want to take a look at it.

For the custom sizes.

A checkbox "Maintain aspect ratio"

and then a math function to get the aspect ratio.

If the user enters a value in width, height is calculated from the new function and that value placed in the height field.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

That's good idea! I will update it when i found a little of free time :)

Btw, I will see to add some Effects like Brightness Contrast, Crop image, Adding some text on image, Black/White effect... Choosing image quality, Rotating... :lmao:

[quote name='dbzfanatic' post='609696' date='Nov 26 2008, 08:46 AM']This is a help forum not a "write this for me" forum.[/quote](Sorry for bad English) :)

Link to comment
Share on other sites

  • 2 weeks later...

great work,

small questions:

1. can you add option process folder not only a single image ?

2. for folder process maybe add small input box to add some prefix to the target file name like tn_

:)) i will try to do it also, because i need this kind of SW to create thumbnails to my web gallery, i have lot of big panoramas and i cant use the hosting server to resize it in the fly on server.

also i will be good if we can specify in % custom image size :), i will try do add it now ^_^

regards

Link to comment
Share on other sites

  • 1 month later...

hi All,

I made some additions to a function within this code. I grabbed the code _ImageResize for a project I was working on and needed to added extra functionality. I figured I should post it back here. It should be plug and play in the other code since the additional function inputs are all optional.

I added the following:

- Aspect Ratio can be preserved

- User choice for whether image is stretched to fit requested dimensions

- User choice to check file extension against array of extensions.

It also incorporates a number of other checks eg. check $sinImage exists.

I have also listed some possible enhancements.

I am not sure of the etiquette here and please forgive me if I have overstepped any bounds.

Most of the code is comments to try and help others understand it (and maybe find errors in logic).

Let me know if you find any errors.

Enjoy

Jason

CODE
; #FUNCTION# ====================================================================================================

================

; Name...........: _ImageResize

; Description ...: Resizes image to values as given and saves to $sOutImage

; Syntax.........: _ImageResize($sInImage, $sOutImage, $iW = -1, $iH = -1, $bPreserveAR = True, $bStretch = False, $bUseExtensions = False)

; Parameters ....: $sInImage - Full path and filename of the image to be resized

; $sOutImage - Full path and filename of the file to save to

; $iW - Width in pixels (optional - Will default to $sInImage width)

; $iH - Height in pixels (optional - Will default to $sInImage height)

; $bPreserveAR - Boolean, True to preserve aspect ratio, False to not (optional - Default True)

; $bStretch - Boolean, True increases small images to fit, False does not (optional - Default False)

; $bUseExtensions - Boolean, Indicates whether to use the extensions list or not (optional - Default False)

; Return values .: Success - Returns a 1

; Failure - Returns a 0

; @Error - 0 = No error.

; |1 = Input file does not exist

; |2 = Not a recognized image format

; |3 = No resize performed

; Author ........: Original from n3nE

; Edited by Jason Webb (jasondexterwebb at gmail dot com)

; Modified.......: 04/06/2009

; Remarks .......: This function assumes that if a H and W are inputted then the user does not want an image bigger than either of

; those numbers. Aspect Ratio and Strectch are taken into account if required however the image will never be bigger

; in either dimension than what was requested.

; Related .......:

; Link ..........;

; Enhancements ..; add option to use a specify a ratio based resize eg. 0.5 would be half original size, 2.0 would be twice original etc.

; add more file extensions

; add ability to pass file extension array to function

; add check that $sOutImage can be written to

; ====================================================================================================

===========================

Func _ImageResize($sInImage, $sOutImage, $iW = -1, $iH = -1, $bPreserveAR = True, $bStretch = False, $bUseExtensions = False)

Local $hWnd, $hDC, $hBMP, $hImage1, $hImage2, $hGraphic, $CLSID, $i = 0

Local $sOF, $Ext, $vAspectR

Local $tPath, $tDir

Local $aImageExtensions

Dim $aImageExtensions[4] = ["jpg", "tif", "jpeg", "bmp", "png"]

_ArraySort($aImageExtensions)

;~ Process Defaults

If $iW = Default Then $iW = -1

If $iH = Default Then $iH = -1

;~ must use "==" below because False = Default resolves to TRUE..this causes errors in this code.

If $bPreserveAR == Default Then $bPreserveAR = True

If $bStretch == Default Then $bStretch = False

If $bUseExtensions == Default Then $bUseExtensions = False

;~ check input image exists

If Not FileExists($sInImage) Then Return SetError(1, 0, 0)

;~ extract image name and extension

$Ext = StringUpper(StringMid($sOutImage, StringInStr($sOutImage, ".", 0, -1) + 1))

$sOF = StringMid($sOutImage, StringInStr($sOutImage, "\", 0, -1) + 1)

;~ check file extension

If $bUseExtensions Then

If _ArrayBinarySearch($aImageExtensions, StringLower($Ext)) = -1 Then Return SetError(2, 0, 0)

EndIf

;~ start GDI plus to get aspect ratio

_GDIPlus_Startup()

$hImage2 = _GDIPlus_ImageLoadFromFile($sInImage)

;~ find current aspect ratio (using width / height)

$iActW = _GDIPlus_ImageGetWidth($hImage2)

$iActH = _GDIPlus_ImageGetHeight($hImage2)

$vAspectR = $iActW / $iActH

;~ populate values if defaults used. Use "<0" to capture incorrect size entries.

If $iH < 0 And $iW < 0 Then

$iH = $iActH

$iW = $iActW

ElseIf $iH < 0 Then

$iH = $iW / $vAspectR

ElseIf $iW < 0 Then

$iW = $iH * $vAspectR

EndIf

;~ process all options and combinations (36 in total)

If $bPreserveAR = True And (($iActW >= $iW Or $iActH >= $iH) Or ($iActW <= $iW And $iActH <= $iH And $bStretch = True)) Then

;~ captures the following cases (17)

;~ $iActW > $iW, $iActH > $iH, $bPreserveAR = True, $bStretch = True ; reduce to largest image that will fit in H and W with Aspect Ratio (see remarks)

;~ $iActW > $iW, $iActH > $iH, $bPreserveAR = True, $bStretch = False ; reduce to largest image that will fit in H and W with Aspect Ratio (see remarks)

;~ $iActW = $iW, $iActH > $iH, $bPreserveAR = True, $bStretch = True ; reduce to largest image that will fit in H and W with Aspect Ratio (see remarks)

;~ $iActW = $iW, $iActH > $iH, $bPreserveAR = True, $bStretch = False ; reduce to largest image that will fit in H and W with Aspect Ratio (see remarks)

;~ $iActW > $iW, $iActH = $iH, $bPreserveAR = True, $bStretch = True ; reduce to largest image that will fit in H and W with Aspect Ratio (see remarks)

;~ $iActW > $iW, $iActH = $iH, $bPreserveAR = True, $bStretch = False ; reduce to largest image that will fit in H and W with Aspect Ratio (see remarks)

;~ $iActW > $iW, $iActH < $iH, $bPreserveAR = True, $bStretch = True ; reduce W to max, calc H with Aspect Ratio

;~ $iActW > $iW, $iActH < $iH, $bPreserveAR = True, $bStretch = False ; reduce W to max, calc H with Aspect Ratio

;~ $iActW < $iW, $iActH > $iH, $bPreserveAR = True, $bStretch = True ; reduce H to max, calc W with Aspect Ratio

;~ $iActW < $iW, $iActH > $iH, $bPreserveAR = True, $bStretch = False ; reduce H to max, calc W with Aspect Ratio

;~ $iActW = $iW, $iActH < $iH, $bPreserveAR = True, $bStretch = True ; no resize due to Aspect Ratio constraint, $iW = $iActW and $iH = $iActH

;~ $iActW = $iW, $iActH < $iH, $bPreserveAR = True, $bStretch = False ; no resize due to Aspect Ratio constraint, $iW = $iActW and $iH = $iActH

;~ $iActW < $iW, $iActH = $iH, $bPreserveAR = True, $bStretch = True ; no resize due to Aspect Ratio constraint, $iW = $iActW and $iH = $iActH

;~ $iActW < $iW, $iActH = $iH, $bPreserveAR = True, $bStretch = False ; no resize due to Aspect Ratio constraint, $iW = $iActW and $iH = $iActH

;~ $iActW = $iW, $iActH = $iH, $bPreserveAR = True, $bStretch = True ; don't need to capture this here but it does anyway, no resize required

;~ $iActW = $iW, $iActH = $iH, $bPreserveAR = True, $bStretch = False ; don't need to capture this here but it does anyway, no resize required

;~ $iActW < $iW, $iActH < $iH, $bPreserveAR = True, $bStretch = True ; increase to largest image that will fit in H and W with Aspect Ratio (see remarks)

If $iH * $vAspectR <= $iW Then

$iW = $iH * $vAspectR

Else

$iH = $iW / $vAspectR

EndIf

ElseIf $bPreserveAR = False And (($bStretch = True And ($iActW <= $iW Or $iActH <= $iH)) Or ($iActW >= $iW And $iActH >= $iH)) Then

;~ for all cases below since aspect ratio is false the image will be resized to requested amount and potentially deformed

;~ no action required $iW = $iW and $iH = $iH

;~ captures the following cases (13)

;~ $iActW < $iW, $iActH < $iH, $bPreserveAR = False, $bStretch = True

;~ $iActW > $iW, $iActH > $iH, $bPreserveAR = False, $bStretch = True

;~ $iActW > $iW, $iActH > $iH, $bPreserveAR = False, $bStretch = False

;~ $iActW > $iW, $iActH < $iH, $bPreserveAR = False, $bStretch = True

;~ $iActW < $iW, $iActH > $iH, $bPreserveAR = False, $bStretch = True

;~ $iActW = $iW, $iActH < $iH, $bPreserveAR = False, $bStretch = True

;~ $iActW < $iW, $iActH = $iH, $bPreserveAR = False, $bStretch = True

;~ $iActW = $iW, $iActH = $iH, $bPreserveAR = False, $bStretch = False

;~ $iActW = $iW, $iActH = $iH, $bPreserveAR = False, $bStretch = True

;~ $iActW = $iW, $iActH > $iH, $bPreserveAR = False, $bStretch = True

;~ $iActW = $iW, $iActH > $iH, $bPreserveAR = False, $bStretch = False

;~ $iActW > $iW, $iActH = $iH, $bPreserveAR = False, $bStretch = True

;~ $iActW > $iW, $iActH = $iH, $bPreserveAR = False, $bStretch = False

ElseIf $bStretch = False And ($iActW <= $iW And $iActH <= $iH) Then

;~ all cases below no resize is available because stretch is false and the original has all dimensions smaller than or equal to the request.

;~ captures the following cases (4)

;~ $iActW < $iW, $iActH < $iH, $bPreserveAR = True, $bStretch = False

;~ $iActW < $iW, $iActH < $iH, $bPreserveAR = False, $bStretch = False

;~ $iActW < $iW, $iActH = $iH, $bPreserveAR = False, $bStretch = False

;~ $iActW = $iW, $iActH < $iH, $bPreserveAR = False, $bStretch = False

;~ these cases are captured earlier but also make the above true

;~ $iActW = $iW, $iActH < $iH, $bPreserveAR = True, $bStretch = False

;~ $iActW < $iW, $iActH = $iH, $bPreserveAR = True, $bStretch = False

;~ $iActW = $iW, $iActH = $iH, $bPreserveAR = True, $bStretch = False

$iH = $iActH

$iW = $iActW

ElseIf $bStretch = False And $bPreserveAR = False Then

;~ captures the following cases (2)

;~ $iActW < $iW, $iActH > $iH, $bPreserveAR = False, $bStretch = False ; resize to requested H and original W

;~ $iActW > $iW, $iActH < $iH, $bPreserveAR = False, $bStretch = False ; resize to requested W and original H

;~ these cases are captured earlier but also make the above true

;~ $iActW < $iW, $iActH < $iH, $bPreserveAR = False, $bStretch = False

;~ $iActW < $iW, $iActH = $iH, $bPreserveAR = False, $bStretch = False

;~ $iActW = $iW, $iActH < $iH, $bPreserveAR = False, $bStretch = False

;~ $iActW > $iW, $iActH > $iH, $bPreserveAR = False, $bStretch = False

;~ $iActW = $iW, $iActH = $iH, $bPreserveAR = False, $bStretch = False

;~ $iActW = $iW, $iActH > $iH, $bPreserveAR = False, $bStretch = False

;~ $iActW > $iW, $iActH = $iH, $bPreserveAR = False, $bStretch = False

If $iActW < $iW Then

$iW = $iActW

ElseIf $iActH < $iH Then

$iH = $iActH

EndIf

EndIf

If Not ($iW = $iActW And $iH = $iActH) Then

; resize picture

$hWnd = _WinAPI_GetDesktopWindow()

$hDC = _WinAPI_GetDC($hWnd)

$hBMP = _WinAPI_CreateCompatibleBitmap($hDC, $iW, $iH)

_WinAPI_ReleaseDC($hWnd, $hDC)

$hImage1 = _GDIPlus_BitmapCreateFromHBITMAP($hBMP)

$hGraphic = _GDIPlus_ImageGetGraphicsContext($hImage1)

_GDIPlus_GraphicsDrawImageRect($hGraphic, $hImage2, 0, 0, $iW, $iH)

$CLSID = _GDIPlus_EncodersGetCLSID($Ext)

_GDIPlus_ImageSaveToFileEx($hImage1, $sOutImage, $CLSID)

_GDIPlus_ImageDispose($hImage1)

_GDIPlus_ImageDispose($hImage2)

_GDIPlus_GraphicsDispose($hGraphic)

_WinAPI_DeleteObject($hBMP)

_GDIPlus_Shutdown()

Return SetError(0, 0, 1)

EndIf

_GDIPlus_Shutdown()

Return SetError(3, 0, 0)

EndFunc ;==>_ImageResize

Uploaded file also.

FUNC_ImageResize.au3

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...