Image Resizer Use Case
    • PDF

    Image Resizer Use Case

    • PDF

    Article summary

    Objective

    Resizing Images with Glasswall Embedded Engine

    The Image Resizer tool within the Glasswall Embedded Engine's Export/Import functionality, can be used to reduce the byte size of embedded images within documents, before reinserting them into the parent document.

    Users can also perform additional operations on these images, with the resizer helping to manage image sizes and, in some cases, reduce the overall document size.

    For details on the Embedded Engine’s export and import capabilities, visit Content Export and Import.

    This guide demonstrates how to use Embedded Engine wrappers to export and import images in a sample PDF, resize them, and reinsert them into the document.

    Learn more about Glasswall Embedded Engine API Functions

    Image Resizer User Guide

    Prerequisites

    • Glasswall Embedded Engine SDK
    • An active license with Export and Import functionality enabled
    • Required libraries for the selected Embedded Engine wrapper
    • Image Resizer CLI
    • Sample PDF File
      • This document contains 2 JPEG embedded images.

    Configuration

    The following XML file includes a policy set to sanitise all content.

    It is also configured to export images to their raw format under the sysconfig section, see export_embedded_images. For more on engine configuration info see Configuration Management.

    <?xml version="1.0" encoding="utf-8"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    	<pdfConfig>
    		<acroform>sanitise</acroform>
    		<actions_all>sanitise</actions_all>
    		<embedded_files>sanitise</embedded_files>
    		<embedded_images>sanitise</embedded_images>
    		<external_hyperlinks>sanitise</external_hyperlinks>
    		<internal_hyperlinks>sanitise</internal_hyperlinks>
    		<javascript>sanitise</javascript>
    		<metadata>sanitise</metadata>
    		<digital_signatures>sanitise</digital_signatures>
    		<value_outside_reasonable_limits>sanitise</value_outside_reasonable_limits>
    	</pdfConfig>
    	<wordConfig>
    		<embedded_files>sanitise</embedded_files>
    		<embedded_images>sanitise</embedded_images>
    		<external_hyperlinks>sanitise</external_hyperlinks>
    		<internal_hyperlinks>sanitise</internal_hyperlinks>
    		<macros>sanitise</macros>
    		<metadata>sanitise</metadata>
    		<review_comments>sanitise</review_comments>
    		<dynamic_data_exchange>sanitise</dynamic_data_exchange>
    	</wordConfig>
    	<xlsConfig>
    		<embedded_files>sanitise</embedded_files>
    		<embedded_images>sanitise</embedded_images>
    		<external_hyperlinks>sanitise</external_hyperlinks>
    		<internal_hyperlinks>sanitise</internal_hyperlinks>
    		<macros>sanitise</macros>
    		<metadata>sanitise</metadata>
    		<review_comments>sanitise</review_comments>
    		<dynamic_data_exchange>sanitise</dynamic_data_exchange>
    		<connections>sanitise</connections>
    	</xlsConfig>
    	<pptConfig>
    		<embedded_files>sanitise</embedded_files>
    		<embedded_images>sanitise</embedded_images>
    		<external_hyperlinks>sanitise</external_hyperlinks>
    		<internal_hyperlinks>sanitise</internal_hyperlinks>
    		<macros>sanitise</macros>
    		<metadata>sanitise</metadata>
    		<review_comments>sanitise</review_comments>
    	</pptConfig>
    	<tiffConfig>
    		<geotiff>sanitise</geotiff>
    	</tiffConfig>
    	<svgConfig>
    		<scripts>sanitise</scripts>
    		<foreign_objects>sanitise</foreign_objects>
    		<hyperlinks>sanitise</hyperlinks>
    	</svgConfig>
    	<webpConfig>
    		<metadata>sanitise</metadata>
    	</webpConfig>
    	<sysConfig>
    		<interchange_type>sisl</interchange_type>
    		<export_embedded_images>false</export_embedded_images>
    	</sysConfig>
    </config>
    

    Step 1 - Export

    Start by exporting the original file, ensuring you have the paths to your XML configuration file and Glasswall license ready.

    Note: these examples lack error-handling logic. For details, see Return Types.

    C#
    using glasswall_core2;
    
    var filePath = "<path-to-input-file>";
    var pathToConfig = "<path-to-config>";
    var licenseFilePath = "<path-license-file>";
    var outputDirectory = "<output-directory>";
    
    Glasswall glasswall = new Glasswall();
    
    int session = 0;
    session = glasswall.OpenSession();
    glasswall.RegisterLicenseFile(session, licenseFilePath);
    glasswall.RegisterPoliciesFile(session, pathToConfig, 0);
    
    glasswall.RegisterInputFile(session, filePath);
    glasswall.RegisterExportFile(session, Path.Combine(outputDirectory, $"export.zip"));
    glasswall.RunSession(session);
    
    glasswall.CloseSession(session);
    

    Upon successful Export of the sample file, an archive named export.zip should have been created in your chosen output directory.

    Inspecting this ZIP file should reveal:

    • Several SISL files
    • 2 JPEG images
      • 41 KB file
      • 2 KB file
    • 2 JSON files

    For more information on the export contents, refer to Export Package Content.

    Next, we'll extract the JPEG images from the ZIP file to use with the Image Resizer CLI.

    Step 2 - Resize Images

    The embedded images from our sample PDF file have now been extracted. We can proceed to resize them before reconstructing the clean file.

    For CLI usage instructions, run the -h command.

    For additional information, please refer to Glasswall Image Resizer Overview.

    Resizing an image

    Let's resize the 41 KB JPEG file, ensuring the filename stays the same. The Embedded Engine will expect JPEG files with the same names from the ZIP in the next step.

    Here, we are resizing the 41 KB extracted JPEG image from the export ZIP to a target size of 20 KB (20,000 bytes).

    mkdir output
    
    ./gw_image_resizer -i <image>.jpeg -o output/<image>.jpeg -t 20000
    

    We should now have another JPEG file with the exact same name but with a size of ~20KB.

    Replacing the image in the export zip

    Now we can replace the original 41 KB JPEG file with our resized one by dropping it into export.zip.

    Step 3 - Import

    Using the .zip file containing our resized images, let's import it back into a PDF file.

    C#
    using glasswall_core2;
    
    var exportZipPath = "<export-zip-path>";
    var pathToConfig = "<path-to-config>";
    var licenseFilePath = "<path-license-file>";
    var outputDirectory = "<output-directory>";
    
    Glasswall glasswall = new Glasswall();
    
    int session = 0;
    session = glasswall.OpenSession();
    glasswall.RegisterLicenseFile(session, licenseFilePath);
    glasswall.RegisterPoliciesFile(session, pathToConfig, 0);
    
    glasswall.RegisterImportFile(session, exportZipPath);
    glasswall.RegisterOutFile(session, Path.Combine(outputDirectory, $"Sample.Clean.pdf"));
    glasswall.RunSession(session);
    
    glasswall.CloseSession(session);
    

    The result is a sanitized sample file, Sample.Clean.Pdf, which includes the resized JPEG image.


    Was this article helpful?

    What's Next