-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathimageResizeToMaxDimension.cmd
More file actions
83 lines (68 loc) · 2.55 KB
/
imageResizeToMaxDimension.cmd
File metadata and controls
83 lines (68 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
@echo off
setlocal enabledelayedexpansion
echo ##################################################
echo # Description: Resize an image to a maximum dimension
echo # Usage: imageResizeToMaxDimension.cmd image_file.jpg 640
echo # imageResizeToMaxDimension.cmd /path/to/images/ 640 [custom args]
echo # Param 1: Image file or directory
echo # Param 2: Maximum width/height
echo # Param 3+ [Optional]: Custom args for ImageMagick
echo # Requires: Imagemagick
echo ##################################################
REM ################################################################################
REM # check parameters & set defaults
REM ################################################################################
REM Check first parameter (image file or directory)
IF "%~1"=="" (
echo Error: 1st arg must be an image file or directory
exit /b 1
)
set inputPath=%1
REM Check second parameter (max dimension)
IF "%~2"=="" (
echo Error: 2nd arg must be maximum size
exit /b 1
)
set maxDimension=%2
REM Get remaining args for ImageMagick
set imgArgs=
if "%3"=="" (
echo ### Using default resize settings
) else (
REM Use all arguments after the second one
set "allArgs=%*"
for /f "tokens=1,2,* delims= " %%a in ("!allArgs!") do set "imgArgs=%%c"
echo ### Using user-defined args: !imgArgs!
)
REM ################################################################################
REM Check if path is a directory or a file
REM ################################################################################
if exist "%inputPath%\" (
REM Process directory
echo.
echo Processing all image files in directory: %inputPath%
REM Process all image files in the directory
for %%F in ("%inputPath%\*.jpg" "%inputPath%\*.jpeg" "%inputPath%\*.png" "%inputPath%\*.gif" "%inputPath%\*.bmp" "%inputPath%\*.tif" "%inputPath%\*.tiff") do (
echo.
echo Processing: %%F
CALL :ProcessFile "%%F" "%maxDimension%" "%imgArgs%"
)
echo.
echo Finished resizing all images in directory: %inputPath%
) else (
REM Process single file
CALL :ProcessFile "%inputPath%" "%maxDimension%" "%imgArgs%"
)
exit /b 0
:ProcessFile
REM Process a single image file
set "filename=%~1"
set "dimension=%~2"
set "extraArgs=%~3"
set "outputFile=%~dpn1_resized_%dimension%%~x1"
echo Resizing image: %filename% to max dimension: %dimension%
REM Do conversion
magick "%filename%" -resize "%dimension%x%dimension%" %extraArgs% "%outputFile%"
echo Success: Image resized to max dimension of: %dimension%
echo %outputFile%
exit /b 0