Skip to content

Instantly share code, notes, and snippets.

@vojkny
Created May 1, 2014 08:31
Show Gist options
  • Save vojkny/260a410e03e5663cdd3e to your computer and use it in GitHub Desktop.
Save vojkny/260a410e03e5663cdd3e to your computer and use it in GitHub Desktop.
package org.maite.types;
import lombok.extern.log4j.Log4j;
import java.io.IOException;
@Log4j
public class GDImageWrapper implements ImageWrapper<GDImageWrapper> {
public static final String JPEG = "Image::JPEG";
private PHP php = new PHP();
private String file;
public GDImageWrapper(File file) throws IOException {
this(file.getAbsolutePath());
}
public GDImageWrapper(String file) throws IOException {
this.file = file.replace("file://", "");
php = init();
}
@Override
public GDImageWrapper crop(int x, int y, int width, int height) {
php.add("$image->crop(%d, %d, %d, %d);", x, y, width, height);
return this;
}
@Override
public GDImageWrapper cropTo(int[] widthHeight) throws ImageWrapperException {
if (widthHeight.length != 2) {
throw new ImageWrapperException("Given array must contain only: width, height.");
}
return cropTo(widthHeight[0], widthHeight[1]);
}
@Override
public GDImageWrapper cropTo(int width, int height) throws ImageWrapperException {
int source[];
double ratio = ((double) width)/height;
if (ratio < getRatio()) {
int resize = (int) Math.round(getHeight() * ratio);
int halfx = getWidth() / 2;
source = new int[] {
halfx-(resize/2),
0,
resize,
getHeight()
};
} else {
int resize = (int) Math.round(getWidth() / ratio);
int halfy = getHeight() / 2;
source = new int[] {
0,
halfy-(resize/2),
getWidth(),
resize,
};
}
log.debug(String.format("Cropping to requested [%d,%d] as a crop from [%d-%d,%d-%d]",
width, height,
source[0], source[0]+source[2], source[1], source[1]+source[3]));
crop(source[0], source[1], source[2], source[3]);
resize(width, height);
return this;
}
@Override
public GDImageWrapper resize(Integer width, Integer height) throws ImageWrapperException {
final String stretch = width == null || height == null
? "Image::FIT" : "Image::EXACT";
php.add("$image->resize(%d, %d, %s);", width, height, stretch);
return this;
}
@Override
public GDImageWrapper write(String fileName, String type, float quality) throws ImageWrapperException {
php.add("$image->save('%s', %d, %s);",
fileName.replace("file://", ""),
(int) (quality * 100),
type);
try {
php.run();
} catch (Exception e) {
throw new ImageWrapperException(e);
}
return this;
}
@Override
public GDImageWrapper write(File file, String type, float quality) throws ImageWrapperException {
write(file.getAbsolutePath(), type, quality);
return this;
}
private String runSimple(String command) {
try {
return init().add(command).run();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Override
public int getWidth() {
return Integer.valueOf(runSimple("echo $image->getWidth();"));
}
@Override
public int getHeight() {
return Integer.valueOf(runSimple("echo $image->getHeight();")); }
@Override
public double getRatio() {
return Double.valueOf(runSimple("echo $image->getWidth() / $image->getHeight();"));
}
@Override
public void flush() {
}
private PHP init() throws IOException {
File netteImage = new File("/tmp/netteImage.php");
if (!netteImage.exists()) {
IOUtils.copy(
getClass().getResourceAsStream("/scripts/netteImage.php"),
netteImage.getOutputStream());
}
final String path = netteImage.getPath();
final PHP php = new PHP();
php.add("require '%s';", path);
php.add("$image = Image::fromFile('%s');", file);
return php;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment