středa 15. února 2012

Resourcespace - resize original

We use ResourceSpace as a photo gallery, where people from our company post their photos. As we don't need the photos to be high resolution, and we are space limited, we started to look for a way to resize the images. RS doesn't offer this funcionality (it never touches the original files), so I created a plugin for resizing the resources either on upload, or later in the Team center.

Imagick is required for this to work. You need to add these lines to your include/config.php

# resize on upload
$resize_original_on_upload=false;
# resize plugin
$resize_original=true;
# extensions to be resized
$resize_original_allowed_ext=array("jpg","png","tiff","tif");
# longer side dimension
$resize_original_max="2000";


For the resize on upload, open include/image_processing.php, and after these lines:

$identoutput=run_command($identcommand);
preg_match('/^([0-9]+)x([0-9]+)$/ims',$identoutput,$smatches);
if ((@list(,$sw,$sh) = $smatches)===false) { return false; }


insert the code for resizing:
global $resize_original_on_upload, $resize_original_allowed_ext, $resize_original_max;
//to be changed to $path=$file once debugged
$path=$file;
//check if resize_original
if ($resize_original_on_upload) {
if (file_exists($file)) {
//check extension
$path_info= pathinfo($file);
if (in_array($path_info['extension'],$resize_original_allowed_ext)) {
//get dimensions
$image=new Imagick($file);
//check if one of the dimensions is longer than resize_original_max
if (($resize_original_max < $sh) || ($resize_original_max < $sw) ) {
//find longer side and scale the image
if ($sw > $sh) {
$image -> scaleImage($resize_original_max,0);}
else {
$image -> scaleImage(0,$resize_original_max);}
//save the image
$image -> writeImage($path);
//get new dimensions
$e = $image->identifyImage($path);
$sh=$e['geometry']['height'];
$sw=$e['geometry']['width'];
}}}}
If you prefer to resize later, download the plugin, put it into your plugins folder and activate it. When you go to Team center, an option "Resize original images" should appear. Click it, you get number of resources to be resized, "this cannot be undone" warning and link to resize images. Click, the resources should get resized.

What the plugin does: it finds resources with one side longer than $resize_original_max, resizes them, and writes new dimensions back to db. One glitch here: the exif data is not touched, so you probably get mismatch between exif data and info in db.

Feel free to use this anyway you like, suggestions and patches are welcome.

Žádné komentáře:

Okomentovat