Java Code Examples for org.im4java.core.IMOperation#resize()

The following examples show how to use org.im4java.core.IMOperation#resize() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GMBatchCommandTest.java    From gm4java with Apache License 2.0 6 votes vote down vote up
@Test
public void run_chokes_whenServiceChokes() throws Exception {
    // create command
    final String command = "bad";
    sut = new GMBatchCommand(service, command);
    // create the operation, add images and operators/options
    IMOperation op = new IMOperation();
    op.addImage(SOURCE_IMAGE);
    op.resize(800, 600);
    op.addImage(TARGET_IMAGE);
    final String message = "bad command";
    when(service.execute(anyListOf(String.class))).thenThrow(new GMException(message));
    exception.expect(CommandException.class);
    exception.expectMessage(message);

    // execute the operation
    sut.run(op);
}
 
Example 2
Source File: GMBatchCommandTest.java    From gm4java with Apache License 2.0 6 votes vote down vote up
@Test
@SuppressWarnings("NP_NONNULL_PARAM_VIOLATION")
public void run_chokes_whenErrorConsumerIsNull() throws Exception {
    // create command
    final String command = "bad";
    sut = new GMBatchCommand(service, command);
    // create the operation, add images and operators/options
    IMOperation op = new IMOperation();
    op.addImage(SOURCE_IMAGE);
    op.resize(800, 600);
    op.addImage(TARGET_IMAGE);
    final String message = "bad command";
    when(service.execute(anyListOf(String.class))).thenThrow(new GMException(message));
    exception.expect(CommandException.class);
    exception.expectMessage(message);
    sut.setErrorConsumer(null);

    // execute the operation
    sut.run(op);
}
 
Example 3
Source File: GMBatchCommandTest.java    From gm4java with Apache License 2.0 6 votes vote down vote up
@Test
public void run_sendsCommandToService() throws Exception {
    // create command
    final String command = "convert";
    sut = new GMBatchCommand(service, command);
    // create the operation, add images and operators/options
    IMOperation op = new IMOperation();
    op.addImage(SOURCE_IMAGE);
    op.resize(800, 600);
    op.addImage(TARGET_IMAGE);

    // execute the operation
    sut.run(op);

    verify(service).execute(Arrays.asList(command, SOURCE_IMAGE, "-resize", "800x600", TARGET_IMAGE));
}
 
Example 4
Source File: GMBatchCommandTest.java    From gm4java with Apache License 2.0 6 votes vote down vote up
@Test
@SuppressWarnings("NP_NONNULL_PARAM_VIOLATION")
public void run_works_whenOutputConsumerIsNull() throws Exception {
    // create command
    final String command = "convert";
    sut = new GMBatchCommand(service, command);
    // create the operation, add images and operators/options
    IMOperation op = new IMOperation();
    op.addImage(SOURCE_IMAGE);
    op.resize(800, 600);
    op.addImage(TARGET_IMAGE);
    sut.setOutputConsumer(null);

    // execute the operation
    sut.run(op);

    verify(service).execute(Arrays.asList(command, SOURCE_IMAGE, "-resize", "800x600", TARGET_IMAGE));
}
 
Example 5
Source File: GMBatchCommandTest.java    From gm4java with Apache License 2.0 6 votes vote down vote up
@Test
public void run_handlesBufferedImageAsInput() throws Exception {
    final String command = "convert";
    sut = new GMBatchCommand(service, command);
    BufferedImage image = ImageIO.read(getClass().getResourceAsStream("/a.png"));
    IMOperation op = new IMOperation();
    op.addImage();                        // input
    op.resize(80, 60);
    op.addImage();                        // output

    sut.run(op, image, TARGET_IMAGE);

    @java.lang.SuppressWarnings("unchecked")
    ArgumentCaptor<List<String>> captor = ArgumentCaptor.forClass((Class<List<String>>) (Class<?>) List.class);
    verify(service).execute(captor.capture());
    assertThat(captor.getValue(),
            equalTo(Arrays.asList(command, captor.getValue().get(1), "-resize", "80x60", TARGET_IMAGE)));
}
 
Example 6
Source File: ImageResource.java    From entando-components with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Resize images using im4Java
 */
private void saveResizedImage(ResourceDataBean bean, ImageResourceDimension dimension) throws ApsSystemException {
    if (dimension.getIdDim() == 0) {
        // skips element with id zero that shouldn't be resized
        return;
    }
    String imageName = this.getNewInstanceFileName(bean.getFileName(), dimension.getIdDim(), null);
    String subPath = super.getDiskSubFolder() + imageName;
    try {
        this.getStorageManager().deleteFile(subPath, this.isProtectedResource());
        ResourceInstance resizedInstance = new ResourceInstance();
        resizedInstance.setSize(dimension.getIdDim());
        resizedInstance.setFileName(imageName);
        resizedInstance.setMimeType(bean.getMimeType());
        String tempFilePath = System.getProperty("java.io.tmpdir") + File.separator + "temp_" + imageName;
        File tempFile = new File(tempFilePath);
        long realLength = tempFile.length() / 1000;
        resizedInstance.setFileLength(realLength + " Kb");
        this.addInstance(resizedInstance);
        // create command
        ConvertCmd convertCmd = new ConvertCmd();
        //Is it a windows system?
        if (this.isImageMagickWindows()) {
            //yes so configure the path where ImagicMagick is installed
            convertCmd.setSearchPath(this.getImageMagickPath());
        }

        //svg files won't resize correctly via the image processor so save them directly
        if(bean.getMimeType().contains("svg")) {
            FileUtils.copyFile(bean.getFile(), tempFile);
            this.getStorageManager().saveFile(subPath, this.isProtectedResource(), new FileInputStream(tempFile));
        }else {
            // create the operation, add images and operators/options
            IMOperation imOper = new IMOperation();
            imOper.addImage();
            imOper.resize(dimension.getDimx(), dimension.getDimy());
            imOper.addImage();
            convertCmd.run(imOper, bean.getFile().getAbsolutePath(), tempFile.getAbsolutePath());
            this.getStorageManager().saveFile(subPath, this.isProtectedResource(), new FileInputStream(tempFile));
        }

        boolean deleted = tempFile.delete();

        if (!deleted) {
            logger.warn(FAILED_TO_DELETE_TEMP_FILE, tempFile);
        }
    } catch (Throwable t) {
        logger.error("Error creating resource file instance '{}'", subPath, t);
        throw new ApsSystemException("Error creating resource file instance '" + subPath + "'", t);
    }
}