0

AImage content: how to zip content

asked 2017-02-17 10:25:26 +0800

dis gravatar image dis flag of Switzerland
140 4

Hi there

I am using following code to display generated SVG images (charts) to an org.zkoss.zul.Image:

  AImage aImage = new AImage("chart1.svg", data);
  chartImage.setContent(aImage);

It works perfectly! The problem is that the generated SVG image may become very large. I could see with Firebug Network Analyzer that the content of the SVG image is NOT zipped while transferring from server to browser.

Is there a way to force zipping the AImage content? SVG images are actually perfect to zip (XML content).

Thank you Best Regards Dieter

delete flag offensive retag edit

3 Answers

Sort by ยป oldest newest most voted
1

answered 2017-02-23 01:24:12 +0800

cor3000 gravatar image cor3000
3446 1 7
ZK Team

updated 2017-02-23 01:29:06 +0800

I had a second look and found a tomcat configuration that seems to work:

in server.xml add the 3 compressionXXX parameters which will enable compression of svg responses. I didn't enable any other mime types since ZK enabling gzip compression by default for many other resource responses (css/js/zul) - to avoid double gzipping (see configuration reference on ZK AU Engine, ZK Loader and if enabled DSP Loader).

<Connector port="8080" protocol="HTTP/1.1"
    connectionTimeout="20000"
    redirectPort="8443"
    compression="on"
    compressableMimeType="image/svg+xml"
    compressionMinSize="0"
    />

I tested the following zul file on a tomcat 8 with the configuration above:

<zk>
    <image id="chartImage"></image>
    <zscript><![CDATA[
        import org.zkoss.image.AImage;
        import org.zkoss.util.media.AMedia;
        AImage aImage = new AImage("chart1.svg", "<svg xmlns=\"http://www.w3.org/2000/svg\" height=\"100\" width=\"100\"><circle cx=\"50\" cy=\"50\" r=\"40\" stroke=\"black\" stroke-width=\"3\" fill=\"red\"/></svg>".getBytes());
        desktop.getDynamicMediaURI(chartImage, "chart1.svg");
        chartImage.setContent(aImage);
    ]]></zscript>
</zk>

This url was generated:

Request URL:http://localhost:8080/mytestapp/zkau/view/z_2zl/dMAP0/hhh1/1/c/chart1.svg

As you can see in the headers the compression was enabled:

HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Accept-Ranges: bytes
Content-Type: image/svg+xml;charset=UTF-8
Content-Language: en-US
Transfer-Encoding: chunked
Content-Encoding: gzip
Vary: Accept-Encoding
Date: Thu, 23 Feb 2017 01:09:58 GMT

I hope this information (even if only tomcat specific) helps for your application.

Update: another (less global) way is to implement a Servlet Filter with a filter-mapping for only /zkau/view/* and then apply the compression manually only when needed based on your own specific rules.

Robert

link publish delete flag offensive edit

Comments

0

answered 2017-02-22 06:53:18 +0800

Darksu gravatar image Darksu
1976 1 4

Hello dis,

I found some interesting articles based on your idea. Basically you enable compression on your application server as shown below:

http://viralpatel.net/blogs/enable-gzip-compression-in-tomcat/

For more info please refer to the following:

https://betterexplained.com/articles/how-to-optimize-your-site-with-gzip-compression/

Best Regards,

Darksu

link publish delete flag offensive edit

Comments

zk enables compression by default for specific responses since images as gif/png/jpg are already compressed by default: see https://www.zkoss.org/wiki/ZKConfigurationReference/web.xml/ZKAUEngine.

cor3000 ( 2017-02-22 07:19:38 +0800 )edit

I guess it disables compression for all images by default. I just need to find the time to check if exceptions can be configured.

cor3000 ( 2017-02-22 07:20:15 +0800 )edit

should be possible by adding the svg mime type (image/svg+xml) to the list of compressible mime types by your application server. Which application server are you using: here the docs for tomcat https://tomcat.apache.org/tomcat-8.0-doc/config/http.html#Standard_Implementation

cor3000 ( 2017-02-22 08:39:59 +0800 )edit

I tested the tomcat configuration myself, and that's not doing what I expected. So for now I must say I don't know a direct way to achieve, that easily without a custom component.

cor3000 ( 2017-02-22 12:35:28 +0800 )edit
0

answered 2017-02-23 10:04:11 +0800

dis gravatar image dis flag of Switzerland
140 4

Hello Robert

Thank you for your solution. This works perfectly for our Tomcat installations. We use also an embedded Jetty server in production. It works similar for Jetty: a GzipHandler must be added as an additional handler.

Dieter

link publish delete flag offensive edit

Comments

glad this helps. Just be careful to avoid double-gzipping a response

cor3000 ( 2017-02-23 10:50:16 +0800 )edit
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow
1 follower

RSS

Stats

Asked: 2017-02-17 10:25:26 +0800

Seen: 36 times

Last updated: Feb 23