MAS:Dynamic Image Resize

From Mansion Productions WIKI
Jump to: navigation, search

In cases where it's needed to show a MAS-created image (a face image, gallery image, additional preview image referenced with a [path] tag or even a full-res image from a gallery), but in different dimensions, you could use the following feature.

For example it comes handy when doing a mobile version of a site and the minimalistic mobile design requires the face images to be smaller than the ones you have for the normal members area (using the same content sets). Or in cases where the design of the home page is made to use a different size for face images, not the one that is used more frequently in other section or gallery pages (and defined as faceimage width/height in the gallery template). And also in cases where you want to show a bigger preview image (from protected member content) from the tour side in a secure manner, without exposing the rest of the content items.

The feature is however limited only to images originating from the MAS faceimages/, preview/ or content/ folders, and only to JPG files. As of version 2.0.9 it accepts GIF files too but without resizing - direct stream output - as a means to allow the secure display of animated GIF's from the non-logged-in tour side.

In any template, instead of refering to an image directly, with an image tag, you should put a reference to this php script with parameters. For example, instead of doing

   <img src="[imgsrc1]">

you could do

   <img src="image_resize.php?i=[imgsrc1]&w=300&h=180">


Generic syntax

<mas_url>image_resize.php?i=__path/to/image.jpg&w=x&h=y&stretching=option&upscale=no&screencolor=cccccc


  • <mas_url>
Optional parameter. The URL of the public/ subdirectory within your MAS v.2 install (e.g. http://mas2.domain.com/public/ ) (In MAS v 2.x there is also a template tag [masurl] which you could use in any section or gallery template.)
You could omit this one (and hide the URL of the MAS admin from members) if you create an additional auxiliary site file, named image_resize.php in the members/tour area directory(ies) with the following contents:
<?php @include('/path/to/mas/install/public/image_resize.php'); ?>
The /path/to/mas/install/ you could lookup from any other aux file, for example search.php, and it is common for all sites.
  • i=path/to/image.jpg

Required parameter, giving the path to the image to be displayed. Here are some examples:

i=[imgsrc1]
i=[imgsrc_new1@5]
i=[path1]/thumbs/preview01.jpg
  • w=x
  • h=y
The desired width & height of the image. You could provide both, either one, or none of them.
If you provide both, the resulting image will be made exactly that size, by applying the default (or given) stretching mode.
If only width or height provided, then the missing parameter will be calculated so that resing is done proportionally to the original image's aspect ratio.
If none provided, image will be displayed as-is, at its original dimensions but in a secure manner (suitable for site tours).
  • stretching=option
    Optional parameter controlling how to stretch or crop the image if the new given dimensions are in a different aspect ratio than the source. The values of option are the same as the ones used by the JW Player, for convenience; one of those:
    • none -- no stretching, no resizing, only crop to the given dimensions with black borders around where the image is smaller than the requested size;
    • exactfit -- disproportionate, the source will be stretched exactly to the given dimensions;
    • uniform -- stretch with black borders while keeping the original aspect ratio; the bigger side fills the given size, black (letterbox) around the smaller sides;
    • fill -- uniform, but completely fill the display; the smaller side fills the given size; the bigger one gets cropped on the sides - like MAS face images.
If the parameter is ommited, the default value is assumed exactfit.
  • upscale=yes/no
Optional parameter, with default value of yes. If given upscale=no, and the requested width/height are bigger than the source image's width/height, it will not be enlarged but filled with black, to avoid degrading of quality.
  • screencolor=cccccc
Optional parameter providing the web-value of the color to be used for background around the image, in cases when stretching is none or uniform, or upscale=no and the requested width/height are bigger than the image. By default it's black. Do not include the hash # character in front of the color code!
  • quality=85
Optional parameter, JPEG compression quality [10-100]. Default value: 85. If given as a single digit value (e.g. 7) it is automatically assumed multiplied by ten (assume 70).
  • sharpen=yes/no
Optional parameter, with default value of yes. If set, the resized image will be filtered through UnsharpMask(amount=80, radius=0.5, threshold=3). The filter values are hardcoded as of the current version, eventually to be moved in global configuration at a later point.


Examples:

image_resize.php?i=[imgsrc1]&w=300&h=150&stretching=uniform&screencolor=EEAA77
image_resize.php?i=[imgsrc_new1@5]&w=800&h=600&upscale=no
image_resize.php?i=[path1]/thumbs/preview01.jpg&w=150


Image Cache

The image_resize script is caching the requested images, so that actual resizing only happens the first time an image is called with different parameters, to minimize the server's CPU load. The resized image is then saved under the directory /cache/image_resize/ within the MAS installation and subsequent requests to the same image with the same size and other parameters return directly the cached image. The cache exprires automatically with the beginning of each month, to minimize the amount of disk space used by old images.

To force flush the cache manually, log in with FTP and browse to the MAS directory, go to /cache/image_resize/ and find there a numeric subdirectory named after the current date. Rename it to anything else (for example 0) - and with the next request for an image MAS will remove it, then start building the cache again upon request.


Secure Token

The image_resize script can be called only from within MAS templates so that MAS will find the image_resize.php reference and append a secure token to it - making it impossible to any user to modify the image path in the URI and gain access to another image and not the one intended by the webmaster. This makes the script an ideal tool to display bigger preview images (from protected member content) on the tour side in a secure manner, without exposing the rest of the content items. For example it works very well in the Images subtemplate of a tour-version of a gallery template with enabled option [x] Show selected images only or when using the clause <!-- [SkipContent:1,10] -->. E.g.:

<a href="image_resize.php?i=[directlink]" rel="hiresgal"><img src="[thumb]" alt="" /></a>

Personal tools
Namespaces

Variants
Actions
Products
Navigation
Toolbox