You Cant Upload Png With Textual Chunks

png-itxt

Windows Mac/Linux
Windows Build status Build Status

js-standard-style

Tool for calculation and reading textual data in PNG images using streams. All three textual chunks (iTXt, zTXt and tEXt) can be both read and written by the tool. Chunks tin be filtered by chunk blazon and keyword equally required. Compressing and decompressing of data, where advisable, is handled transparently to the user so you only ever see the uncompressed values.

Three different methods are provided for using the tool:

  • command line
  • client side browser (cheers to browserify)
  • node library (via crave)

Install with

              npm install png-itxt                          

Using in Node

To apply the tool in your node programs you must first require the module. If installed with npm and so you could write the following.

                

var  pngitxt = require ( ' png-itxt ' )

Constants

The module exports constants for the types of the textual chunks. These tin can be accessed as follows.

                

pngitxt . iTXt

pngitxt . zTXt

pngitxt . tEXt

Data Format

Chunk data should be provided and will exist returned every bit an object equally shown below. Note that this is the total declaration for a iTXt chunk as will be produced by the program. However not all fields are required when passing data to the programme. Additionally some of these fields are not relevant to zTXt and tEXt chunks.

                

{

  type : ' iTXt ' ,

  keyword : ' keyword ' ,

  value : ' value ' ,

  language : ' ' ,

  translated : ' ' ,

  compressed : false ,

  compression_type : 0

}

See the table below for details of which fields are relevent to each chunk and what their default values are.

Field Name Chunks Default
blazon ALL iTXt
keyword ALL None - must be specified for set
value ALL None - must exist specified for ready
lanuage iTXt empty string
translated iTXt empty cord
compressed iTXt, zTXt false apart from zTXt chunks
compression_type iTXt, zTXt 0 (only valid value)

prepare - Writing iTXt data

The set part can be used to write new textual chunks to a paradigm or replace existing ones. The following example shows how a new iTXt chunk can be added with the keyword pizza and the value delicious. While only these 2 options are specified hither whatsoever of the other fields identified before tin be specified. If compressed is prepare to true and so the value will exist compressed before it is written to the image.

                

fs . createReadStream ( ' input.png ' )

. pipe ( pngitxt . set ( {  keyword : ' pizza ' ,  value : ' succulent ' } ) )

. pipe ( fs . createWriteStream ( ' output.png ' ) )

The set function will overwrite all the chunks of the same type and the keyword. In the previous example is in that location were already iTXt chunks with the keyword pizza (the spec allows for more than one chunk with the same keyword) and then the values stored in those chunks would exist lost.

It is also possible to have different types of textual chunk with the same keyword. An option is provided to allow you to supplant all textual chunks with the same keyword regardless of their type. To exercise this specify the value true as the second parameter to the function.

                

fs . createReadStream ( ' input.png ' )

. pipe ( pngitxt . set up ( {  keyword : ' pizza ' ,  value : ' delicious ' } , true ) )

. pipe ( fs . createWriteStream ( ' output.png ' ) )

In this case if there were already any iTXt, zTXT or tEXt chunks with the keyword pizza and then they would both exist replaced by a unmarried iTXt chunk with the new value.

Using gear up to Remove Chunks

In the special example where you lot pass zip as the value to be stored then all chunks that would usually have been replaced will only be removed. For example the following example would result in all the textual chunks with the keyword pizza beingness removed from the image.

                

fs . createReadStream ( ' input.png ' )

. pipe ( pngitxt . gear up ( {  keyword : ' pizza ' ,  value : naught } , truthful ) )

. pipe ( fs . createWriteStream ( ' output.png ' ) )

Exceptions

An exception will be thrown if y'all pass in an unknown chunk type.

                

fs . createReadStream ( ' input.png ' )

. pipe ( pngitxt . set ( {  type : ' causeanexception ' ,

        keyword : ' pizza ' ,  value : ' delicious ' } ) )

. piping ( fs . createWriteStream ( ' output.png ' ) )

get - Reading iTXt data

Retrieval of data is accomplished using the get role and callbacks. The callback will be invoked each time a matching chunk is found. It cannot be assumed that callback volition only be called in one case for a particular keyword. You are guaranteed that your callback volition exist chosen at least once fifty-fifty if no matching chunks are found - in that case a zip is provided instead of data. By default the go function will return any textual chunks that match the criteria provided. If you but want 1 blazon of chunk (east.g iTXt) and then meet the section of filters.

