Skip to content
Browse files

Maximum of 4 screenshots at a time

  • Loading branch information...
1 parent 57099d6 commit 449664ec93b2f0813fbc6a36956e831425631e25 @tobi committed Apr 2, 2012
Showing with 42 additions and 8 deletions.
  1. +2 −3 main.go
  2. +4 −2 mogrify/mogrify.go
  3. +35 −2 phantom/phantom.go
  4. +1 −1 phantom/phantom_test.go
View
5 main.go
@@ -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)
}
@@ -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
}
View
6 mogrify/mogrify.go
@@ -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
@@ -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()
View
37 phantom/phantom.go
@@ -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()
@@ -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 {
@@ -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
View
2 phantom/phantom_test.go
@@ -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.
Something went wrong with that request. Please try again.