Skip to content

Commit

Permalink
fix: modified handling svg files in ssi-sdk.core
Browse files Browse the repository at this point in the history
  • Loading branch information
sksadjad committed Feb 14, 2024
1 parent d7823eb commit c86188e
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 35 deletions.
2 changes: 1 addition & 1 deletion packages/ssi-sdk-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"cross-fetch": "^3.1.8",
"debug": "^4.3.4",
"image-size": "^2.0.0-beta.2",
"uint8arrays": "3.1.1"
"uint8arrays": "^3.1.1"
},
"devDependencies": {
"did-resolver": "^4.1.0"
Expand Down
20 changes: 20 additions & 0 deletions packages/ssi-sdk-core/src/utils/__tests__/image.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,26 @@ describe('@sphereon/ssi-sdk.core:image', () => {
expect(result?.width).toEqual(1901)
})

it('should return image dimensions for a 2-layered svg base64', async (): Promise<void> => {
const svg_base64 =
'PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiIHN0YW5kYWxvbmU9Im5vIj8+CjxzdmcKICAgeG1sbnM6ZGM9Imh0dHA6Ly9wdXJsLm9yZy9kYy9lbGVtZW50cy8xLjEvIgogICB4bWxuczpjYz0iaHR0cDovL2NyZWF0aXZlY29tbW9ucy5vcmcvbnMjIgogICB4bWxuczpyZGY9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkvMDIvMjItcmRmLXN5bnRheC1ucyMiCiAgIHhtbG5zOnN2Zz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciCiAgIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIKICAgdmVyc2lvbj0iMS4xIgogICBpZD0ic3ZnMjgwOTQiCiAgIHZpZXdCb3g9IjAgMCA3NDQuMDk0NDg4MTkgMTA1Mi4zNjIyMDQ3IgogICBoZWlnaHQ9IjI5N21tIgogICB3aWR0aD0iMjEwbW0iPgogIDxkZWZzCiAgICAgaWQ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imensions = await getImageDimensions(svg_base64)
expect(result).toBeDefined()
expect(result?.height).toEqual(297)
expect(result?.width).toEqual(210)
})

it('should return image dimensions for a animated svg base64', async (): Promise<void> => {
const svg_base64 =
'PHN2ZyB3aWR0aD0iMjQiIGhlaWdodD0iMjQiIHZpZXdCb3g9IjAgMCAyNCAyNCIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj48Zz48Y2lyY2xlIGN4PSIxMiIgY3k9IjIuNSIgcj0iMS41IiBvcGFjaXR5PSIuMTQiLz48Y2lyY2xlIGN4PSIxNi43NSIgY3k9IjMuNzciIHI9IjEuNSIgb3BhY2l0eT0iLjI5Ii8+PGNpcmNsZSBjeD0iMjAuMjMiIGN5PSI3LjI1IiByPSIxLjUiIG9wYWNpdHk9Ii40MyIvPjxjaXJjbGUgY3g9IjIxLjUwIiBjeT0iMTIuMDAiIHI9IjEuNSIgb3BhY2l0eT0iLjU3Ii8+PGNpcmNsZSBjeD0iMjAuMjMiIGN5PSIxNi43NSIgcj0iMS41IiBvcGFjaXR5PSIuNzEiLz48Y2lyY2xlIGN4PSIxNi43NSIgY3k9IjIwLjIzIiByPSIxLjUiIG9wYWNpdHk9Ii44NiIvPjxjaXJjbGUgY3g9IjEyIiBjeT0iMjEuNSIgcj0iMS41Ii8+PGFuaW1hdGVUcmFuc2Zvcm0gYXR0cmlidXRlTmFtZT0idHJhbnNmb3JtIiB0eXBlPSJyb3RhdGUiIGNhbGNNb2RlPSJkaXNjcmV0ZSIgZHVyPSIwLjc1cyIgdmFsdWVzPSIwIDEyIDEyOzMwIDEyIDEyOzYwIDEyIDEyOzkwIDEyIDEyOzEyMCAxMiAxMjsxNTAgMTIgMTI7MTgwIDEyIDEyOzIxMCAxMiAxMjsyNDAgMTIgMTI7MjcwIDEyIDEyOzMwMCAxMiAxMjszMzAgMTIgMTI7MzYwIDEyIDEyIiByZXBlYXRDb3VudD0iaW5kZWZpbml0ZSIvPjwvZz48L3N2Zz4='

const result: IImageDimensions = await getImageDimensions(svg_base64)
expect(result).toBeDefined()
expect(result?.height).toEqual(24)
expect(result?.width).toEqual(24)
})

it('should return image type for png base64', async (): Promise<void> => {
const png_base64 =
'iVBORw0KGgoAAAANSUhEUgAAAQYAAAB0CAYAAABnqJxCAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAcoUlEQVR4nO2df5BlZXnnP9+u61TXFDXFDuwUoXB2aooAEiBIkHvOyD0ioqjFskbkR1bUVIFsNBvQVeKyiBaLrItbG0MiYmIgIQZhIWCILKIg4G3gnh5YZBERWSQIFEtRSFEu1Ts11dXf/eP8vn27+57bDTPDvh9q6HN/nPc87+/nfZ7nfS8EAoFAIBAIBAKBQCAQCAQCgUAgEAgEAoFAIBAIBAKBQCAQCAQCgUAgEAgEAoFAIBAIBAKBQCAQCAQCgUAgEAgEAoFAIBAIBAKBQCAQCAQCgUAgEAgEAoFAIBAIBAKBQCAQCAQCgUAgEAgE/v9Fz6zb52cYEDT+Fi80dOnizuL94qbstVTdXl4XOL+t/qzqu78G4jfv/NXCa5TXNSOKk4uNTxPKs2FAF6SD/j/satkCgbWgI/sQxAugJ8qOqmIAMCrfpOzxKr5Udux8FMg7fDYguExIEraRGomXaRsfitkv/8Luj9kPcYjzF/n/N+xKkQKBtaSTd8bb3rzzV2ftKiGeXbfPjcAJlTqy+6PGlRnSjQKBPZpONZPvOjykQez2lMstqotdXYiBwBrSyf7s6katxp/dnUKxqZlI9hTRA4Gx6OwWM532LDW8YWOxdosiDATWkinlZsJdilV1tj2BmrfF5BrELi/EQGDtmALt8glb+cy7J/WtQlphJGcu2EDgDULHsMsnaufxDXtS36oGMVUZCATeIHR2h/a8p63RMy+KymAtodKzEgi8EZjaHdpzuU7fYxYTlcFRRQzDnja6BQLLkBkfd3V/LK13e0bnkqrYC7tYSezqQgwE1o6OtevDiqQ9r1sp1xAUNIXAG5Ap7F2+PjZm16st7bBdrYHM7uD0DQTWjA5q767sxsmU4FDbxyJ+C7QJ6IBfAX4h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UTZOoxyxUA8bconhnM4f0bduVROKFoDUEAoFAIBAIBAKBQCAQCAQCgUAgEAgEAoFAIBAIBAKBQCAQCAQCgUAgEAgEAoFAIBAIBAKBQCAQCAQCgUAgEAgEAoFAIBAIBAKBQCAQCAQCgUAgEAgEAoFAIBAIBAKBQCAQCAQCgUAgEAgEAoHAa8X/AzpJlj2mUDhvAAAAAElFTkSuQmCC'
Expand Down
54 changes: 24 additions & 30 deletions packages/ssi-sdk-core/src/utils/image.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import fetch from 'cross-fetch'
import { imageSize } from 'image-size'
import { IImageDimensions, IImageResource } from '../types'
import * as u8a from 'uint8arrays'

type SizeCalculationResult = {
width?: number
Expand All @@ -9,45 +10,38 @@ type SizeCalculationResult = {
type?: string
}

const uint8ArrayToString = (uint8Array: Uint8Array): string => {
const decoder = new TextDecoder('utf-8')
return decoder.decode(uint8Array)
}

// TODO: here we're handling svg separately, remove this section when image-size starts supporting it in version 2
const isSvg = (uint8Array: Uint8Array): boolean => {
const text = uint8ArrayToString(uint8Array)
const normalizedText = text.trim().toLowerCase()
const maxCheckLength: number = Math.min(80, uint8Array.length)
const initialText: string = u8a.toString(uint8Array.subarray(0, maxCheckLength))
const normalizedText: string = initialText.trim().toLowerCase()
return normalizedText.startsWith('<svg') || normalizedText.startsWith('<?xml')
}

const getSvgDimensions = (uint8Array: Uint8Array) => {
const svgContent = uint8ArrayToString(uint8Array)
const widthMatch = svgContent.match(/width="([^"]+)"/)
const heightMatch = svgContent.match(/height="([^"]+)"/)
const viewBoxMatch = svgContent.match(/viewBox="([^"]+)"/)

let width, height

if (widthMatch) {
width = widthMatch[1]
}

if (heightMatch) {
height = heightMatch[1]
}
function parseDimension(dimension: string): number | undefined {
const match: RegExpMatchArray | null = dimension.match(/^(\d+(?:\.\d+)?)([a-z%]*)$/)
return match ? parseFloat(match[1]) : 0
}

if ((!width || !height) && viewBoxMatch) {
const parts = viewBoxMatch[1].split(' ').map(Number)
if (!width && parts.length === 4) {
width = parts[2].toString()
}
if (!height && parts.length === 4) {
height = parts[3].toString()
const getSvgDimensions = (uint8Array: Uint8Array): SizeCalculationResult => {
const svgContent: string = new TextDecoder().decode(uint8Array)
const widthMatch: RegExpMatchArray | null = svgContent.match(/width="([^"]+)"/)
const heightMatch: RegExpMatchArray | null = svgContent.match(/height="([^"]+)"/)
const viewBoxMatch: RegExpMatchArray | null = svgContent.match(/viewBox="[^"]*"/)

let width: number | undefined = widthMatch ? parseDimension(widthMatch[1]) : undefined
let height: number | undefined = heightMatch ? parseDimension(heightMatch[1]) : undefined

if (viewBoxMatch && (!width || !height)) {
const parts = viewBoxMatch[0].match(/[\d\.]+/g)?.map(Number)
if (parts && parts.length === 4) {
const [x, y, viewBoxWidth, viewBoxHeight] = parts
width = width ?? viewBoxWidth - x
height = height ?? viewBoxHeight - y
}
}

return { width: Number(width), height: Number(height), type: 'svg' }
return { width, height, type: 'svg' }
}

export const getImageMediaType = async (base64: string): Promise<string | undefined> => {
Expand Down
4 changes: 0 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c86188e

Please sign in to comment.