Skip to content
Snippets Groups Projects
Commit 17f213d5 authored by srosse's avatar srosse
Browse files

OO-476: optimize guess of image size in ImageComponent

parent 10fa1318
No related branches found
No related tags found
No related merge requests found
......@@ -29,9 +29,14 @@ package org.olat.core.gui.components.image;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.util.Iterator;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import javax.imageio.stream.MemoryCacheImageInputStream;
import org.apache.commons.io.IOUtils;
import org.olat.core.gui.UserRequest;
import org.olat.core.gui.components.Component;
import org.olat.core.gui.components.ComponentRenderer;
......@@ -39,6 +44,8 @@ import org.olat.core.gui.media.MediaResource;
import org.olat.core.logging.AssertException;
import org.olat.core.logging.OLog;
import org.olat.core.logging.Tracing;
import org.olat.core.util.StringHelper;
import org.olat.core.util.image.Size;
/**
* Description: <br>
......@@ -137,17 +144,28 @@ public class ImageComponent extends Component {
if (mediaResource == null || mediaResource.getInputStream() == null) {
throw new AssertException("Set media resource to a valid value befor calling scaleToFit::" + mediaResource);
}
BufferedInputStream fileStrean = null;
BufferedImage imageSrc = null;
try {
fileStrean = new BufferedInputStream(mediaResource.getInputStream());
imageSrc = ImageIO.read(fileStrean);
if (imageSrc == null) {
// happens with faulty Java implementation, e.g. on MacOSX
String type = mediaResource.getContentType();
String suffix = getSuffix(type);
Size size = null;
if(StringHelper.containsNonWhitespace(suffix)) {
size = getImageSize(suffix);
if(size != null) {
System.out.println(size);
}
}
if(size == null) {
size = getImageSizeFallback();
}
if(size == null) {
return;
}
double realWidth = imageSrc.getWidth();
double realHeight = imageSrc.getHeight();
double realWidth = size.getWidth();
double realHeight = size.getHeight();
// calculate scaling factor
double scalingFactor = 1;
if (realWidth > maxWidth) {
......@@ -160,21 +178,70 @@ public class ImageComponent extends Component {
}
setHeight(new Long( Math.round(realHeight * scalingFactor)));
setWidth(new Long( Math.round(realWidth * scalingFactor)));
} catch (IOException e) {
} catch (Exception e) {
// log error, don't do anything else
log.error("Problem while setting image size to fit " + maxWidth + "x" + maxHeight + " for resource::" + mediaResource, e);
} finally {
// release all resources
if (fileStrean != null) {
try {
fileStrean.close();
} catch (IOException e) {
log.error("Problem while closing file stream for resource::" + mediaResource, e);
}
}
}
private String getSuffix(String contentType) {
contentType = contentType.toLowerCase();
if(contentType.indexOf("jpg") >= 0 || contentType.indexOf("jpeg") >= 0) {
return "jpg";
}
if(contentType.indexOf("gif") >= 0) {
return "gif";
}
if(contentType.indexOf("png") >= 0) {
return "png";
}
return null;
}
private Size getImageSizeFallback() {
BufferedInputStream fileStrean = null;
BufferedImage imageSrc = null;
try {
fileStrean = new BufferedInputStream(mediaResource.getInputStream());
imageSrc = ImageIO.read(fileStrean);
if (imageSrc == null) {
// happens with faulty Java implementation, e.g. on MacOSX
return null;
}
double realWidth = imageSrc.getWidth();
double realHeight = imageSrc.getHeight();
return new Size((int)realWidth, (int)realHeight, false);
} catch (IOException e) {
// log error, don't do anything else
log.error("Problem while setting image size to fit for resource::" + mediaResource, e);
return null;
} finally {
IOUtils.closeQuietly(fileStrean);
if (imageSrc != null) {
imageSrc.flush();
}
}
}
private Size getImageSize(String suffix) {
Size result = null;
Iterator<ImageReader> iter = ImageIO.getImageReadersBySuffix(suffix);
if (iter.hasNext()) {
ImageReader reader = iter.next();
try {
ImageInputStream stream = new MemoryCacheImageInputStream(mediaResource.getInputStream());
reader.setInput(stream);
int width = reader.getWidth(reader.getMinIndex());
int height = reader.getHeight(reader.getMinIndex());
result = new Size(width, height, false);
} catch (IOException e) {
log.error(e.getMessage());
} finally {
reader.dispose();
}
} else {
log.error("No reader found for given format: " + suffix);
}
return result;
}
}
\ No newline at end of file
......@@ -19,6 +19,11 @@
*/
package org.olat.core.util.image;
/**
*
* @author srosse, stephane.rosse@frentix.com, http://www.frentix
*
*/
public class Size {
private final int width;
private final int height;
......@@ -47,4 +52,14 @@ public class Size {
public boolean isChanged() {
return changed;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("Size[width=").append(width)
.append(":height=").append(height)
.append(":changed=").append(changed)
.append("]").append(super.toString());
return sb.toString();
}
}
......@@ -26,6 +26,7 @@
package org.olat.course.condition.interpreter;
import org.olat.core.id.Identity;
import org.olat.core.logging.OLog;
import org.olat.core.logging.Tracing;
import org.olat.course.editor.CourseEditorEnv;
import org.olat.course.groupsandrights.CourseGroupManager;
......@@ -40,6 +41,7 @@ import org.olat.course.run.userview.UserCourseEnvironment;
* @author gnaegi
*/
public class IsCourseAdministratorFunction extends AbstractFunction {
private static final OLog log = Tracing.createLoggerFor(IsCourseAdministratorFunction.class);
public static final String name = "isCourseAdministrator";
/**
......@@ -66,10 +68,9 @@ public class IsCourseAdministratorFunction extends AbstractFunction {
Identity ident = getUserCourseEnv().getIdentityEnvironment().getIdentity();
CourseGroupManager cgm = getUserCourseEnv().getCourseEnvironment().getCourseGroupManager();
boolean isCourseAdmin = cgm.isIdentityCourseAdministrator(ident);
if (Tracing.isDebugEnabled(IsCourseAdministratorFunction.class)) {
Tracing.logDebug("identity "+ident.getName()+", courseadministrator:"+isCourseAdmin+", in course "+getUserCourseEnv().getCourseEnvironment().getCourseResourceableId(), IsCourseAdministratorFunction.class);
if(log.isDebug()) {
log.debug("identity "+ident.getName()+", courseadministrator:"+isCourseAdmin+", in course "+getUserCourseEnv().getCourseEnvironment().getCourseResourceableId());
}
return isCourseAdmin ? ConditionInterpreter.INT_TRUE: ConditionInterpreter.INT_FALSE;
}
......
......@@ -26,6 +26,7 @@
package org.olat.course.condition.interpreter;
import org.olat.core.id.Identity;
import org.olat.core.logging.OLog;
import org.olat.core.logging.Tracing;
import org.olat.course.editor.CourseEditorEnv;
import org.olat.course.groupsandrights.CourseGroupManager;
......@@ -37,6 +38,7 @@ import org.olat.course.run.userview.UserCourseEnvironment;
* Description: A user is course coach if he/she is in at least one owner group of a learning group of the learning group context of the course
*/
public class IsCourseCoachFunction extends AbstractFunction {
private static final OLog log = Tracing.createLoggerFor(IsCourseCoachFunction.class);
public static final String name = "isCourseCoach";
/**
......@@ -62,8 +64,8 @@ public class IsCourseCoachFunction extends AbstractFunction {
Identity ident = getUserCourseEnv().getIdentityEnvironment().getIdentity();
CourseGroupManager cgm = getUserCourseEnv().getCourseEnvironment().getCourseGroupManager();
boolean isCourseCoach = cgm.isIdentityCourseCoach(ident);
if (Tracing.isDebugEnabled(IsCourseCoachFunction.class)) {
Tracing.logDebug("identity "+ident.getName()+", coursecoach:"+isCourseCoach+", in course "+getUserCourseEnv().getCourseEnvironment().getCourseResourceableId(), IsCourseCoachFunction.class);
if (log.isDebug()) {
log.debug("identity "+ident.getName()+", coursecoach:"+isCourseCoach+", in course "+getUserCourseEnv().getCourseEnvironment().getCourseResourceableId());
}
return isCourseCoach ? ConditionInterpreter.INT_TRUE: ConditionInterpreter.INT_FALSE;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment