Learn what Felgo offers to help your business succeed. Start your free evaluation today! Felgo for Your Business

HttpImageUtils

Provides functionality for scaling and cropping an image file before uploading it with HttpRequest. More...

Import Statement: import Felgo 4.0
Since: Felgo 2.18.1

Methods

Detailed Description

The HttpImageUtils only work in conjunction with the HttpRequest item of the Felgo DuperAgent integration. It is available as a singleton item and usable from all QML components that import Felgo via import Felgo.

When uploading images using HttpRequest, the HttpImageUtils allow to crop, scale and transcode image files before the upload.

Example Usage

The first step is to create a reader for your image. The reader allows to get and set configuration properties, before calling read to load the image with the given settings.

For example to clip the image, you can use the setClipRect function of the reader:

 var reader = HttpImageUtils.createReader(imagePath);
 reader.setClipRect(0, 0, 200, 200);
 reader.read();

The read function reads and decodes the image into memory based on the previously configured settings. You can pass the returned object of read to the attach function of a request created with HttpRequest.

HttpImageUtils Reader API

The reader object retrieved from createReader() supports the following methods:

  • var size() - Returns an object with the width and height properties of the image without actually decoding the image.
  • int filesize() - Returns the filesize (in bytes) of the encoded image.
  • var setScaledSize(int width, int height, int fitMode)

    Sets the desired scaled size for the image when it is decoded. Depending on the image format, some decoders can do this efficiently without having to decode the full image first and then downscale.

     var reader = HttpImageUtils.createReader(imagePath);
     reader.setScaledSize(200, 200, Image.PreserveAspectCrop);
     reader.read();

    For the fitMode, the supported values are Image.Stretch, Image.PreserveAspectFit and Image.PreserveAspectCrop.

  • var setClipRect(int x, int y, int width, int height) - Sets a clipping rectangle for the image reader.
  • var setScaledClipRect(int x, int y, int width, int height) - Sets a scaled clip rect for the image reader.
  • var setAutoTransform(bool enabled)

    Rotates the image if the metadata for the image indicates that it should.

     HttpImageUtils.createReader(imagePath)
       .setAutoTransform(true)
       .read();
  • var read(var options)

    Reads and decodes the image into memory. This function can take optional object of options, which can transcode the image to a different format and quality:

     HttpImageUtils.createReader(imagePath)
       .setScaledSize(200, 200)
       .read({
         transcode: {
           format: "jpg",
           quality: 50
         }
       });

    The function returns an image object you can attach to a request created with HttpRequest.

Method Documentation

var createReader(string filepath)

This function accepts a filepath to an image or to a base64 encoded data uri containing image data and returns an image reader. The image is not decoded at this point, only the reader is created.


Qt_Technology_Partner_RGB_475 Qt_Service_Partner_RGB_475_padded