it.anyplace.web.ImageProcessorMojo Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of a-web-maven-plugin Show documentation
Show all versions of a-web-maven-plugin Show documentation
A web resource compiler/preprocessor, Maven Plugin
The newest version!
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package it.anyplace.web;
import com.google.common.base.Function;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.google.common.collect.LinkedListMultimap;
import com.google.common.collect.Lists;
import com.google.common.collect.Multimap;
import com.mortennobel.imagescaling.ResampleOp;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import javax.imageio.ImageIO;
import org.apache.commons.io.FileUtils;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
/**
*
* @author aleph
*/
@Mojo(name = "img", defaultPhase = LifecyclePhase.PACKAGE)
public class ImageProcessorMojo extends AbstractMojo {
private final Log logger = getLog();
@Parameter(required = true, property = "sourceDirectory", defaultValue = "${project.basedir}/src/main/webapp/")
private File sourceDirectory;
@Parameter(required = true, property = "targetDirectory", defaultValue = "${project.build.directory}/${project.build.finalName}/")
private File targetDirectory;
@Parameter(property = "compressImages", defaultValue = "")
private String compressImages;
private final BuildCache cache = new BuildCache();
public void execute() throws MojoExecutionException, MojoFailureException {
if (!Strings.isNullOrEmpty(compressImages)) {
logger.info("processing images " + compressImages);
List split = Lists.newArrayList(compressImages.split(",? +"));
Preconditions.checkArgument(split.size() % 2 == 0);
Iterator iterator = split.iterator();
Multimap> regexMap = LinkedListMultimap.create();
while (iterator.hasNext()) {
final String regexStr = iterator.next(), config = iterator.next();
final int width, height;
final String suffix;
if (config.matches("^[0-9]+$")) {
width = height = Integer.valueOf(config);
suffix = null;
} else if (config.matches("^[0-9]+x[0-9]+$")) {
width = Integer.valueOf(config.split("x")[0]);
height = Integer.valueOf(config.split("x")[1]);
suffix = null;
} else if (config.matches("^[0-9]+x[0-9]+:.+$")) {
width = Integer.valueOf(config.split("[x:]")[0]);
height = Integer.valueOf(config.split("[x:]")[1]);
suffix = config.split("[x:]")[2];
} else {
throw new UnsupportedOperationException("unsupported image config = " + config);
}
final ResampleOp resampleOp = new ResampleOp(width, height);
regexMap.put(regexStr, new Function() {
public Void apply(File sourceFile) {
try {
File targetFile = targetDirectory.toPath().resolve(sourceDirectory.toPath().relativize(sourceFile.toPath())).toFile();
if (!Strings.isNullOrEmpty(suffix)) {
targetFile = new File(targetFile.getParentFile(), targetFile.getName().replaceFirst("(.+)([.].+)", "$1" + suffix + "$2"));
}
logger.debug("scaling " + sourceFile + " to " + targetFile + " size " + config);
File result = cache.getResult(sourceFile, "imageProcessing" + config);
if (result == null) {
BufferedImage sourceImage = ImageIO.read(sourceFile);
BufferedImage scaledImage = resampleOp.filter(sourceImage, null);
ImageIO.write(scaledImage, sourceFile.getName().replaceFirst("^.*[.]", ""), targetFile);
cache.cacheResult(sourceFile, "imageProcessing" + config, FileUtils.readFileToByteArray(targetFile));
} else {
FileUtils.copyFile(result, targetFile);
}
// byte[] data = FileUtils.readFileToByteArray(sourceFile), result = cache.getResult(data, config);
// if (result == null) {
// BufferedImage sourceImage = ImageIO.read(new ByteArrayInputStream(data));
// cache.cacheResult(data, config, FileUtils.readFileToByteArray(targetFile));
// } else {
// FileUtils.writeByteArrayToFile(targetFile, result);
// }
// BufferedImage resizedImage = new BufferedImage(IMG_WIDTH, IMG_HEIGHT, type);
// Graphics2D g = resizedImage.createGraphics();
// g.drawImage(originalImage, 0, 0, IMG_WIDTH, IMG_HEIGHT, null);
// g.dispose();
// g.setComposite(AlphaComposite.Src);
//
// g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
// RenderingHints.VALUE_INTERPOLATION_BILINEAR);
// g.setRenderingHint(RenderingHints.KEY_RENDERING,
// RenderingHints.VALUE_RENDER_QUALITY);
// g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
// RenderingHints.VALUE_ANTIALIAS_ON);
return null;
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
});
}
for (File file : FileUtils.listFiles(sourceDirectory, new String[]{"png", "jpg", "jpeg"}, true)) {
for (Map.Entry> entry : regexMap.entries()) {
if (Pattern.compile(entry.getKey()).matcher(file.getPath()).find()) {
entry.getValue().apply(file);
}
}
}
}
}
}