Skip to content

Commit 545879f

Browse files
committed
Added Resize-Image
1 parent ff653a6 commit 545879f

File tree

3 files changed

+105
-0
lines changed

3 files changed

+105
-0
lines changed

Photo.Shell/Public/Get-ImageCodecInfo.ps1

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# Table of file signatures (aka "magic numbers")
2+
# https://www.garykessler.net/library/file_sigs.html
13
function Get-ImageCodecInfo {
24
[CmdletBinding()]
35
param (
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
function Resize-Image {
2+
[CmdletBinding()]
3+
param (
4+
[Parameter(Mandatory = $true)]
5+
[byte[]]$Image,
6+
[Parameter(Mandatory = $false, ParameterSetName = "Sizes")]
7+
[switch]$ProportionalResize = $false,
8+
[Parameter(Mandatory = $true, ParameterSetName = "Sizes")]
9+
[int32]$Width,
10+
[Parameter(Mandatory = $true, ParameterSetName = "Sizes")]
11+
[int32]$Height,
12+
[Parameter(Mandatory = $true, ParameterSetName = "Ratio")]
13+
[decimal]$Ratio = 0.5
14+
)
15+
16+
begin {
17+
Write-Verbose "Cmdlet Resize-Image - Begin"
18+
}
19+
20+
process {
21+
Write-Verbose "Cmdlet Resize-Image - Process"
22+
23+
$stream = [System.IO.MemoryStream]::new($Image)
24+
$img = [System.Drawing.Image]::FromStream($stream)
25+
26+
if ($PSCmdlet.ParameterSetName -eq 'Sizes') {
27+
$ratioX = $Width / $img.Width;
28+
$ratioY = $Height / $img.Height;
29+
$ratio = [System.Math]::Min($ratioX, $ratioY);
30+
}
31+
32+
$autoRatio = $PSCmdlet.ParameterSetName -eq 'Ratio'
33+
[int32]$newWidth = if ($ProportionalResize -or $autoRatio) { $img.Width * $ratio } Else { $Width }
34+
[int32]$newHeight = if ($ProportionalResize -or $autoRatio) { $img.Height * $ratio } Else { $Height }
35+
36+
$destImage = New-Object System.Drawing.Bitmap($newWidth, $newHeight)
37+
38+
$graphics = [System.Drawing.Graphics]::FromImage($destImage)
39+
$graphics.DrawImage($img, 0, 0, $newWidth, $newHeight)
40+
$graphics.Dispose()
41+
42+
$stream2 = [System.IO.MemoryStream]::new()
43+
$destImage.Save($stream2, [System.Drawing.Imaging.ImageFormat]::Png)
44+
$stream2
45+
}
46+
47+
end {
48+
Write-Verbose "Cmdlet Resize-Image - End"
49+
}
50+
}

Tests/Photo.Shell.Tests.ps1

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@ function Get-TestImage {
1717
$result.ToArray()
1818
}
1919

20+
function Convert-BytesToImage {
21+
param (
22+
[byte[]]$testImage
23+
)
24+
25+
$stream = [System.IO.MemoryStream]::new($testImage)
26+
[System.Drawing.Image]::FromStream($stream)
27+
28+
}
29+
2030
Describe 'Photo.Shell.Tests' {
2131
Context "Get-ImageCodecInfo" {
2232
$testImage = Get-TestImage
@@ -41,4 +51,47 @@ Describe 'Photo.Shell.Tests' {
4151
Get-ImageCodecInfo $img | Select-Object -ExpandProperty MimeType | Should -BeExactly $Expected
4252
}
4353
}
54+
55+
Context "Resize-Image" {
56+
$testImage = Get-TestImage
57+
58+
59+
It "should resize and keep ratio - smaller" {
60+
# 1000 x 800
61+
$img_before = Convert-BytesToImage $testImage
62+
63+
$img_after = Resize-Image -Image $testImage -Ratio 0.1 #10%
64+
$img_after = [System.Drawing.Image]::FromStream($img_after)
65+
66+
$img_after.Width | Should -BeExactly ($img_before.Width * 0.1)
67+
$img_after.Height | Should -BeExactly ($img_before.Height * 0.1)
68+
}
69+
70+
It "should resize and keep ratio - bigger" {
71+
# 1000 x 800
72+
$img_before = Convert-BytesToImage $testImage
73+
74+
$img_after = Resize-Image -Image $testImage -Ratio 1.2 #120%
75+
$img_after = [System.Drawing.Image]::FromStream($img_after)
76+
77+
$img_after.Width | Should -BeExactly ($img_before.Width * 1.2)
78+
$img_after.Height | Should -BeExactly ($img_before.Height * 1.2)
79+
}
80+
81+
It "should resize to a given size" {
82+
$img_after = Resize-Image -Image $testImage -Width 200 -Height 200
83+
$img_after = [System.Drawing.Image]::FromStream($img_after)
84+
85+
$img_after.Width | Should -BeExactly 200
86+
$img_after.Height | Should -BeExactly 200
87+
}
88+
89+
It "should resize to a given size with ratio" {
90+
$img_after = Resize-Image -Image $testImage -Width 200 -Height 200 -ProportionalResize
91+
$img_after = [System.Drawing.Image]::FromStream($img_after)
92+
93+
$img_after.Width | Should -BeExactly 200
94+
$img_after.Height | Should -BeExactly 160
95+
}
96+
}
4497
}

0 commit comments

Comments
 (0)