Tech Tip: Image Compression with CONVERT PICTURE
PRODUCT: 4D | VERSION: 16 | PLATFORM: Mac & Win
Published On: March 30, 2018
Image compression (reducing the size of a graphic file with an acceptable level of image quality loss) is important to reduce the size of disk space used by graphics, or speed up a web based application that serves graphics to end users. In 4D, the command CONVERT PICTURE lets the developer compress an image via the third parameter, but not all image types are supported with compression.
The ".jpg", ".jp2", ".wdp", and ".hdp" codecs support image compression with CONVERT PICTURE in 4D, but with some conversion from one format to another it is possible to store images in, for example, a compressed TIF format. Simply convert the image to a compressed JPG format and then to a TIF format and the image will be of reduced size*.
For example:
C_PICTURE($pic1;$pic2) // Pull a record from a table with an image, store the image twice for comparison FIRST RECORD([Table_1]) $pic1:=[Table_1]img $pic2:=[Table_1]img // convert the image to TIF CONVERT PICTURE($pic1;".tif") // convert the image to JPG, compressed, then TIF CONVERT PICTURE($pic2;".jpg";0) CONVERT PICTURE($pic2;".tif") |
Looking in the debugger, $pic1 and $pic2 (the same image) shows the image size for $pic1 (uncompressed TIF) versus $pic2 (compressed TIF).
*Note: Compression of TIF formatted image through the above method will work only on a WINDOWS, with MAC the TIF image remains the same size after compression. Not all TIF formatted images are compressible.
The command PICTURE CODEC LIST returns an array with the list of picture file codec ids that are available on the machine where it is executed, which can be called as:
ARRAY TEXT($codecs;0) ARRAY TEXT($codec_names;0) PICTURE CODEC LIST($codecs;$codec_names) // Returns arrays of codecs and codec names for supported picture files supported |