Updated on 2023-11-09 GMT+08:00

Processing an Image

If you have any questions during development, post them on the Issues page of GitHub.

OBS can be used to process images in a stable, secure, efficient, easy-of-use, and cost-efficient manner. If the object to be downloaded is an image, you can pass the image processing parameters to process the image, including cutting and resizing it as well as putting a watermark and converting the format.

For more information, see Image Processing Feature Guide.

Sample code:

static void test_get_object_image()
{
    // Create and initialize option.
    obs_options option;
    init_obs_options(&option);
    option.bucket_options.host_name = "<your-endpoint>";
    option.bucket_options.bucket_name = "<Your bucketname>";

    // Hard-coded or plaintext AK/SK are risky. For security purposes, encrypt your AK/SK and store them in the configuration file or environment variables. In this example, the AK/SK are stored in environment variables for identity authentication. Before running this example, configure environment variables ACCESS_KEY_ID and SECRET_ACCESS_KEY.
    // Obtain an AK/SK pair on the management console. For details, see https://support.huaweicloud.com/intl/en-us/usermanual-ca/ca_01_0003.html.
    option.bucket_options.access_key = getenv("ACCESS_KEY_ID");
    option.bucket_options.secret_access_key = getenv("SECRET_ACCESS_KEY");
    // Image transcoding configuration
    obs_get_conditions getcondition;
    memset(&getcondition, 0, sizeof(obs_get_conditions));
    init_get_properties(&getcondition);
    getcondition.image_process_config.image_process_mode = obs_image_process_cmd;
    getcondition.image_process_config.cmds_stylename = "resize,m_fixed,w_100,h_100/rotate,90";
    // Download object information.
    obs_object_info object_info;
    memset(&object_info, 0, sizeof(obs_object_info));
    object_info.key = "<object key>";
    object_info.version_id = "<object version ID>";
    // Download object callback data.
    get_object_callback_data data;
    data.ret_status = OBS_STATUS_BUTT;
    data.outfile = write_to_file("<file path>");
    // Set response callback function.
    obs_get_object_handler getobjectHandler =
    { 
        { &response_properties_callback, &get_object_complete_callback},
        &get_object_data_callback
    };
     
    // Download an object.
    get_object(&option,&object_info,&getcondition,0,&getobjectHandler,&data);
    fclose(data.outfile);
     if (OBS_STATUS_OK == data.ret_status) {
            printf("get object successfully. \n");
        }
    else
    {
        printf("get object faied(%s).\n", obs_get_status_name(data.ret_status));
    }
}
  • Use getcondition.image_process_config to specify the image processing parameters.
  • Currently, image processing parameters can only be processed in the image/commands format.
  • Image processing parameters can be processed in cascading mode. This indicates that multiple commands can be performed on an image in sequence.