Method: Perfion.WriteFile( … )

Will write any file and image stored by Perfion to a file. For images it can resize, change the image format and change the aspect by exact fitting or cropping or smart fitting. It performs all of the same well known image manipulation functionality as the Media Server, and the syntax is identical.
This method will also check if the file you intend to write has already been written, and only spend time on retrieving and updating the file if its timestamp is different from the file stored in Perfion.
This method is also capable of writing multiple files, e.g. from a Multi-valued feature in Perfion.

Syntax

{ScriptObject} WriteFile( string id = "", string file = "{@id}{@par}", string par = "", string root = "" )

All the parameters to this method are optional. Therefore it is recommend to include them by name, as shown in the examples below. The behavior of each parameter is described in the following table.

Method Parameters

string
id

The unique Perfion Guid ID of a file, or a ';' separated list of multiple IDs, specifying which files or images to deliver. Default: Will take the Value of the current Map-entry. So if used on a Map-entry that contains valid IDs you do not need to specify this parameter.

 

string
file

Specify the filename(s).
The best option is to create this as a file-naming-template, since you may not know what name the file should have, and if there are multiple files or information about the files you want to include in the name this is much easier.

If unspecified it defaults to the following template "{@id}{@par}"
This means that the filename will begin with the unique Perfion Guid ID followed by the parameters specifying the requested file (e.g. version, image-size, dpi, fitting, quality and background-color etc.).

When specifying a template you can use static content as well several different parameters about the file as described in the following table:

 

 

@id

The unique Perfion Guid ID

 

@par

Short-form describing the most important aspects of the requested file or image, such as version, image-size, dpi, fitting, quality and background.color. The value could look like this, if you requested an image to be cropped and fit within 300x300 pixels: -300x300-Crop

 

@filename

The name of the file as it is stored in Perfion

 

@index

A numeric index, that can be used for Multi-valued features, if you do not want to use the unique @id or @filename is not unique, or you simply want to use a shared name (e.g. the SKU) so they would all have the same filename unless an index was added.

 

Alternative:
As an alternative to the above template naming. You can also specify the name exactly as you want it, or specify a ';' separated list of filenames to be used when multiple id's are requested. In the latter case, the number of filenames must of course match the number of id's requested.|

string
par

Specify what (if anything) to do or change about images before writing them.
This syntax is 100% identical to what is used on the Media Server.
Example: size=300x300&format=jpg&fit=crop
The following table describes each of the parameters:

 

 

size

The size in pixels (width x height) that an image has to fit within.
Example: size=300x300
If the image is larger it is resized without changing the aspect ratio (depending on the fit parameter)
Default: No resizing, which means the original size.

 

dpi

The size in DPI (dots per inch).
Example: dpi=150
size and dpi cannot both be specified. If so the size will take precedence.

 

fit

How an image is made to fit within a specified size.
fit=fit : Aspect ratio is maintained as original (this is the default)
fit=exact : Image is stretched to match the requested size
fit=crop : Image is cropped to match the requested size
fit=smart : Image is partially cropped and stretched to fit the requested size
fit=aspectratio : Image is resized to the new size by keeping the aspect ratio so that it fits best and then the remaining area (if the new size does not match the original image aspect ratio) is filled using a color which can be specified using 'canvascolor' parameter.

 

format

The requested image-format.

The Perfion stored image, is automatically converted to the requested format such as jpg, png, tif, bmp, gif etc.
Example: format=jpg : will deliver an image in jpg-format

It is also possible to specify the format as follows:
format=web : will automatically convert images from the stored format to one of the formats supported in a browser, i.e. either jpg or png.
Images stored as jpg will be delivered as jpg and all other image formats will be delivered in png-format.

 

version

If logging of binaries is enabled in Perfion this makes it possible to request the logged version of the binary file. Values are 0 (not versioned) or 1 (versioned) binary.
Example: version=1
If unspecified, the default value is 0 which is also the only version of binary if logging of binaries is not enabled.

 

quality

The image quality.
This is only relevant for e.g. JPG and PNG formats and ignored otherwise. The value should be between 0 and 100.
Example: quality=85

 

tiffcomp

