Skip to content

Commit 198c5e9

Browse files
StartAutomatingStartAutomating
authored andcommitted
feat: Turtle.get_JPEG ( Fixes PoshWeb#60 )
1 parent 08c1d0f commit 198c5e9

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

Turtle.types.ps1xml

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -856,6 +856,57 @@ if ($VerbosePreference -ne 'SilentlyContinue') {
856856
}
857857
</SetScriptBlock>
858858
</ScriptProperty>
859+
<ScriptProperty>
860+
<Name>JPEG</Name>
861+
<GetScriptBlock>
862+
$chromiumNames = 'chromium','chrome','msedge'
863+
foreach ($browserName in $chromiumNames) {
864+
$chromiumCommand =
865+
$ExecutionContext.SessionState.InvokeCommand.GetCommand($browserName,'Application')
866+
if (-not $chromiumCommand) {
867+
$chromiumCommand =
868+
Get-Process -Name $browserName -ErrorAction Ignore |
869+
Select-Object -First 1 -ExpandProperty Path
870+
}
871+
if ($chromiumCommand) { break }
872+
}
873+
if (-not $chromiumCommand) {
874+
Write-Error "No Chromium-based browser found. Please install one of: $($chromiumNames -join ', ')"
875+
return
876+
}
877+
878+
$pngRasterizer = $this.Canvas -replace '/\*Insert-Post-Processing-Here\*/', @'
879+
const dataUrl = await canvas.toDataURL('image/jpeg')
880+
console.log(dataUrl)
881+
882+
const newImage = document.createElement('img')
883+
newImage.src = dataUrl
884+
document.body.appendChild(newImage)
885+
'@
886+
887+
888+
$appDataRoot = [Environment]::GetFolderPath("ApplicationData")
889+
$appDataPath = Join-Path $appDataRoot 'Turtle'
890+
$filePath = Join-Path $appDataPath 'Turtle.raster.html'
891+
$null = New-Item -ItemType File -Force -Path $filePath -Value (
892+
$pngRasterizer -join [Environment]::NewLine
893+
)
894+
# $pngRasterizer &gt; $filePath
895+
896+
$headlessArguments = @(
897+
'--headless', # run in headless mode
898+
'--dump-dom', # dump the DOM to stdout
899+
'--disable-gpu', # disable GPU acceleration
900+
'--no-sandbox' # disable the sandbox if running in CI/CD
901+
)
902+
903+
$chromeOutput = &amp; $chromiumCommand @headlessArguments "$filePath" | Out-String
904+
if ($chromeOutput -match '&lt;img\ssrc="data:image/\w+;base64,(?&lt;b64&gt;[^"]+)') {
905+
,[Convert]::FromBase64String($matches.b64)
906+
}
907+
908+
</GetScriptBlock>
909+
</ScriptProperty>
859910
<ScriptProperty>
860911
<Name>Mask</Name>
861912
<GetScriptBlock>

0 commit comments

Comments
 (0)