How to limit image size when uploading
When images are uploaded to the server, you may wish to limit the image size stored on the server, because in most cases, image sizes shown in web pages don't need to have maximum resolution.
You may argue that it doesn't really matter, since we have the Image Processor serving the size that's right for the occasion, but think about storage: when storing the images in personalization or global storage for instance, the space taken does matter, if you don't want the database to explode.
Now, the image macro in Smartsite iXperion 1.4 has gotten a very nice feature: you can set the resulttype to DataTable. This way, the macro doesn't output an image HTML tag, but wraps the image inside a DataTable for storage.
To do this, create a new Translation:
Smartsite SXML | Copy Code |
---|---|
DataTable Resize_Image(DataTable image, integer maxWidth, integer maxHeight) |
Now, add the following replacement SXML:
XML | Copy Code |
---|---|
<se:image resulttype="datatable" filetype="jpg" save="data" jpgquality="95" inputdata="{translation.arg(image)}" maxwidth="{translation.arg(maxWidth, default=640)}" maxheight="{translation.arg(maxHeight, default=480)}" /> {translation.setresult($data)} |
There you are. Now, use the translation to capture files which are being uploaded.
The following snippet is taken from an adapted version of the ShowForm() translation of the 'My Profile Smartlet' (personalization module):
Smartsite SXML | Copy Code |
---|---|
<se:format inputdata="{request.files()}" > <se:rowformat match="first"> {personalization.set('', Avatar, resize_image(request.getfiledata(this.field(key)), 300, 200))} </se:rowformat> </se:format> |
Ready! Images for a user's profile are only shown in very small thumbnails, so why store big bloated images?