use of org.olat.core.commons.services.image.Crop in project OpenOLAT by OpenOLAT.
the class ImageFormItem method evalFormRequest.
@Override
public void evalFormRequest(UserRequest ureq) {
String imgId = "o_img" + imageComponent.getDispatchID();
String x = getRootForm().getRequestParameter(imgId + "_x");
String y = getRootForm().getRequestParameter(imgId + "_y");
String w = getRootForm().getRequestParameter(imgId + "_w");
String h = getRootForm().getRequestParameter(imgId + "_h");
if (StringHelper.isLong(x) && StringHelper.isLong(y) && StringHelper.isLong(w) && StringHelper.isLong(h)) {
try {
Crop c = new Crop();
c.setX(Integer.parseInt(x));
c.setY(Integer.parseInt(y));
c.setWidth(Integer.parseInt(w));
c.setHeight(Integer.parseInt(h));
cropSelection = c;
} catch (NumberFormatException e) {
log.warn("", e);
cropSelection = null;
}
} else {
cropSelection = null;
}
}
use of org.olat.core.commons.services.image.Crop in project openolat by klemens.
the class VideoManagerImpl method setPosterframeResizeUploadfile.
/**
* Sets the posterframe resize uploadfile. Tries to fit image to dimensions of video.
*
* @param videoResource the video resource
* @param posterframe the newPosterFile
*/
@Override
public void setPosterframeResizeUploadfile(OLATResource videoResource, VFSLeaf newPosterFile) {
VideoMeta videoMetadata = getVideoMetadata(videoResource);
Size posterRes = imageHelper.getSize(newPosterFile, FILETYPE_JPG);
// file size needs to be bigger than target resolution, otherwise use image as it comes
if (posterRes != null && posterRes.getHeight() != 0 && posterRes.getWidth() != 0 && posterRes.getHeight() >= videoMetadata.getHeight() && posterRes.getWidth() >= videoMetadata.getWidth()) {
VFSLeaf oldPosterFile = getPosterframe(videoResource);
oldPosterFile.delete();
VFSContainer masterContainer = getMasterContainer(videoResource);
LocalFileImpl newPoster = (LocalFileImpl) masterContainer.createChildLeaf(FILENAME_POSTER_JPG);
// to shrink image file, resolution ratio needs to be equal, otherwise crop from top left corner
if (posterRes.getHeight() / posterRes.getWidth() == videoMetadata.getHeight() / videoMetadata.getWidth()) {
imageHelper.scaleImage(newPosterFile, newPoster, videoMetadata.getWidth(), videoMetadata.getHeight(), true);
} else {
Crop cropSelection = new Crop(0, 0, videoMetadata.getHeight(), videoMetadata.getWidth());
imageHelper.cropImage(((LocalFileImpl) newPosterFile).getBasefile(), newPoster.getBasefile(), cropSelection);
}
} else {
setPosterframe(videoResource, newPosterFile);
}
}
use of org.olat.core.commons.services.image.Crop in project openolat by klemens.
the class FileElementImpl method moveUploadFileTo.
@Override
public VFSLeaf moveUploadFileTo(VFSContainer destinationContainer, boolean crop) {
VFSLeaf targetLeaf = null;
if (tempUploadFile != null && tempUploadFile.exists()) {
// Check if such a file does already exist, if yes rename new file
VFSItem existsChild = destinationContainer.resolve(uploadFilename);
if (existsChild != null) {
// Use standard rename policy
uploadFilename = VFSManager.rename(destinationContainer, uploadFilename);
}
// Create target leaf file now and delete original temp file
if (destinationContainer instanceof LocalFolderImpl) {
// Optimize for local files (don't copy, move instead)
LocalFolderImpl folderContainer = (LocalFolderImpl) destinationContainer;
File destinationDir = folderContainer.getBasefile();
File targetFile = new File(destinationDir, uploadFilename);
Crop cropSelection = previewEl == null ? null : previewEl.getCropSelection();
if (crop && cropSelection != null) {
CoreSpringFactory.getImpl(ImageService.class).cropImage(tempUploadFile, targetFile, cropSelection);
targetLeaf = (VFSLeaf) destinationContainer.resolve(targetFile.getName());
} else if (FileUtils.copyFileToFile(tempUploadFile, targetFile, true)) {
targetLeaf = (VFSLeaf) destinationContainer.resolve(targetFile.getName());
} else {
log.error("Error after copying content from temp file, cannot copy file::" + (tempUploadFile == null ? "NULL" : tempUploadFile) + " - " + (targetFile == null ? "NULL" : targetFile), null);
}
if (targetLeaf == null) {
log.error("Error after copying content from temp file, cannot resolve copied file::" + (tempUploadFile == null ? "NULL" : tempUploadFile) + " - " + (targetFile == null ? "NULL" : targetFile), null);
}
} else {
// Copy stream in case the destination is a non-local container
VFSLeaf leaf = destinationContainer.createChildLeaf(uploadFilename);
boolean success = false;
try {
success = VFSManager.copyContent(new FileInputStream(tempUploadFile), leaf);
} catch (FileNotFoundException e) {
log.error("Error while copying content from temp file::" + (tempUploadFile == null ? "NULL" : tempUploadFile.getAbsolutePath()), e);
}
if (success) {
// Delete original temp file after copy to simulate move
// behavior
tempUploadFile.delete();
targetLeaf = leaf;
}
}
} else if (log.isDebug()) {
log.debug("Error while copying content from temp file, no temp file::" + (tempUploadFile == null ? "NULL" : tempUploadFile.getAbsolutePath()));
}
return targetLeaf;
}
use of org.olat.core.commons.services.image.Crop in project openolat by klemens.
the class ImageFormItem method evalFormRequest.
@Override
public void evalFormRequest(UserRequest ureq) {
String imgId = "o_img" + imageComponent.getDispatchID();
String x = getRootForm().getRequestParameter(imgId + "_x");
String y = getRootForm().getRequestParameter(imgId + "_y");
String w = getRootForm().getRequestParameter(imgId + "_w");
String h = getRootForm().getRequestParameter(imgId + "_h");
if (StringHelper.isLong(x) && StringHelper.isLong(y) && StringHelper.isLong(w) && StringHelper.isLong(h)) {
try {
Crop c = new Crop();
c.setX(Integer.parseInt(x));
c.setY(Integer.parseInt(y));
c.setWidth(Integer.parseInt(w));
c.setHeight(Integer.parseInt(h));
cropSelection = c;
} catch (NumberFormatException e) {
log.warn("", e);
cropSelection = null;
}
} else {
cropSelection = null;
}
}
use of org.olat.core.commons.services.image.Crop in project OpenOLAT by OpenOLAT.
the class VideoManagerImpl method setPosterframeResizeUploadfile.
/**
* Sets the posterframe resize uploadfile. Tries to fit image to dimensions of video.
*
* @param videoResource the video resource
* @param posterframe the newPosterFile
*/
@Override
public void setPosterframeResizeUploadfile(OLATResource videoResource, VFSLeaf newPosterFile) {
VideoMeta videoMetadata = getVideoMetadata(videoResource);
Size posterRes = imageHelper.getSize(newPosterFile, FILETYPE_JPG);
// file size needs to be bigger than target resolution, otherwise use image as it comes
if (posterRes != null && posterRes.getHeight() != 0 && posterRes.getWidth() != 0 && posterRes.getHeight() >= videoMetadata.getHeight() && posterRes.getWidth() >= videoMetadata.getWidth()) {
VFSLeaf oldPosterFile = getPosterframe(videoResource);
oldPosterFile.delete();
VFSContainer masterContainer = getMasterContainer(videoResource);
LocalFileImpl newPoster = (LocalFileImpl) masterContainer.createChildLeaf(FILENAME_POSTER_JPG);
// to shrink image file, resolution ratio needs to be equal, otherwise crop from top left corner
if (posterRes.getHeight() / posterRes.getWidth() == videoMetadata.getHeight() / videoMetadata.getWidth()) {
imageHelper.scaleImage(newPosterFile, newPoster, videoMetadata.getWidth(), videoMetadata.getHeight(), true);
} else {
Crop cropSelection = new Crop(0, 0, videoMetadata.getHeight(), videoMetadata.getWidth());
imageHelper.cropImage(((LocalFileImpl) newPosterFile).getBasefile(), newPoster.getBasefile(), cropSelection);
}
} else {
setPosterframe(videoResource, newPosterFile);
}
}
Aggregations