The TIFF compression method.
Values: LZW (Lempel-Ziv and Welch compression), ZIP (Deflate compression) or None. Default value is LZW.
The parameter can be used only if the target image format is a TIFF image.
Example: tiffcomp=zip

 

backgroundcolor

The requested background color of an image.
This only works when the target image format does not support transparency and the source image format has transparency. In such case the source image transparency will be replaced with background color.
The color must be specified as RGB color code in hexadecimal format.
Example: backgroundcolor=3399FF

 

canvascolor

The requested canvas color of the image. It is only usable when image is resized with 'fit' option set to 'aspectratio'.
The color must be specified as RGB or ARGB color code in hexadecimal format by using 6 (RGB) or 8 (ARGB) char color code. In case of 8 char color code, the first 2 characters represent the alpha channel value.
Example: canvascolor=3399FF

 

colorprof

The color profile of the target image. It is only usable with images which support color profiles. If specified the image colors will be converted to that color profile and the profile itself will be embedded to the image file.
Values: Original (keep the original color profile), CMYK (Coated FOGRA39), sRGB, AdobeRGB1998, ProPhoto.
Example: colorprof=srgb

 

usepoi

The request to use the Point-of-Interest (POI) of the image if it has POI specified in its metadata. If image does not have POI specified or the POI is not used, then POI will be equivalent to value 50x50 which is the center of the image.
Values: 0 (do not use POI) and 1 (use POI). Default value: 0.
The parameter can only be used with 'size' and 'fit' parameters when 'fit' parameter is set to 'crop' or 'smart'.
Example: usepoi=1

 

poi

The request to use the custom Point-of-Interest (POI) of the image. This parameter can only be used if 'usepoi' is used and set to '1'. If this parameter is used, then it will overwrite the original image POI value saved as its metadata.
Value is of format <X>x<Y> where X and Y are relative image horizontal and vertical coordinates expressed in %. The reference point is image top right corner which will have value 0x0. The bottom right corner of the image will have value 100x100. The center of the image will have value 50x50.
The parameter can only be used with 'size' and 'fit' parameters when 'fit' parameter is set to 'crop' or 'smart'.
Example: poi=50x50

 

wa

Write Always
The default behaviour when unspecified (wa=0) is to never write a file if it already exists and its timestamp is identical to the file stored in Perfion.
This parameter makes it possible to override this behaviour and make sure a file is always written.
Example: wa=1 : means always write.
This is useful in situations where you know the file is not the same (e.g. because it's of a different size). Only the filename and timestamp are used for comparison. So the best option is therefore to include parameters about the file in its filename to make sure that files of e.g. different sizes have unique names (unless you for some reason want a different behaviour, but in such case the automatic comparison will not work).

string
root

Specifies the root of a path.
If specified, all filenames will be prefixed with the root-path value.

 

Example 1

Below automatically takes the image-ids from the current Map-entry, and generates filenames based on the specified template by including the static part "Binaries/SKUImage-" and concatenating this with a lookup from the current Input-row of the "{SKU}" followed by a name-part "{@par}" constructed automatically based on the requested image parameters.
The root-path is taken from a global variable named $RootPath.
The image is requested as "size=200x200&format=web", meaning to fit within 200x200 pixels and be delivered in one of the web-formats (jpg, gif or png).
The method returns a simple object that delivers the OutFileName (which is the name the file was written as based on the file-naming-template, rootpath etc. This OutFileName is written to the Value of the Output-row so it contains information about where the file is. Voila!

var info = Perfion.WriteFile( file: "Binaries/SKUImage-{SKU}{@par}", root: "{=$RootPath}", par: "size=200x200&format=web" ); Value = info.OutFileName;

Example 2

Below example is a tiny bit more advanced? It shows how a multivalued image feature could be written. Again by using a file-naming template, but this time including the @index parameter to make sure each image get a unique name, since they would otherwise end up with the same name (in this example). You can of course always use the unique Perfion Guid ID, but sometimes you want to export the images or files by using other more readable names.

var info = Perfion.WriteFile( file: "Binaries/SKUMultiImages-{SKU}[{@index}]*{@par}", root: "C:/FileRoot/", par: "size=300x200&format=web" ); Value = info.OutFileName;