mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-07-31 17:39:05 +00:00
Updated image transformers to have formalised parameters, support for resize image transformations, get and update thumbnail implementation
git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@8779 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
@@ -71,8 +71,17 @@ public class BinaryPassThroughContentTransformer extends AbstractContentTransfor
|
||||
}
|
||||
else
|
||||
{
|
||||
// formats are the same and are not text
|
||||
return true;
|
||||
if (BinaryPassThroughContentTransformer.class == options.getClass())
|
||||
{
|
||||
// formats are the same and are not text
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
// If it has meaningful options then we assume there is another transformer better equiped
|
||||
// to deal with it
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -24,12 +24,11 @@
|
||||
*/
|
||||
package org.alfresco.repo.content.transform;
|
||||
|
||||
import org.alfresco.repo.content.transform.magick.ImageMagickContentTransformerTest;
|
||||
import org.alfresco.repo.content.transform.magick.JMagickContentTransformerTest;
|
||||
|
||||
import junit.framework.Test;
|
||||
import junit.framework.TestSuite;
|
||||
|
||||
import org.alfresco.repo.content.transform.magick.ImageMagickContentTransformerTest;
|
||||
|
||||
|
||||
/**
|
||||
* Version test suite
|
||||
@@ -60,7 +59,6 @@ public class TransformTestSuite extends TestSuite
|
||||
suite.addTestSuite(TextMiningContentTransformerTest.class);
|
||||
suite.addTestSuite(TextToPdfContentTransformerTest.class);
|
||||
suite.addTestSuite(ImageMagickContentTransformerTest.class);
|
||||
suite.addTestSuite(JMagickContentTransformerTest.class);
|
||||
return suite;
|
||||
}
|
||||
}
|
||||
|
@@ -54,6 +54,9 @@ public class ImageMagickContentTransformer extends AbstractImageMagickContentTra
|
||||
/** the system command executer */
|
||||
private RuntimeExec executer;
|
||||
|
||||
/**
|
||||
* Default constructor
|
||||
*/
|
||||
public ImageMagickContentTransformer()
|
||||
{
|
||||
}
|
||||
@@ -100,7 +103,14 @@ public class ImageMagickContentTransformer extends AbstractImageMagickContentTra
|
||||
// set properties
|
||||
if (options instanceof ImageTransformationOptions)
|
||||
{
|
||||
properties.put(KEY_OPTIONS, (String) ((ImageTransformationOptions)options).getCommandOptions());
|
||||
ImageTransformationOptions imageOptions = (ImageTransformationOptions)options;
|
||||
ImageResizeOptions resizeOptions = imageOptions.getResizeOptions();
|
||||
String commandOptions = imageOptions.getCommandOptions();
|
||||
if (resizeOptions != null)
|
||||
{
|
||||
commandOptions = commandOptions + " " + getImageResizeCommandOptions(resizeOptions);
|
||||
}
|
||||
properties.put(KEY_OPTIONS, commandOptions);
|
||||
}
|
||||
properties.put(VAR_SOURCE, sourceFile.getAbsolutePath());
|
||||
properties.put(VAR_TARGET, targetFile.getAbsolutePath());
|
||||
@@ -117,4 +127,47 @@ public class ImageMagickContentTransformer extends AbstractImageMagickContentTra
|
||||
logger.debug("ImageMagic executed successfully: \n" + executer);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the imagemagick command string for the image resize options provided
|
||||
*
|
||||
* @param imageResizeOptions image resize options
|
||||
* @return String the imagemagick command options
|
||||
*/
|
||||
private String getImageResizeCommandOptions(ImageResizeOptions imageResizeOptions)
|
||||
{
|
||||
StringBuilder builder = new StringBuilder(32);
|
||||
|
||||
if (imageResizeOptions.isResizeToThumbnail() == true)
|
||||
{
|
||||
builder.append("-thumbnail ");
|
||||
}
|
||||
else
|
||||
{
|
||||
builder.append("-resize ");
|
||||
}
|
||||
|
||||
if (imageResizeOptions.getWidth() > -1)
|
||||
{
|
||||
builder.append(imageResizeOptions.getWidth());
|
||||
}
|
||||
|
||||
if (imageResizeOptions.getHeight() > -1)
|
||||
{
|
||||
builder.append("x");
|
||||
builder.append(imageResizeOptions.getHeight());
|
||||
}
|
||||
|
||||
if (imageResizeOptions.isPercentResize() == true)
|
||||
{
|
||||
builder.append("%");
|
||||
}
|
||||
|
||||
if (imageResizeOptions.isMaintainAspectRatio() == false)
|
||||
{
|
||||
builder.append("!");
|
||||
}
|
||||
|
||||
return builder.toString();
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,105 @@
|
||||
/*
|
||||
* Copyright (C) 2005-2007 Alfresco Software Limited.
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation; either version 2
|
||||
* of the License, or (at your option) any later version.
|
||||
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
|
||||
* As a special exception to the terms and conditions of version 2.0 of
|
||||
* the GPL, you may redistribute this Program in connection with Free/Libre
|
||||
* and Open Source Software ("FLOSS") applications as described in Alfresco's
|
||||
* FLOSS exception. You should have recieved a copy of the text describing
|
||||
* the FLOSS exception, and it is also available here:
|
||||
* http://www.alfresco.com/legal/licensing"
|
||||
*/
|
||||
package org.alfresco.repo.content.transform.magick;
|
||||
|
||||
/**
|
||||
* Image resize options
|
||||
*
|
||||
* @author Roy Wetherall
|
||||
*/
|
||||
public class ImageResizeOptions
|
||||
{
|
||||
/** The width */
|
||||
private int width = -1;
|
||||
|
||||
/** The height */
|
||||
private int height = -1;
|
||||
|
||||
/** Indicates whether the aspect ratio of the image should be maintained */
|
||||
private boolean maintainAspectRatio = true;
|
||||
|
||||
/** Indicates whether this is a percentage resize */
|
||||
private boolean percentResize = false;
|
||||
|
||||
/** Indicates whether the resized image is a thumbnail */
|
||||
private boolean resizeToThumbnail = false;
|
||||
|
||||
/**
|
||||
* Defatult constructor
|
||||
*/
|
||||
public ImageResizeOptions()
|
||||
{
|
||||
}
|
||||
|
||||
public void setWidth(int width)
|
||||
{
|
||||
this.width = width;
|
||||
}
|
||||
|
||||
public int getWidth()
|
||||
{
|
||||
return width;
|
||||
}
|
||||
|
||||
public void setHeight(int height)
|
||||
{
|
||||
this.height = height;
|
||||
}
|
||||
|
||||
public int getHeight()
|
||||
{
|
||||
return height;
|
||||
}
|
||||
|
||||
public void setMaintainAspectRatio(boolean maintainAspectRatio)
|
||||
{
|
||||
this.maintainAspectRatio = maintainAspectRatio;
|
||||
}
|
||||
|
||||
public boolean isMaintainAspectRatio()
|
||||
{
|
||||
return maintainAspectRatio;
|
||||
}
|
||||
|
||||
public void setPercentResize(boolean percentResize)
|
||||
{
|
||||
this.percentResize = percentResize;
|
||||
}
|
||||
|
||||
public boolean isPercentResize()
|
||||
{
|
||||
return percentResize;
|
||||
}
|
||||
|
||||
public void setResizeToThumbnail(boolean resizeToThumbnail)
|
||||
{
|
||||
this.resizeToThumbnail = resizeToThumbnail;
|
||||
}
|
||||
|
||||
public boolean isResizeToThumbnail()
|
||||
{
|
||||
return resizeToThumbnail;
|
||||
}
|
||||
}
|
@@ -24,22 +24,146 @@
|
||||
*/
|
||||
package org.alfresco.repo.content.transform.magick;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.alfresco.service.cmr.repository.NodeRef;
|
||||
import org.alfresco.service.cmr.repository.NodeService;
|
||||
import org.alfresco.service.cmr.repository.TransformationOptions;
|
||||
import org.alfresco.service.namespace.NamespaceService;
|
||||
import org.alfresco.service.namespace.QName;
|
||||
|
||||
/**
|
||||
* Image transformation options
|
||||
*
|
||||
* @author Roy Wetherall
|
||||
*/
|
||||
public class ImageTransformationOptions extends TransformationOptions
|
||||
{
|
||||
/** imageTransformOptions aspect details */
|
||||
public static final QName ASPECT_IMAGE_TRANSFORATION_OPTIONS = QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, "imageTransformationOptions");
|
||||
public static final QName PROP_COMMAND_OPTIONS = QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, "commandOptions");
|
||||
public static final QName PROP_RESIZE = QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, "resize");
|
||||
public static final QName PROP_RESIZE_WIDTH = QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, "resizeWidth");
|
||||
public static final QName PROP_RESIZE_HEIGHT = QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, "resizeHeight");
|
||||
public static final QName PROP_RESIZE_MAINTAIN_ASPECT_RATIO = QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, "resizeMaintainAspectRatio");
|
||||
public static final QName PROP_RESIZE_PERCENT = QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, "resizePercent");
|
||||
public static final QName PROP_RESIZE_TO_THUMBNAIL = QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, "resizeToThumbnail");
|
||||
|
||||
/** Command string options, provided for backward compatibility */
|
||||
private String commandOptions = "";
|
||||
|
||||
/** Image resize options */
|
||||
private ImageResizeOptions resizeOptions;
|
||||
|
||||
/**
|
||||
* Set the command string options
|
||||
*
|
||||
* @param commandOptions the command string options
|
||||
*/
|
||||
public void setCommandOptions(String commandOptions)
|
||||
{
|
||||
this.commandOptions = commandOptions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the command string options
|
||||
*
|
||||
* @return String the command string options
|
||||
*/
|
||||
public String getCommandOptions()
|
||||
{
|
||||
return commandOptions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the image resize options
|
||||
*
|
||||
* @param resizeOptions image resize options
|
||||
*/
|
||||
public void setResizeOptions(ImageResizeOptions resizeOptions)
|
||||
{
|
||||
this.resizeOptions = resizeOptions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the image resize options
|
||||
*
|
||||
* @return ImageResizeOptions image resize options
|
||||
*/
|
||||
public ImageResizeOptions getResizeOptions()
|
||||
{
|
||||
return resizeOptions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Save the transformation options to the ImageTransformationOptions aspect.
|
||||
*
|
||||
* @see org.alfresco.service.cmr.repository.TransformationOptions#saveToNode(org.alfresco.service.cmr.repository.NodeRef, org.alfresco.service.cmr.repository.NodeService)
|
||||
*/
|
||||
@Override
|
||||
public void saveToNode(NodeRef nodeRef, NodeService nodeService)
|
||||
{
|
||||
super.saveToNode(nodeRef, nodeService);
|
||||
|
||||
// Create a list of the properties
|
||||
Map<QName, Serializable> properties = new HashMap<QName, Serializable>(7);
|
||||
properties.put(PROP_COMMAND_OPTIONS, this.commandOptions);
|
||||
if (this.resizeOptions != null)
|
||||
{
|
||||
properties.put(PROP_RESIZE, true);
|
||||
properties.put(PROP_RESIZE_HEIGHT, this.resizeOptions.getHeight());
|
||||
properties.put(PROP_RESIZE_WIDTH, this.resizeOptions.getWidth());
|
||||
properties.put(PROP_RESIZE_MAINTAIN_ASPECT_RATIO, this.resizeOptions.isMaintainAspectRatio());
|
||||
properties.put(PROP_RESIZE_PERCENT, this.resizeOptions.isPercentResize());
|
||||
properties.put(PROP_RESIZE_TO_THUMBNAIL, this.resizeOptions.isResizeToThumbnail());
|
||||
}
|
||||
else
|
||||
{
|
||||
properties.put(PROP_RESIZE, false);
|
||||
}
|
||||
|
||||
// Add the aspect
|
||||
nodeService.addAspect(nodeRef, ASPECT_IMAGE_TRANSFORATION_OPTIONS, properties);
|
||||
}
|
||||
|
||||
/**
|
||||
* Populate the image transformation options from the node provided.
|
||||
*
|
||||
* @see org.alfresco.service.cmr.repository.TransformationOptions#populateFromNode(org.alfresco.service.cmr.repository.NodeRef, org.alfresco.service.cmr.repository.NodeService)
|
||||
*/
|
||||
@Override
|
||||
public void populateFromNode(NodeRef nodeRef, NodeService nodeService)
|
||||
{
|
||||
super.populateFromNode(nodeRef, nodeService);
|
||||
|
||||
// Check whether the node has the image transformation options aspect
|
||||
if (nodeService.hasAspect(nodeRef, ASPECT_IMAGE_TRANSFORATION_OPTIONS) == true)
|
||||
{
|
||||
// Get the node's properties
|
||||
Map<QName, Serializable> properties = nodeService.getProperties(nodeRef);
|
||||
|
||||
// Set the properties
|
||||
this.commandOptions = (String)properties.get(PROP_COMMAND_OPTIONS);
|
||||
|
||||
// Set the resize properties
|
||||
Boolean isResize = (Boolean)properties.get(PROP_RESIZE);
|
||||
if (isResize.booleanValue() == true)
|
||||
{
|
||||
int height = ((Long)properties.get(PROP_RESIZE_HEIGHT)).intValue();
|
||||
int width = ((Long)properties.get(PROP_RESIZE_WIDTH)).intValue();
|
||||
boolean maintainAspectRatio = ((Boolean)properties.get(PROP_RESIZE_MAINTAIN_ASPECT_RATIO)).booleanValue();
|
||||
boolean percentResize = ((Boolean)properties.get(PROP_RESIZE_PERCENT)).booleanValue();
|
||||
boolean resizeToThumbnail = ((Boolean)properties.get(PROP_RESIZE_TO_THUMBNAIL)).booleanValue();
|
||||
|
||||
this.resizeOptions = new ImageResizeOptions();
|
||||
this.resizeOptions.setHeight(height);
|
||||
this.resizeOptions.setWidth(width);
|
||||
this.resizeOptions.setMaintainAspectRatio(maintainAspectRatio);
|
||||
this.resizeOptions.setPercentResize(percentResize);
|
||||
this.resizeOptions.setResizeToThumbnail(resizeToThumbnail);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -1,66 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2005-2007 Alfresco Software Limited.
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation; either version 2
|
||||
* of the License, or (at your option) any later version.
|
||||
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
|
||||
* As a special exception to the terms and conditions of version 2.0 of
|
||||
* the GPL, you may redistribute this Program in connection with Free/Libre
|
||||
* and Open Source Software ("FLOSS") applications as described in Alfresco's
|
||||
* FLOSS exception. You should have recieved a copy of the text describing
|
||||
* the FLOSS exception, and it is also available here:
|
||||
* http://www.alfresco.com/legal/licensing"
|
||||
*/
|
||||
package org.alfresco.repo.content.transform.magick;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import magick.ImageInfo;
|
||||
import magick.MagickImage;
|
||||
|
||||
import org.alfresco.service.cmr.repository.TransformationOptions;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
/**
|
||||
* Makes use of the {@link http://www.textmining.org/ TextMining} library to
|
||||
* perform conversions from MSWord documents to text.
|
||||
*
|
||||
* @author Derek Hulley
|
||||
*/
|
||||
public class JMagickContentTransformer extends AbstractImageMagickContentTransformer
|
||||
{
|
||||
@SuppressWarnings("unused")
|
||||
private static final Log logger = LogFactory.getLog(JMagickContentTransformer.class);
|
||||
|
||||
public JMagickContentTransformer()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Uses the <b>JMagick</b> library to perform the transformation
|
||||
*
|
||||
* @param sourceFile
|
||||
* @param targetFile
|
||||
* @throws Exception
|
||||
*/
|
||||
@Override
|
||||
protected void transformInternal(File sourceFile, File targetFile, TransformationOptions options) throws Exception
|
||||
{
|
||||
ImageInfo imageInfo = new ImageInfo(sourceFile.getAbsolutePath());
|
||||
MagickImage image = new MagickImage(imageInfo);
|
||||
image.setFileName(targetFile.getAbsolutePath());
|
||||
image.writeImage(imageInfo);
|
||||
}
|
||||
}
|
@@ -1,70 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2005-2007 Alfresco Software Limited.
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation; either version 2
|
||||
* of the License, or (at your option) any later version.
|
||||
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
|
||||
* As a special exception to the terms and conditions of version 2.0 of
|
||||
* the GPL, you may redistribute this Program in connection with Free/Libre
|
||||
* and Open Source Software ("FLOSS") applications as described in Alfresco's
|
||||
* FLOSS exception. You should have recieved a copy of the text describing
|
||||
* the FLOSS exception, and it is also available here:
|
||||
* http://www.alfresco.com/legal/licensing"
|
||||
*/
|
||||
package org.alfresco.repo.content.transform.magick;
|
||||
|
||||
import org.alfresco.repo.content.MimetypeMap;
|
||||
import org.alfresco.repo.content.transform.AbstractContentTransformerTest;
|
||||
import org.alfresco.repo.content.transform.ContentTransformer;
|
||||
import org.alfresco.service.cmr.repository.TransformationOptions;
|
||||
|
||||
/**
|
||||
* @see org.alfresco.repo.content.transform.magick.JMagickContentTransformer
|
||||
*
|
||||
* @author Derek Hulley
|
||||
*/
|
||||
public class JMagickContentTransformerTest extends AbstractContentTransformerTest
|
||||
{
|
||||
private JMagickContentTransformer transformer;
|
||||
|
||||
@Override
|
||||
public void setUp() throws Exception
|
||||
{
|
||||
super.setUp();
|
||||
|
||||
transformer = new JMagickContentTransformer();
|
||||
transformer.setMimetypeService(mimetypeService);
|
||||
transformer.init();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Returns the same transformer regardless - it is allowed
|
||||
*/
|
||||
protected ContentTransformer getTransformer(String sourceMimetype, String targetMimetype)
|
||||
{
|
||||
return transformer;
|
||||
}
|
||||
|
||||
public void testReliability() throws Exception
|
||||
{
|
||||
if (!transformer.isAvailable())
|
||||
{
|
||||
return;
|
||||
}
|
||||
boolean reliability = transformer.isTransformable(MimetypeMap.MIMETYPE_IMAGE_GIF, MimetypeMap.MIMETYPE_TEXT_PLAIN, new TransformationOptions());
|
||||
assertEquals("Mimetype should not be supported", false, reliability);
|
||||
reliability = transformer.isTransformable(MimetypeMap.MIMETYPE_IMAGE_GIF, MimetypeMap.MIMETYPE_IMAGE_JPEG, new TransformationOptions());
|
||||
assertEquals("Mimetype should be supported", true, reliability);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user