📄 cmsimagescaler.java
字号:
// select the "right" method of scaling according to the "t" parameter
case 1:
// thumbnail generation mode (like 0 but no image enlargement)
image = scaler.resize(image, getWidth(), getHeight(), color, getPosition(), false);
break;
case 2:
// scale to exact target size, crop what does not fit
image = scaler.resize(image, getWidth(), getHeight(), getPosition());
break;
case 3:
// scale and keep image propotions, target size variable
image = scaler.resize(image, getWidth(), getHeight(), true);
break;
case 4:
// don't keep image propotions, use exact target size
image = scaler.resize(image, getWidth(), getHeight(), false);
break;
default:
// scale to exact target size with background padding
image = scaler.resize(image, getWidth(), getHeight(), color, getPosition(), true);
}
if (!m_filters.isEmpty()) {
Rectangle targetSize = scaler.applyFilterDimensions(getWidth(), getHeight());
image = scaler.resize(
image,
(int)targetSize.getWidth(),
(int)targetSize.getHeight(),
Simapi.COLOR_TRANSPARENT,
Simapi.POS_CENTER);
image = scaler.applyFilters(image);
}
// get the byte result for the scaled image
result = scaler.getBytes(image, imageType);
} catch (Exception e) {
if (LOG.isDebugEnabled()) {
LOG.debug(Messages.get().getBundle().key(Messages.ERR_UNABLE_TO_SCALE_IMAGE_2, rootPath, toString()), e);
}
}
return result;
}
/**
* Returns a scaled version of the given image file according this image scalers parameters.<p>
*
* @param file the image file to scale
*
* @return a scaled version of the given image file according to the provided scaler parameters
*/
public byte[] scaleImage(CmsFile file) {
return scaleImage(file.getContents(), file.getRootPath());
}
/**
* Sets the color.<p>
*
* @param color the color to set
*/
public void setColor(Color color) {
m_color = color;
}
/**
* Sets the color as a String.<p>
*
* @param value the color to set
*/
public void setColor(String value) {
if (COLOR_TRANSPARENT.indexOf(value) == 0) {
m_color = Simapi.COLOR_TRANSPARENT;
}
m_color = CmsStringUtil.getColorValue(value, Color.WHITE, SCALE_PARAM_COLOR);
}
/**
* Sets the list of filters as a String.<p>
*
* @param value the list of filters to set
*/
public void setFilters(String value) {
m_filters = new ArrayList();
List filters = CmsStringUtil.splitAsList(value, ':');
Iterator i = filters.iterator();
while (i.hasNext()) {
String filter = (String)i.next();
filter = filter.trim().toLowerCase();
Iterator j = FILTERS.iterator();
while (j.hasNext()) {
String candidate = (String)j.next();
if (candidate.startsWith(filter)) {
// found a matching filter
addFilter(candidate);
break;
}
}
}
}
/**
* Sets the height.<p>
*
* @param height the height to set
*/
public void setHeight(int height) {
m_height = height;
}
/**
* Sets the maximum image size (width * height) to apply image blurring when downscaling images.<p>
*
* @param maxBlurSize the maximum image blur size to set
*
* @see #getMaxBlurSize() for a more detailed description about this parameter
*/
public void setMaxBlurSize(int maxBlurSize) {
m_maxBlurSize = maxBlurSize;
}
/**
* Sets the scale position.<p>
*
* @param position the position to set
*/
public void setPosition(int position) {
switch (position) {
case Simapi.POS_DOWN_LEFT:
case Simapi.POS_DOWN_RIGHT:
case Simapi.POS_STRAIGHT_DOWN:
case Simapi.POS_STRAIGHT_LEFT:
case Simapi.POS_STRAIGHT_RIGHT:
case Simapi.POS_STRAIGHT_UP:
case Simapi.POS_UP_LEFT:
case Simapi.POS_UP_RIGHT:
// pos is fine
m_position = position;
break;
default:
m_position = Simapi.POS_CENTER;
}
}
/**
* Sets the image saving quality in percent.<p>
*
* @param quality the image saving quality (in percent) to set
*/
public void setQuality(int quality) {
if (quality < 0) {
m_quality = 0;
} else if (quality > 100) {
m_quality = 100;
} else {
m_quality = quality;
}
}
/**
* Sets the image rendering mode constant.<p>
*
* @param renderMode the image rendering mode to set
*
* @see #getRenderMode() for a list of allowed values for the rendering mode
*/
public void setRenderMode(int renderMode) {
if ((renderMode < Simapi.RENDER_QUALITY) || (renderMode > Simapi.RENDER_SPEED)) {
renderMode = Simapi.RENDER_QUALITY;
}
m_renderMode = renderMode;
}
/**
* Sets the scale type.<p>
*
* @param type the scale type to set
*
* @see #getType() for a detailed description of the possible values for the type
*/
public void setType(int type) {
if ((type < 0) || (type > 4)) {
// invalid type, use 0
m_type = 0;
} else {
m_type = type;
}
}
/**
* Sets the width.<p>
*
* @param width the width to set
*/
public void setWidth(int width) {
m_width = width;
}
/**
* Creates a request parameter configured with the values from this image scaler, also
* appends a <code>'?'</code> char as a prefix so that this may be direclty appended to an image URL.<p>
*
* This can be appended to an image request in order to apply image scaling parameters.<p>
*
* @return a request parameter configured with the values from this image scaler
*/
public String toRequestParam() {
StringBuffer result = new StringBuffer(128);
result.append('?');
result.append(PARAM_SCALE);
result.append('=');
result.append(toString());
return result.toString();
}
/**
* @see java.lang.Object#toString()
*/
public String toString() {
if (m_scaleParameters != null) {
return m_scaleParameters;
}
StringBuffer result = new StringBuffer(64);
result.append(CmsImageScaler.SCALE_PARAM_WIDTH);
result.append(':');
result.append(m_width);
result.append(',');
result.append(CmsImageScaler.SCALE_PARAM_HEIGHT);
result.append(':');
result.append(m_height);
if (m_type > 0) {
result.append(',');
result.append(CmsImageScaler.SCALE_PARAM_TYPE);
result.append(':');
result.append(m_type);
}
if (m_position > 0) {
result.append(',');
result.append(CmsImageScaler.SCALE_PARAM_POS);
result.append(':');
result.append(m_position);
}
if (m_color != Color.WHITE) {
result.append(',');
result.append(CmsImageScaler.SCALE_PARAM_COLOR);
result.append(':');
result.append(getColorString());
}
if (m_quality > 0) {
result.append(',');
result.append(CmsImageScaler.SCALE_PARAM_QUALITY);
result.append(':');
result.append(m_quality);
}
if (m_renderMode > 0) {
result.append(',');
result.append(CmsImageScaler.SCALE_PARAM_RENDERMODE);
result.append(':');
result.append(m_renderMode);
}
if (!m_filters.isEmpty()) {
result.append(',');
result.append(CmsImageScaler.SCALE_PARAM_FILTER);
result.append(':');
result.append(getFiltersString());
}
m_scaleParameters = result.toString();
return m_scaleParameters;
}
/**
* Initializes the members with the default values.<p>
*/
private void init() {
m_height = -1;
m_width = -1;
m_type = 0;
m_position = 0;
m_renderMode = 0;
m_quality = 0;
m_color = Color.WHITE;
m_filters = new ArrayList();
m_maxBlurSize = CmsImageLoader.getMaxBlurSize();
}
/**
* Copys all values from the given scaler into this scaler.<p>
*
* @param source the source scaler
*/
private void initValuesFrom(CmsImageScaler source) {
m_width = source.m_width;
m_height = source.m_height;
m_type = source.m_type;
m_position = source.m_position;
m_renderMode = source.m_renderMode;
m_quality = source.m_quality;
m_color = source.m_color;
m_filters = new ArrayList(source.m_filters);
m_maxBlurSize = source.m_maxBlurSize;
}
/**
* Parses the scaler parameters.<p>
*
* @param parameters the parameters to parse
*/
private void parseParameters(String parameters) {
m_width = -1;
m_height = -1;
m_position = 0;
m_type = 0;
m_color = Color.WHITE;
List tokens = CmsStringUtil.splitAsList(parameters, ',');
Iterator it = tokens.iterator();
String k;
String v;
while (it.hasNext()) {
String t = (String)it.next();
// extract key and value
k = null;
v = null;
int idx = t.indexOf(':');
if (idx >= 0) {
k = t.substring(0, idx).trim();
if (t.length() > idx) {
v = t.substring(idx + 1).trim();
}
}
if (CmsStringUtil.isNotEmpty(k) && CmsStringUtil.isNotEmpty(v)) {
// key and value are available
if (SCALE_PARAM_HEIGHT.equals(k)) {
// image height
m_height = CmsStringUtil.getIntValue(v, Integer.MIN_VALUE, k);
} else if (SCALE_PARAM_WIDTH.equals(k)) {
// image width
m_width = CmsStringUtil.getIntValue(v, Integer.MIN_VALUE, k);
} else if (SCALE_PARAM_TYPE.equals(k)) {
// scaling type
setType(CmsStringUtil.getIntValue(v, -1, CmsImageScaler.SCALE_PARAM_TYPE));
} else if (SCALE_PARAM_COLOR.equals(k)) {
// image background color
setColor(v);
} else if (SCALE_PARAM_POS.equals(k)) {
// image position (depends on scale type)
setPosition(CmsStringUtil.getIntValue(v, -1, CmsImageScaler.SCALE_PARAM_POS));
} else if (SCALE_PARAM_QUALITY.equals(k)) {
// image position (depends on scale type)
setQuality(CmsStringUtil.getIntValue(v, 0, k));
} else if (SCALE_PARAM_RENDERMODE.equals(k)) {
// image position (depends on scale type)
setRenderMode(CmsStringUtil.getIntValue(v, 0, k));
} else if (SCALE_PARAM_FILTER.equals(k)) {
// image filters to apply
setFilters(v);
} else {
if (LOG.isDebugEnabled()) {
LOG.debug(Messages.get().getBundle().key(Messages.ERR_INVALID_IMAGE_SCALE_PARAMS_2, k, v));
}
}
} else {
if (LOG.isDebugEnabled()) {
LOG.debug(Messages.get().getBundle().key(Messages.ERR_INVALID_IMAGE_SCALE_PARAMS_2, k, v));
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -