Skip to content

validateImage ​

Valida uma imagem antes da compressão — formato, dimensões e integridade básica.

Assinatura ​

typescript
function validateImage(file: File | Blob): Promise<ImageValidationResult>

function isValidImage(file: File | Blob): Promise<boolean>

Parâmetros ​

file ​

File | Blob — candidate image.

Retorno ​

ImageValidationResult ​

typescript
interface ImageValidationResult {
  isValid: boolean
  errors: string[]
  warnings: string[]
  actualFormat?: string
  declaredFormat?: string
  dimensions?: { width: number; height: number }
  fileSize?: number
}
FieldDescription
isValidtrue when there are no hard errors
errorsBlocking issues (corrupt, not an image, …)
warningsSoft issues (MIME mismatch, huge dimensions, …)
actualFormatDetected MIME / codec
declaredFormatfile.type when available
dimensionsNatural width/height when decodable
fileSizeByte length

isValidImage(file) is equivalent to (await validateImage(file)).isValid.

Exemplos ​

Gate compression ​

typescript
import { validateImage, compress } from '@pantanal/pixu'

const validation = await validateImage(file)

if (!validation.isValid) {
  throw new Error(validation.errors.join('; '))
}

for (const warning of validation.warnings) {
  console.warn(warning)
}

const result = await compress(file, { quality: 0.8 })

Shorthand ​

typescript
import { isValidImage, compress } from '@pantanal/pixu'

if (await isValidImage(file)) {
  await compress(file)
}

Via compress options ​

typescript
await compress(file, {
  validateImage: true,
  quality: 0.8,
})

Relacionado ​

Publicado sob a licença MIT.