-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmake-thumbs.ps1
More file actions
60 lines (54 loc) · 2.08 KB
/
Copy pathmake-thumbs.ps1
File metadata and controls
60 lines (54 loc) · 2.08 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
param(
[string]$FullDir = "assets/awards/full",
[string]$ThumbDir = "assets/awards/thumbs",
[int]$Height = 240,
[long]$Quality = 85,
[switch]$Force
)
$ErrorActionPreference = "Stop"
Add-Type -AssemblyName System.Drawing
if (-not (Test-Path -LiteralPath $FullDir)) {
throw "Full image directory not found: $FullDir"
}
if (-not (Test-Path -LiteralPath $ThumbDir)) {
New-Item -ItemType Directory -Path $ThumbDir | Out-Null
}
$codec = [System.Drawing.Imaging.ImageCodecInfo]::GetImageEncoders() |
Where-Object { $_.MimeType -eq "image/jpeg" }
$encoder = [System.Drawing.Imaging.Encoder]::Quality
$params = New-Object System.Drawing.Imaging.EncoderParameters(1)
$params.Param[0] = New-Object System.Drawing.Imaging.EncoderParameter($encoder, $Quality)
Get-ChildItem -LiteralPath $FullDir -File |
Where-Object { $_.Extension -match '^\.(jpg|jpeg|png)$' } |
ForEach-Object {
$baseName = $_.BaseName
if ($baseName.EndsWith("-full")) {
$thumbName = $baseName.Substring(0, $baseName.Length - 5) + "-thumb.jpg"
} else {
$thumbName = $baseName + "-thumb.jpg"
}
$thumbPath = Join-Path $ThumbDir $thumbName
if ((Test-Path -LiteralPath $thumbPath) -and -not $Force) {
Write-Output "skip $thumbPath"
return
}
$image = [System.Drawing.Image]::FromFile($_.FullName)
try {
$width = [int][Math]::Round($image.Width * $Height / $image.Height)
$bitmap = New-Object System.Drawing.Bitmap($width, $Height)
$graphics = [System.Drawing.Graphics]::FromImage($bitmap)
try {
$graphics.InterpolationMode = [System.Drawing.Drawing2D.InterpolationMode]::HighQualityBicubic
$graphics.SmoothingMode = [System.Drawing.Drawing2D.SmoothingMode]::HighQuality
$graphics.PixelOffsetMode = [System.Drawing.Drawing2D.PixelOffsetMode]::HighQuality
$graphics.DrawImage($image, 0, 0, $width, $Height)
$bitmap.Save($thumbPath, $codec, $params)
Write-Output "wrote $thumbPath ${width}x${Height}"
} finally {
$graphics.Dispose()
$bitmap.Dispose()
}
} finally {
$image.Dispose()
}
}