Skip to content

Commit

Permalink
Maximum of 4 screenshots at a time
Browse files Browse the repository at this point in the history
  • Loading branch information
tobi committed Apr 2, 2012
1 parent 57099d6 commit 449664e
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 8 deletions.
5 changes: 2 additions & 3 deletions main.go
Expand Up @@ -29,7 +29,6 @@ func servePng(w http.ResponseWriter, file io.Reader) {

// 3 hours
w.Header().Set("Cache-Control", "public, max-age=10800")

w.WriteHeader(http.StatusOK)
io.Copy(w, file)
}
Expand Down Expand Up @@ -83,9 +82,9 @@ func Server(w http.ResponseWriter, r *http.Request) {
if len(buffer) == 0 {

// make the screenshot
filename, err := phantom.Screenshot(url)
filename := phantom.Screenshot(url)

if err != nil {
if filename == "" {
httpError(w, "Error creating screenshot")
return
}
Expand Down
6 changes: 4 additions & 2 deletions mogrify/mogrify.go
Expand Up @@ -16,7 +16,9 @@ func init() {
}

func Resize(out io.Writer, in io.Reader, size string) error {
cmd := exec.Command("gm", "convert", "-resize", size, "-colorspace", "RGB", "-", "-")
log.Printf("resize:%s", size)

cmd := exec.Command("gm", "convert", "-resize", size, "-colorspace", "RGB", "-quality", "90%%", "+profile", "'!EXIF,!IPTC,*'", "-", "-")
stdin, err := cmd.StdinPipe()
if err != nil {
return err
Expand Down Expand Up @@ -44,7 +46,7 @@ func Resize(out io.Writer, in io.Reader, size string) error {

func ResizeFile(filename string, size string) error {
log.Printf("resize:%s", size)
cmd := exec.Command("gm", "convert", "-resize", size, "-colorspace", "RGB", filename, filename)
cmd := exec.Command("gm", "convert", "-resize", size, "-colorspace", "RGB", "-quality", "90%%", "+profile", "'!EXIF,!IPTC,*'", filename, filename)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
Expand Down
37 changes: 35 additions & 2 deletions phantom/phantom.go
Expand Up @@ -13,6 +13,13 @@ var phantomPath string
var rand uint32
var dir string

type shot struct {
url string
resultChan chan string
}

var screenshotChan = make(chan *shot, 10)

func init() {
dir = os.TempDir()
rand = reseed()
Expand All @@ -24,6 +31,32 @@ func init() {
if err != nil {
log.Fatalf("Cannot find phantomjs executable in bath")
}

go webkitWorker()
go webkitWorker()
go webkitWorker()
go webkitWorker()
}

func webkitWorker() {
for shot := range screenshotChan {

result, err := screenshot(shot.url)
if err == nil {
shot.resultChan <- result
} else {
log.Printf("Screenshot error: %s", err)
close(shot.resultChan)
}

}
}

func Screenshot(url string) string {
shot := shot{url, make(chan string)}

screenshotChan <- &shot
return <-shot.resultChan
}

func reseed() uint32 {
Expand All @@ -41,12 +74,12 @@ func tempPngFileName() string {
return filepath.Join(dir, "img", nextSuffix(), ".png")
}

func Screenshot(url string) (string, error) {
func screenshot(url string) (string, error) {
log.Printf("Screenshotting %s", url)

filename := tempPngFileName()

cmd := exec.Command(phantomPath, "render.js", url, "1024", "786", filename)
cmd := exec.Command(phantomPath, "render.js", url, "1280", "500", filename)

// connect to STDIN / STDOUT
cmd.Stdout = os.Stdout
Expand Down
2 changes: 1 addition & 1 deletion phantom/phantom_test.go
Expand Up @@ -6,5 +6,5 @@ import (

func TestScreenshot(t *testing.T) {

phantomScreenshot("http://www.snowdevil.ca")
Screenshot("http://www.snowdevil.ca")
}

0 comments on commit 449664e

Please sign in to comment.