Callback Signature

The callback to the get role is given 2 parameters. The kickoff is used to indicate whether or not an error was encountered. The second returns the information found in the format outlined above. In the example of an error the information provided may not be null. For example if the error was caused when trying to inflate a compressed value then all the other data nerveless about the clamper will be returned with the mistake.

                

function pngtxtCallback ( err , data ) {

if ( ! err &&  data ) {

panel . log ( information . keyword , " : " , data . value )

}

}

Finding a specific keyword

To find the text associated with a specific keyword you lot must call get and provide both the keyword and a callback. Passing null as the keyword volition cause all textual chunks to be passed to the callback. If the keyword is not constitute the callback will exist called with null for both parameters. Note equally mentioned previously this example will render whatever textual clamper that has the specified keyword. If you but want a specific type of clamper and then come across the section on filters beneath.

                

fs . createReadStream ( ' input.png ' )

. piping ( pngitxt . get ( ' pizza ' , function ( err , data ) {

if ( ! err &&  data ) {

console . log ( data . keyword , " : " , data . value )

}

} ) )

Using Regular Expressions

Instead of providing a keyword you can provide a regular expression and all chunks whose keywords match the regular expression volition be passed to the callback function. If no chunks are found then the callback volition be called once with null for both parameters.

                

fs . createReadStream ( ' input.png ' )

. pipe ( pngitxt . get ( / Pi [ z ] {2} a / i , function ( err , data ) {

if ( ! err &&  information ) {

console . log ( data . keyword , " : " , information . value )

}

} ) )

Finding all textual chunks

To detect all chunks simply provide a callback that takes two parameters. If no textual chunks are found the callback will be called once with goose egg for both parameters. Otherwise it volition be called for each block.

                

fs . createReadStream ( ' input.png ' )

. pipe ( pngitxt . become ( function ( err , data ) {

if ( ! err &&  data ) {

console . log ( information . keyword , " : " , data . value )

}

} ) )

Filtering by Chunk Blazon

To filter the chunks by type you can provide an array of acceptable clamper types. Even so for yous convience three wrapper functions are provided if y'all are you are just looking for chunks of a particular type. These functions are shown beneath and, apart from the filters assortment, they accept the verbal same parameters as the normal get office.

                

office callback ( err , data ) {

}

fs . createReadStream ( ' input.png ' )

. pipe ( pngitxt . getitxt ( ' cat ' ,  callback ) )

. pipe ( pngitxt . getitxt ( callback ) )

. pipe ( pngitxt . getztxt ( ' cat ' ,  callback ) )

. pipe ( pngitxt . getztxt ( callback ) )

. pipe ( pngitxt . gettext ( ' cat ' ,  callback ) )

. piping ( pngitxt . gettext ( callback ) )

To specify a specific combination of chunk types use an array every bit shown in the following example where the information is filtered so that you merely get tEXt and iTXt chunks with the keyword pizza.

                

fs . createReadStream ( ' input.png ' )

. piping ( pngitxt . get ( ' pizza ' , [ pngitxt . tEXt , pngitxt . iTXt ] , function ( err , data ) {

if ( ! err &&  data ) {

console . log ( data . keyword , " : " , data . value )

}

} ) )

Please notation that even if yous are only looking for one type of chunk the value must be passed as an array or an exception volition be thrown. You can specify whatever combination of chunk types that yous want just be aware that specifying all 3, as shown below, is the equivalent of having no filter.

                

fs . createReadStream ( ' input.png ' )

. pipage ( pngitxt . become ( ' pizza ' , [ pngitxt . tEXt , pngitxt . zTXt , pngitxt . iTXt ] ,

role ( err , data ) {

if ( ! err &&  data ) {

console . log ( data . keyword , " : " , data . value )

}

} ) )

If no keyword is specified so the filter assortment can exist provided as the first parameter to the function.

                

fs . createReadStream ( ' input.png ' )

. pipe ( pngitxt . get ( [ pngitxt . iTXt , pngitxt . zTXt ] , function ( err , data ) {

if ( ! err &&  data ) {

console . log ( data . keyword , " : " , information . value )

}

} ) )

Exceptions

