Skip to content

Commit 8980e9d

Browse files
committed
[Resize-Image] Signature change
- now accept bytes and stream
1 parent 545879f commit 8980e9d

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

Photo.Shell/Public/Resize-Image.ps1

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ function Resize-Image {
22
[CmdletBinding()]
33
param (
44
[Parameter(Mandatory = $true)]
5-
[byte[]]$Image,
5+
$Image,
66
[Parameter(Mandatory = $false, ParameterSetName = "Sizes")]
77
[switch]$ProportionalResize = $false,
88
[Parameter(Mandatory = $true, ParameterSetName = "Sizes")]
@@ -20,7 +20,13 @@ function Resize-Image {
2020
process {
2121
Write-Verbose "Cmdlet Resize-Image - Process"
2222

23-
$stream = [System.IO.MemoryStream]::new($Image)
23+
if ($Image.PSTypeNames -contains [System.Byte[]].ToString()) {
24+
$stream = [System.IO.MemoryStream]::new($Image)
25+
}
26+
else {
27+
$stream = $Image
28+
}
29+
2430
$img = [System.Drawing.Image]::FromStream($stream)
2531

2632
if ($PSCmdlet.ParameterSetName -eq 'Sizes') {

Tests/Photo.Shell.Tests.ps1

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ function Get-TestImage {
1414

1515
$result = [System.IO.MemoryStream]::new()
1616
$bitmap.Save($result, $Format)
17-
$result.ToArray()
17+
Write-Output $result.ToArray() -NoEnumerate
1818
}
1919

2020
function Convert-BytesToImage {
@@ -55,7 +55,6 @@ Describe 'Photo.Shell.Tests' {
5555
Context "Resize-Image" {
5656
$testImage = Get-TestImage
5757

58-
5958
It "should resize and keep ratio - smaller" {
6059
# 1000 x 800
6160
$img_before = Convert-BytesToImage $testImage
@@ -93,5 +92,20 @@ Describe 'Photo.Shell.Tests' {
9392
$img_after.Width | Should -BeExactly 200
9493
$img_after.Height | Should -BeExactly 160
9594
}
95+
96+
It "should accept" {
97+
$stream = [System.IO.MemoryStream]::new($testImage)
98+
{ Resize-Image -Image $stream -Ratio 1 } | Should -Not -Throw
99+
{ Resize-Image -Image $stream -Ratio 1 } | Should -Not -BeNullOrEmpty
100+
101+
{ Resize-Image -Image $testImage -Ratio 1 } | Should -Not -Throw
102+
{ Resize-Image -Image $testImage -Ratio 1 } | Should -Not -BeNullOrEmpty
103+
}
104+
105+
It "shouldn't accept" {
106+
$stream = [System.IO.MemoryStream]::new($testImage)
107+
$img = [System.Drawing.Image]::FromStream($stream)
108+
{ Resize-Image -Image $img -Ratio 1 } | Should -Throw
109+
}
96110
}
97111
}

0 commit comments

Comments
 (0)