# JSImage

## Overview

The `JSImage` class provides a scripting interface for image manipulation, enabling operations like resizing, rotating, flipping, and metadata management. It integrates with the images plugin for streamlined handling of image files.

## Functionality

The class supports image transformation, including flipping vertically or horizontally with `flip(type)` and rotating by a specified number of degrees using `rotate(degrees)`. Images can be resized while maintaining their aspect ratio through `resize(width, height)`. Methods like `getWidth()` and `getHeight()` retrieve image dimensions.

For metadata, `getMetaDataProperties()` lists available properties, while `getMetaDataDescription(property)` and `getMetaDataObject(property)` provide detailed information or the actual metadata object. The `getContentType()` method fetches the MIME type of the image, and `getData()` retrieves the image bytes for storage or further processing.

These features enable comprehensive control and management of image files in scripting environments.

For more details, please refer to the [Images plugin](https://docs.servoy.com/guides/develop/programming-guide/working-with-files/images) section of this documentation.

## Methods Summarized

| Type                                                                    | Name                                                                 | Summary                                                                                 |
| ----------------------------------------------------------------------- | -------------------------------------------------------------------- | --------------------------------------------------------------------------------------- |
| [JSImage](/reference/servoyextensions/server-plugins/images/jsimage.md) | [flip(type)](#flip-type)                                             | Flips the image verticaly (type param=0) or horizontaly (type param=1).                 |
| [String](/reference/servoycore/dev-api/js-lib/string.md)                | [getContentType()](#getcontenttype)                                  | Gets the contenttype (image/jpeg) of this image.                                        |
| [Array](/reference/servoycore/dev-api/js-lib/array.md)                  | [getData()](#getdata)                                                | Gets the bytes of this image, so that they can be saved to disk or stored the database. |
| [Number](/reference/servoycore/dev-api/js-lib/number.md)                | [getHeight()](#getheight)                                            | Gets the height of this image.                                                          |
| [String](/reference/servoycore/dev-api/js-lib/string.md)                | [getMetaDataDescription(property)](#getmetadatadescription-property) | Gets the description of a metadata property from the image.                             |
| [Object](/reference/servoycore/dev-api/js-lib/object.md)                | [getMetaDataObject(property)](#getmetadataobject-property)           | Gets the real object of a metadata property from the image.                             |
| [Array](/reference/servoycore/dev-api/js-lib/array.md)                  | [getMetaDataProperties()](#getmetadataproperties)                    | Gets the available metadata properties from the image.                                  |
| [Number](/reference/servoycore/dev-api/js-lib/number.md)                | [getWidth()](#getwidth)                                              | Gets the width of this image.                                                           |
| [JSImage](/reference/servoyextensions/server-plugins/images/jsimage.md) | [resize(width, height)](#resize-width-height)                        | Resizes the image to the width/height given, keeping aspect ratio.                      |
| [JSImage](/reference/servoyextensions/server-plugins/images/jsimage.md) | [rotate(degrees)](#rotate-degrees)                                   | Rotates the image the number of degrees that is given.                                  |

## Methods Detailed

### flip(type)

Flips the image verticaly (type param=0) or horizontaly (type param=1). A new JSImage is returned.

**Parameters**

* [Number](/reference/servoycore/dev-api/js-lib/number.md) **type** ;

**Returns:** [JSImage](/reference/servoyextensions/server-plugins/images/jsimage.md) A new JSImage instance flipped vertically or horizontally, based on the type parameter.

**Sample**

```js
var image = plugins.images.getImage(byteArray_or_file_or_filename);//loads the image
image = image.flip(0);//flip vertically
var bytes = image.getData();//gets the image bytes
plugins.file.writeFile('filename',bytes);//saves the image bytes
```

### getContentType()

Gets the contenttype (image/jpeg) of this image.

**Returns:** [String](/reference/servoycore/dev-api/js-lib/string.md) The MIME type of the image, such as "image/jpeg" or "image/png", or null if unavailable.

**Sample**

```js
var image = plugins.images.getImage(byteArray_or_file);
var width = image.getWidth();
var height = image.getHeight();
var contentType = image.getContentType();
```

### getData()

Gets the bytes of this image, so that they can be saved to disk or stored the database.

**Returns:** [Array](/reference/servoycore/dev-api/js-lib/array.md) The image data as a byte array for saving or processing.

**Sample**

```js
var image = plugins.images.getImage(byteArray_or_file_or_filename);//loads the image
image = image.resize(200,200);//resizes it to 200,200
var bytes = image.getData();//gets the image bytes
plugins.file.writeFile('filename',bytes);//saves the image bytes
```

### getHeight()

Gets the height of this image.

**Returns:** [Number](/reference/servoycore/dev-api/js-lib/number.md) The height of the image in pixels.

**Sample**

```js
var image = plugins.images.getImage(byteArray_or_file);
var width = image.getWidth();
var height = image.getHeight();
var contentType = image.getContentType();
```

### getMetaDataDescription(property)

Gets the description of a metadata property from the image. Currently only jpg is supported.

**Parameters**

* [String](/reference/servoycore/dev-api/js-lib/string.md) **property** ;

**Returns:** [String](/reference/servoycore/dev-api/js-lib/string.md) The description of the specified metadata property, or null if not found.

**Sample**

```js
var image = plugins.images.getImage(byteArray_or_file_or_filename);//loads the image
// get the available metadata properties from the image, currently only jpg is supported
var propertiesArray = image.getMetaDataProperties();
for(var i=0;i<propertiesArray.length;i++)
{
	var property = propertiesArray[i]
	application.output("property: " + property);
	application.output("description (string): " + image.getMetaDataDescription(property))
	application.output("real object: " + image.getMetaDataObject(property))
}
// Thumbnail data is stored under property 'Exif - Thumbnail Data', extract that and set it in a dataprovider
thumbnail = image.getMetaDataObject("Exif - Thumbnail Data"); // gets thumbnail data from the image
```

### getMetaDataObject(property)

Gets the real object of a metadata property from the image. Currently only jpg is supported.

**Parameters**

* [String](/reference/servoycore/dev-api/js-lib/string.md) **property** ;

**Returns:** [Object](/reference/servoycore/dev-api/js-lib/object.md) The metadata object associated with the specified property, or null if not found.

**Sample**

```js
var image = plugins.images.getImage(byteArray_or_file_or_filename);//loads the image
// get the available metadata properties from the image, currently only jpg is supported
var propertiesArray = image.getMetaDataProperties();
for(var i=0;i<propertiesArray.length;i++)
{
	var property = propertiesArray[i]
	application.output("property: " + property);
	application.output("description (string): " + image.getMetaDataDescription(property))
	application.output("real object: " + image.getMetaDataObject(property))
}
// Thumbnail data is stored under property 'Exif - Thumbnail Data', extract that and set it in a dataprovider
thumbnail = image.getMetaDataObject("Exif - Thumbnail Data"); // gets thumbnail data from the image
```

### getMetaDataProperties()

Gets the available metadata properties from the image. Currently only jpg is supported.

**Returns:** [Array](/reference/servoycore/dev-api/js-lib/array.md) An array of metadata property names available in the image.

**Sample**

```js
var image = plugins.images.getImage(byteArray_or_file_or_filename);//loads the image
// get the available metadata properties from the image, currently only jpg is supported
var propertiesArray = image.getMetaDataProperties();
for(var i=0;i<propertiesArray.length;i++)
{
	var property = propertiesArray[i]
	application.output("property: " + property);
	application.output("description (string): " + image.getMetaDataDescription(property))
	application.output("real object: " + image.getMetaDataObject(property))
}
// Thumbnail data is stored under property 'Exif - Thumbnail Data', extract that and set it in a dataprovider
thumbnail = image.getMetaDataObject("Exif - Thumbnail Data"); // gets thumbnail data from the image
```

### getWidth()

Gets the width of this image.

**Returns:** [Number](/reference/servoycore/dev-api/js-lib/number.md) The width of the image in pixels.

**Sample**

```js
var image = plugins.images.getImage(byteArray_or_file);
var width = image.getWidth();
var height = image.getHeight();
var contentType = image.getContentType();
```

### resize(width, height)

Resizes the image to the width/height given, keeping aspect ratio. A new JSImage is returned.

**Parameters**

* [Number](/reference/servoycore/dev-api/js-lib/number.md) **width** ;
* [Number](/reference/servoycore/dev-api/js-lib/number.md) **height** ;

**Returns:** [JSImage](/reference/servoyextensions/server-plugins/images/jsimage.md) A new JSImage instance resized to the specified dimensions, or null if resizing fails.

**Sample**

```js
var image = plugins.images.getImage(byteArray_or_file_or_filename);//loads the image
image = image.resize(200,200);//resizes it to 200,200
var bytes = image.getData();//gets the image bytes
plugins.file.writeFile('filename',bytes);//saves the image bytes
```

### rotate(degrees)

Rotates the image the number of degrees that is given. A new JSImage is returned.

**Parameters**

* [Number](/reference/servoycore/dev-api/js-lib/number.md) **degrees** ;

**Returns:** [JSImage](/reference/servoyextensions/server-plugins/images/jsimage.md) A new JSImage instance rotated by the specified degrees.

**Sample**

```js
var image = plugins.images.getImage(byteArray_or_file_or_filename);//loads the image
image = image.rotate(90);//rotate the image 90 degrees
var bytes = image.getData();//gets the image bytes
plugins.file.writeFile('filename',bytes);//saves the image bytes
```

***


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.servoy.com/reference/servoyextensions/server-plugins/images/jsimage.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