In all just 2 cases errors are indicated past passing them to the callback function. Withal if no callback function is provided or the filter arguement is wrong (i.e. information technology is not an array or doesn't contain any valid clamper types) and then exceptions will be thrown to signal this.

                

fs . createReadStream ( ' input.png ' )

. pipe ( pngitxt . get ( " pizza " , ' apathetic ' ) )

Browser Library

As a test a browser libarary, produced using browserify in standalone mode, is available in the dist folder. Both a normal and minified version is provided. To include it on your webpage simple copy the bundled js file to the appropriate location and add the following. All exports from the library are then bachelor on the pngitxt object.

                

< srcipt src = ' pngitxt-browser.min.js ' > < / srcipt >

The library exposes the same constats every bit for node and tin be accessed in the same style. The library likewise exposes the same go and set functions merely there are some differences in how they are called. The signature for the become function is shown below. All the parameters are the same apart from the start one which should provide the binary data of the image to bank check. Such a cord can be obtained in diverse fashion including using the FileReader.readAsBinaryString method.

                

pngitxt . get ( input ,  keyword ,  filters , part ( err , data ) {

if ( ! err &&  data ) {

console . log ( JSON . stringify ( information ) )

}

} )

As before a number of helper functions are besides provided for those that want to search for only i blazon of text chunk. An example of how to call these methods in the browser are shown below.

                

var  input = undefined

function callback ( err , data ) {

}

pngitxt . getitxt ( input , ' cat ' ,  callback )

pngitxt . getztxt ( input , ' cat ' ,  callback )

pngitxt . gettext ( input , ' cat ' ,  callback )

The ready part has a like input parameter and a data parameter as before. The last parameter is a callback that gives a binary string containing the altered image data. If, for case, you lot wanted to add an iTXt cake to a picture and then display the picture on a folio you tin can convert the data to a base64 encoding to brandish it on the page as follows.

                

pngitxt . set ( input , {  keyword : " examination " ,  value : " value " } ,

function ( effect ) {

var  img = document . createElement ( ' img ' ) ;

img . src = " data:prototype/png;base64, " + btoa ( issue ) ;

document . body . appendChild ( img ) ;

} )

A simple, crude and prepare proof of concept of this functionality is available as a webpage at dist/case.html. This page will allow you to elevate and drop images to either inspect their data or add boosted data.

Control Line Tool

The command line tool, called png-itxt is provided in the bin binder and will exist automatically linked from the node_modules/.bin folder if installed with npm. The two commands information technology supports are as follows.

  • png-itxt get
  • png-itxt set

png-itxt become

This command allows you to seach an paradigm for textual chunks of any type and will output the result in JSON format. For example the following command will search the input.png file for a iTXT or zTXt chunk that has the keyword pizza and then output the results to standard output.

              png-itxt become -m pizza -f iTXt,zTXt input.png                          

A consummate list of the options for the get command are shown below.

                              Usage: png-itxt-get [options] <file.png>    Tool for getting textual information from PNG images    Options:      -h, --help                 output usage data     -V, --version              output the version number     -k, --keyword <keyword>    keyword to serach for     -r, --regexp <expression>  regular expression to search by     -i, --ignorecase           makes the search example insensitive     -s, --stdin                reads the paradigm data from standard input     -f, --filter <chunktypes>  comma seperated list of clamper types to render     -o, --output <file>        write results to a file                          

png-itxt set

This command allows you to add iTXt chunk to an prototype. For instance the following command will add a clamper with keyword pizza and the value delicious to an epitome from the file input.png and and then salve it to output.png. The command can likewise be used to read data from stdin and output the issue to stdout and so that you tin can pipe the information betwixt commands. Notation that this tool will only add iTXt chunks.

              png-itxt prepare -chiliad pizza -d delicious -o output.png input.png                          

A full list of all the avilable options available for apply with the set command are shown below.

                              Usage: png-itxt-gear up [options] <fileIn.png>    Tool for setting textual information into a PNG images. If no output method is specified output volition be put into {fileIn}_out.png    Options:      -h, --help               output usage information     -V, --version            output the version number     -k, --keyword <keyword>  keyword to set up the value for     -d, --data <chunkdata>   data to store with the keyword     -f, --file <datafile>    text file to read the value from     -v, --valuein            read data to store from standard input     -c, --compress           compress the value stored in the clamper     -due south, --stdin              read png data from standard input     -p, --pipe               redirect output to stdout for processing     -o, --output <file>      file to output PNG data to                          

lewisprot1972.blogspot.com

Source: https://www.npmjs.com/package/png-itxt

0 Response to "You Cant Upload Png With Textual Chunks"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel