API: Script: Image Compressor

Trott edited this page Jan 14, 2012 · 6 revisions


The framework provides a script that will compress an image to ideal dimensions based on the user's device. This feature allows one to provide a single image and let the framework scale it dynamically for the end user so that a high-resolution and low-resolution device both need only download an image optimized for their screen size.


Load times prove a delimiting factor to the mobile browsing experience; as such, a script should exist that enables sites to compress images through the framework just-in-time.


The framework provides a script that can be run off of {MOBILE_DOMAIN} to compress images suited for a user's device.


It leverages caching policies to ensure that it does not, except when necessary, have to recompress the same image of the same size multiple times. In addition, it provides three different mechanisms by which a content provider can inform the script of compression:

  • browser_width_force & browser_height_force - These parameters do not take an attribute but instead merely inform the script to compress the image to a maximum width or height equal to the user's browsing device.
  • browser_width_percent & browser_height_percent - These parameters take a value between 1 and 100 and inform the script to compress the image to a maximum width or height equal to the percent specified of the user's browsing agent.
  • max_width & max_height - These parameters do not take into consideration the user's browsing device but instead inform the script to compress the image to a maximum height or width specified in pixels by this parameter. In terms of maximum size, the browser_width_percent and browser_height_percent parameters overrule the browser_width_force and browser_height_force parameters and the max_width and max_height parameters overrule both other sets.

In this example, the image will never be larger than 400 pixels when compressed and in addition will also never be larger than 80% width and 60% height of the user's browsing device.

<img src="http://m.ucla.edu/assets/min/img.php?img=http%3A%2F%2Fm.ucla.edu%2Fsomeimage.jpg
&browser_width_percent=80&browser_height_percent=60&max_width=400" />


The image compression script caching support is a work in progress. The framework development group is revisiting the caching policies which currently have an inordinately long retention period.

There is a bug in Android 2.2 and 2.3 such that the screen dimensions are not reported reliably. MWF can't reliably determine screen width if the OS/browser won't reliably report it. Users of the image minifier are especially encouraged to confirm that results are acceptable in Android 2.2 or 2.3, and to provide a fallback using CSS or other techniques in cases where the minifier cannot provide a satisfactory scaling on those devices.

Clone this wiki locally
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.