MNT-23660: TIFF files fail to transform into PNG & JPG

- set startPage and endPage to 0 as default for NON multi-page image targets
This commit is contained in:
Krystian Dabrowski 2023-06-13 13:45:49 +02:00
parent e6c73c2dad
commit dd6da5b9e0
2 changed files with 46 additions and 39 deletions

View File

@ -26,22 +26,13 @@
*/
package org.alfresco.transform.imagemagick.transformers;
import org.alfresco.transform.base.TransformManager;
import org.alfresco.transform.base.executors.AbstractCommandExecutor;
import org.alfresco.transform.base.executors.RuntimeExec;
import org.alfresco.transform.base.util.CustomTransformerFileAdaptor;
import org.alfresco.transform.exceptions.TransformException;
import org.alfresco.transform.imagemagick.ImageMagickOptionsBuilder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import static org.alfresco.transform.base.util.Util.stringToInteger;
import static org.alfresco.transform.base.util.Util.stringToLong;
import static org.alfresco.transform.common.Mimetype.MIMETYPE_IMAGE_BMP;
import static org.alfresco.transform.common.Mimetype.MIMETYPE_IMAGE_JP2;
import static org.alfresco.transform.common.Mimetype.MIMETYPE_IMAGE_JPEG;
import static org.alfresco.transform.common.Mimetype.MIMETYPE_IMAGE_PNG;
import static org.alfresco.transform.common.Mimetype.MIMETYPE_IMAGE_XWD;
import static org.alfresco.transform.common.RequestParamMap.ALLOW_ENLARGEMENT;
import static org.alfresco.transform.common.RequestParamMap.ALPHA_REMOVE;
import static org.alfresco.transform.common.RequestParamMap.AUTO_ORIENT;
@ -61,9 +52,31 @@ import static org.alfresco.transform.common.RequestParamMap.START_PAGE;
import static org.alfresco.transform.common.RequestParamMap.THUMBNAIL;
import static org.alfresco.transform.common.RequestParamMap.TIMEOUT;
import javax.annotation.PostConstruct;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.alfresco.transform.base.TransformManager;
import org.alfresco.transform.base.executors.AbstractCommandExecutor;
import org.alfresco.transform.base.executors.RuntimeExec;
import org.alfresco.transform.base.util.CustomTransformerFileAdaptor;
import org.alfresco.transform.exceptions.TransformException;
import org.alfresco.transform.imagemagick.ImageMagickOptionsBuilder;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
* Converts image files into different types of images. Transformer supports multi-page images and allows to specify via parameters `startPage` and `endPage` range of pages that should be converted.
* In case of a one-page target image type (like `jpeg` or `png`) parameters `startPage` and `endPage` will be set to 0 by default - this means that only first page will be converted.
*/
@Component
public class ImageMagickTransformer extends AbstractCommandExecutor implements CustomTransformerFileAdaptor
{
private final List<String> singlePageFormats = List.of(MIMETYPE_IMAGE_BMP, MIMETYPE_IMAGE_JP2, MIMETYPE_IMAGE_JPEG, MIMETYPE_IMAGE_PNG, MIMETYPE_IMAGE_XWD);
@Value("${transform.core.imagemagick.exe}")
private String exe;
@Value("${transform.core.imagemagick.dyn}")
@ -152,10 +165,24 @@ public class ImageMagickTransformer extends AbstractCommandExecutor implements C
public void transform(String sourceMimetype, String targetMimetype, Map<String, String> transformOptions,
File sourceFile, File targetFile, TransformManager transformManager) throws TransformException
{
String startPageString = transformOptions.get(START_PAGE);
String endPageString = transformOptions.get(END_PAGE);
if (singlePageFormats.contains(targetMimetype))
{
if (StringUtils.isEmpty(startPageString))
{
startPageString = "0";
}
if (StringUtils.isEmpty(endPageString))
{
endPageString = startPageString;
}
}
final String options = ImageMagickOptionsBuilder
.builder()
.withStartPage(transformOptions.get(START_PAGE))
.withEndPage(transformOptions.get(END_PAGE))
.withStartPage(startPageString)
.withEndPage(endPageString)
.withAlphaRemove(transformOptions.get(ALPHA_REMOVE))
.withAutoOrient(transformOptions.get(AUTO_ORIENT))
.withCropGravity(transformOptions.get(CROP_GRAVITY))
@ -174,8 +201,8 @@ public class ImageMagickTransformer extends AbstractCommandExecutor implements C
.build();
String pageRange = calculatePageRange(
stringToInteger(transformOptions.get(START_PAGE)),
stringToInteger(transformOptions.get(END_PAGE))
stringToInteger(startPageString),
stringToInteger(endPageString)
);
Long timeout = stringToLong(transformOptions.get(TIMEOUT));

View File

@ -151,16 +151,6 @@ public class ImageMagickTransformationIT {
.add(Pair.of("3fr", MIMETYPE_IMAGE_RAW_3FR))
.build();
private static final List<Pair<String, String>> targetExtensionsForTiffFirstPage = new ImmutableList.Builder<Pair<String, String>>()
.add(Pair.of("bmp", MIMETYPE_IMAGE_BMP))
.add(Pair.of("jp2", MIMETYPE_IMAGE_JP2))
.add(Pair.of("jpg", MIMETYPE_IMAGE_JPEG))
.add(Pair.of("png", MIMETYPE_IMAGE_PNG))
.add(Pair.of("xbm", MIMETYPE_IMAGE_XBM))
.add(Pair.of("xpm", MIMETYPE_IMAGE_XPM))
.add(Pair.of("xwd", MIMETYPE_IMAGE_XWD))
.build();
private static final Map<String, FileInfo> TEST_FILES = Stream.of(
testFile(MIMETYPE_IMAGE_BMP, "bmp", "quick.bmp"),
testFile(MIMETYPE_IMAGE_GIF, "gif", "quick.gif"),
@ -209,16 +199,8 @@ public class ImageMagickTransformationIT {
sourceFile, sourceMimetype, targetMimetype, targetExtension);
try
{
// note: some image/tiff->image/* will return multiple page results (hence error) unless options specified for single page
Map<String, String> tOptions = emptyMap();
Pair<String,String> targetPair = Pair.of(targetExtension, targetMimetype);
if (MIMETYPE_IMAGE_TIFF.equals(sourceMimetype) && targetExtensionsForTiffFirstPage.contains(targetPair))
{
tOptions = ImmutableMap.of("startPage", "0", "endPage", "0");
}
final ResponseEntity<Resource> response = sendTRequest(ENGINE_URL, sourceFile, sourceMimetype,
targetMimetype, targetExtension, tOptions);
targetMimetype, targetExtension, emptyMap());
assertEquals(OK, response.getStatusCode(), descriptor);
}
catch (Exception e)
@ -233,6 +215,4 @@ public class ImageMagickTransformationIT {
.stream()
.map(k -> Pair.of(TEST_FILES.get(sourceFile), k));
}
}