Skip to content

Commit 01aabe8

Browse files
committed
Added ConvertTo-Jpg
1 parent 8563e63 commit 01aabe8

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

media/ConvertTo-Jpg.ps1

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Try uncommenting the following line if you receive errors about a missing assembly
2+
# [void][System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
3+
function ConvertTo-Jpg {
4+
<#
5+
.SYNOPSIS
6+
Converts input to an JGP image with given compression level
7+
8+
.DESCRIPTION
9+
Converts input file to an JPG image. Compression level can be specified
10+
11+
.PARAMETER Files
12+
Input files
13+
14+
.PARAMETER Quality
15+
JPEG quality level 0 - 100 (inclusive bounds)
16+
17+
.EXAMPLE
18+
Get-ChildItem . -Filter *.gif | ConvertTo-Jpg -Quality 95
19+
Creates JPG images for all gifs from current location with quality set to 95
20+
21+
.EXAMPLE
22+
Get-Item .\1.gif | ConvertTo-Jpg -Quality 60
23+
Creates JPG image from '1.gif' file with quality set to 60
24+
25+
#>
26+
27+
[CmdletBinding()]
28+
param(
29+
[Parameter(Mandatory = $false, Position = 0, ValueFromPipeline = $true)]
30+
$Files,
31+
[int]
32+
[Parameter(Mandatory = $false, Position = 1)]
33+
$Quality = 100
34+
)
35+
36+
begin {
37+
Write-Verbose "Conversion start"
38+
$qualityEncoder = [System.Drawing.Imaging.Encoder]::Quality
39+
$encoderParams = New-Object System.Drawing.Imaging.EncoderParameters(1)
40+
# Set JPEG quality level here: 0 - 100 (inclusive bounds)
41+
$encoderParams.Param[0] = New-Object System.Drawing.Imaging.EncoderParameter($qualityEncoder, $Quality)
42+
$jpegCodecInfo = [System.Drawing.Imaging.ImageCodecInfo]::GetImageEncoders() | where { $_.MimeType -eq 'image/jpeg' }
43+
}
44+
process {
45+
$Files | % {
46+
$fullName = $_.Fullname
47+
$image = [System.Drawing.Image]::FromFile($fullName)
48+
$filePath = "{0}\{1}.jpg" -f $($_.DirectoryName), $($_.BaseName)
49+
$image.Save($filePath, $jpegCodecInfo, $encoderParams)
50+
$image.Dispose()
51+
}
52+
}
53+
end {
54+
Write-Verbose "Conversion done"
55+
}
56+
}

0 commit comments

Comments
 (0)