Skip to content

Instantly share code, notes, and snippets.

@amscotti
Created April 27, 2013 17:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amscotti/5473860 to your computer and use it in GitHub Desktop.
Save amscotti/5473860 to your computer and use it in GitHub Desktop.
import java.awt.Dimension
import java.awt.Graphics2D
import java.awt.RenderingHints
import java.awt.image.BufferedImage
import javax.imageio.ImageIO
import org.w3c.dom.Document
import org.w3c.tidy.Tidy
import org.xhtmlrenderer.simple.Graphics2DRenderer
def makeThumbnail(address) {
// Size for the renderer
def WIDTH = 1280
def HEIGHT = 800
// Setup Tidy
def tidy = new Tidy()
tidy.with {
setQuiet(true)
setXHTML(true)
setHideComments(true)
setInputEncoding("UTF-8")
setOutputEncoding("UTF-8")
setShowErrors(0)
setShowWarnings(false)
}
def url = new URL(address)
def doc = tidy.parseDOM(new ByteArrayInputStream(url.text.getBytes("UTF-8")), null)
def os = new FileOutputStream("/Users/ascotti/${url.getHost()}.png")
def buf = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB)
def graphics = buf.createGraphics()
def renderer = new Graphics2DRenderer()
renderer.with {
setDocument(doc, address)
layout(graphics, new Dimension(WIDTH, HEIGHT))
render(graphics)
graphics.dispose()
ImageIO.write(buf, "png", os)
}
}
makeThumbnail("http://www.128bitstudios.com/")
makeThumbnail("http://en.wikipedia.org/")
makeThumbnail("http://misplaced-packets.net/")
makeThumbnail("http://stackoverflow.com/")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment