use of org.olat.core.commons.services.image.Size in project openolat by klemens.
the class FileLinkChooserController method getSize.
private Size getSize(VFSItem item, String filename) {
Size size = null;
if (item instanceof VFSLeaf) {
VFSLeaf leaf = (VFSLeaf) item;
String suffix = FileUtils.getFileSuffix(filename);
size = movieService.getSize(leaf, suffix);
}
return size;
}
use of org.olat.core.commons.services.image.Size in project openolat by klemens.
the class AbstractImageHelper method getImageSize.
private Size getImageSize(VFSLeaf media, String suffix) {
Size result = null;
Iterator<ImageReader> iter = ImageIO.getImageReadersBySuffix(suffix);
if (iter.hasNext()) {
ImageInputStream stream = null;
InputStream mediaStream = null;
ImageReader reader = iter.next();
try {
mediaStream = media.getInputStream();
if (mediaStream != null) {
stream = new MemoryCacheImageInputStream(mediaStream);
reader.setInput(stream);
int readerMinIndex = reader.getMinIndex();
int width = reader.getWidth(readerMinIndex);
int height = reader.getHeight(readerMinIndex);
result = new Size(width, height, 0, 0, false);
}
} catch (IOException e) {
log.error(e.getMessage());
} finally {
IOUtils.closeQuietly(stream);
IOUtils.closeQuietly(mediaStream);
reader.dispose();
}
} else {
log.error("No reader found for given format: " + suffix);
}
return result;
}
use of org.olat.core.commons.services.image.Size in project openolat by klemens.
the class AbstractImageHelper method getImageSizeFallback.
private Size getImageSizeFallback(File media) {
BufferedImage imageSrc = null;
try (InputStream fileStream = new FileInputStream(media)) {
imageSrc = ImageIO.read(fileStream);
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, 0, 0, false);
} catch (IOException e) {
// log error, don't do anything else
log.error("Problem while setting image size to fit for resource::" + media, e);
return null;
} finally {
if (imageSrc != null) {
imageSrc.flush();
}
}
}
use of org.olat.core.commons.services.image.Size in project openolat by klemens.
the class AbstractImageHelper method getImageSize.
private Size getImageSize(File media, String suffix) {
Size result = null;
Iterator<ImageReader> iter = ImageIO.getImageReadersBySuffix(suffix);
if (iter.hasNext()) {
ImageReader reader = iter.next();
try (InputStream mediaStream = new FileInputStream(media);
ImageInputStream stream = new MemoryCacheImageInputStream(mediaStream)) {
reader.setInput(stream);
int readerMinIndex = reader.getMinIndex();
int width = reader.getWidth(readerMinIndex);
int height = reader.getHeight(readerMinIndex);
result = new Size(width, height, 0, 0, false);
} catch (IOException e) {
log.error(e.getMessage());
} finally {
reader.dispose();
}
} else {
log.error("No reader found for given format: " + suffix);
}
return result;
}
use of org.olat.core.commons.services.image.Size in project openolat by klemens.
the class ImageComponent method setMaxWithAndHeightToFitWithin.
/**
* Call this method to display the image within a given box of width and
* height. The method does NOT manipulate the image itself, it does only
* adjust the images width and height tag. <br />
* The image will made displayed smaller, it will not enlarge the image since
* this always looks bad. The scaling is done in a way to get an image that is
* smaller than the maxWidth or smaller than the maxHeight, depending on whith
* of the sizes produce a smaller scaling factor. <br />
* To scale an image on the filesystem to another width and height, use the
* ImageHelper.scaleImage() method.
*
* @param maxWidth
* @param maxHeight
*/
public void setMaxWithAndHeightToFitWithin(int maxWidth, int maxHeight) {
if (media == null || !media.exists()) {
scalingFactor = Float.NaN;
realSize = null;
scaledSize = null;
return;
}
try {
Size size = getRealSize();
if (size == null) {
return;
}
int realWidth = size.getWidth();
int realHeight = size.getHeight();
// calculate scaling factor
scalingFactor = 1f;
if (realWidth > maxWidth) {
float scalingWidth = 1f / realWidth * maxWidth;
scalingFactor = (scalingWidth < scalingFactor ? scalingWidth : scalingFactor);
}
if (realHeight > maxHeight) {
float scalingHeight = 1f / realHeight * maxHeight;
scalingFactor = (scalingHeight < scalingFactor ? scalingHeight : scalingFactor);
}
realSize = new Size(realWidth, realHeight, false);
scaledSize = new Size(Math.round(realWidth * scalingFactor), Math.round(realHeight * scalingFactor), false);
setDirty(true);
} catch (Exception e) {
// log error, don't do anything else
log.error("Problem while setting image size to fit " + maxWidth + "x" + maxHeight + " for resource::" + media, e);
}
}
Aggregations