In this small tutorial, you will see how to turn your image for example from a DMS in a SAP ECC system to a new image size. This allows you to simply publish a smaller image size to your client for example via an OData service and improves therefore the load speed.
To be able to use the snippets from below, you already need to read the image as a XString from your DMS with the function modules CV_* or similar.
Resize method
The get_thumbnail method uses the standard helper class cl_fxs_image_processor to resize later our incoming image. This method can be called statically and should be public.
Signature
For our method, I also have created here an image structure with the following fields in it. This allows us later to simply transfer also mime type and the filename next to the content.
Then we can use the following simple signature in our method, which just imports and returns the same structure, but with the modified, resized image.
METHOD get_thumbnail.
DATA: lo_image_processor TYPE REF TO cl_fxs_image_processor,
lv_image_height TYPE i,
lv_image_width TYPE i.
rs_image = is_image.
lo_image_processor = NEW cl_fxs_image_processor( ).
lo_image_processor->add_image(
iv_data = is_image-content
iv_image_name = CONV #( is_image-filename )
).
lo_image_processor->get_info(
EXPORTING
iv_handle = 1 " Loading first image
IMPORTING
ev_xres = lv_image_width
ev_yres = lv_image_height
).
get_thumbnail_size(
EXPORTING
iv_width = lv_image_width
iv_height = lv_image_height
IMPORTING
ev_new_width = lv_image_width
ev_new_height = lv_image_height
).
lo_image_processor->resize(
EXPORTING
iv_handle = 1
iv_xres = lv_image_width
iv_yres = lv_image_height
).
rs_image-content = lo_image_processor->get_image( iv_handle = 1 ).
ENDMETHOD.
Get the correct height and width
To obtain the correct height and width from a ratio, simply multiply the desired dimension by the ratio value. This straightforward calculation ensures that the image maintains its proper proportions and looks visually balanced.
The method itself should also be static but can be private.
Signature
METHOD get_thumbnail_size.
DATA: lv_aspect_ratio TYPE f,
lv_thumb_aspect_ratio TYPE f.
lv_aspect_ratio = iv_width / iv_height.
lv_thumb_aspect_ratio = iv_max_width / iv_max_height.
IF lv_aspect_ratio > lv_thumb_aspect_ratio.
ev_new_width = iv_max_width.
ev_new_height = iv_max_width / lv_aspect_ratio.
ELSE.
ev_new_height = iv_max_height.
ev_new_width = iv_max_height * lv_aspect_ratio.
ENDIF.
ENDMETHOD.
Result
Now when we pass an image into our image (LF_CONTENT) get_thumbnail method, we do see just by viewing the XString length, that we have minified our image to a maximum of 200px for the longest side. Our result is then inside the structure rs_thumbnail.
Leave a Reply