gopngchunks

package module
v0.0.0-...-556dcc1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 12, 2023 License: GPL-3.0 Imports: 7 Imported by: 2

README

go-png-chunks

Library/Utility to manage tEXt chunks inside PNG files

Example for reading all tEXt chunks from file

import (
	"fmt"
	"io/ioutil"
	"os"
	gopngchunks "github.com/chrisbward/go-png-chunks"
)

func ReadtEXtChunksFromFile(inputFilePath string) (tEXtChunks []gopngchunks.TEXtChunk, err error) {
	f, err := os.Open(inputFilePath)
	if err != nil {
		return tEXtChunks, fmt.Errorf("os.Open(): %s", err)
	}
	defer f.Close()
	data, err := ioutil.ReadAll(f)
	if err != nil {
		return tEXtChunks, fmt.Errorf("ioutil.ReadAll(): %s", err)
	}
	tEXtChunks, err = gopngchunks.GetAlltEXtChunks(data)
	if err != nil {
		panic(err)
	}
	return tEXtChunks, nil
}


Example for writing to PNG

import (
	"fmt"
	"io/ioutil"
	"os"

	gopngchunks "github.com/chrisbward/go-png-chunks"
)
var helloWorld = "aGVsbG8gd29ybGQ="

func WritetEXtChunkToFile(inputFilePath string, outputFilePath string) error {
	f, err := os.Open(inputFilePath)
	if err != nil {
		return fmt.Errorf("os.Open(): %s", err)
	}
	defer f.Close()
	data, err := ioutil.ReadAll(f)
	if err != nil {
		return fmt.Errorf("ioutil.ReadAll(): %s", err)
	}
	tEXtChunkToWrite := gopngchunks.TEXtChunk{
		Key:   "helloworld",
		Value: helloWorld,
	}
	w, err := gopngchunks.WritetEXtToPngBytes(data, tEXtChunkToWrite)
	if err != nil {
		panic(err)
	}

	out, err := os.Create(outputFilePath)
	if err != nil {
		panic(err)
	}
	defer out.Close()
	out.Write(w.Bytes())

	return nil
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var IENDChunkType = "IEND"
View Source
var NULLBYTE = "\x00"
View Source
var PNGHeader = "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A"

89 50 4E 47 0D 0A 1A 0A

Functions

func ContainsPNGMagicBytesHeader

func ContainsPNGMagicBytesHeader(data []byte) bool

func WritetEXtToPngBytes

func WritetEXtToPngBytes(inputBytes []byte, TEXtChunkToWrite TEXtChunk) (outputBytes bytes.Buffer, err error)

Types

type TEXtChunk

type TEXtChunk struct {
	Key   string
	Value string
}

func GetAlltEXtChunks

func GetAlltEXtChunks(inputBytes []byte) (textChunks []TEXtChunk, err error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL