Permalink
Browse files

Initial checkin

  • Loading branch information...
0 parents commit db709258c4411a260fee9dd1d286ec7b96f93127 @tobi committed Mar 26, 2012
@@ -0,0 +1,5 @@
+beta:
+ - error messages in browser
+ - caching
+ - permanent db ( leveldb? )
+ - resizing!
Binary file not shown.
@@ -0,0 +1,45 @@
+package main
+
+import (
+ "log"
+ "net/http"
+ "browser/phantom"
+)
+
+func param(r *http.Request, name string) string {
+ if len(r.Form["src"]) > 0 {
+ return r.Form["src"][0]
+ }
+ return ""
+}
+
+func main() {
+ http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
+ r.ParseForm()
+
+ log.Printf("%v", r.Form)
+
+ url := param(r, "src")
+
+ if url != "" {
+ filename, err := phantom.Screenshot(r.Form["src"][0])
+
+ if err != nil {
+ log.Printf("Error creating screenshot: %s", err)
+ http.Error(w, "Could not create screenshot", http.StatusInternalServerError)
+ } else {
+ http.ServeFile(w, r, filename)
+ return
+ }
+ }
+ http.Error(w, "missing src parameter", http.StatusInternalServerError)
+
+ })
+
+ port := ":3000"
+ log.Printf("Running and listening to port %s", port)
+
+ if err := http.ListenAndServe(port, nil); err != nil {
+ log.Panicln("Could not start server:", err)
+ }
+}
@@ -0,0 +1,17 @@
+var page = new WebPage(), address, output, width, height;
+
+address = phantom.args[0];
+output = phantom.args[3];
+
+page.viewportSize = { width: phantom.args[1], height: phantom.args[2] };
+page.open(address, function (status) {
+ if (status !== 'success') {
+ console.log('Unable to load the address!');
+ exit(1)
+ } else {
+ window.setTimeout(function () {
+ page.render(output);
+ phantom.exit();
+ }, 400);
+ }
+});
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@@ -0,0 +1,49 @@
+package mogrify
+
+import (
+ "io"
+ "log"
+ "os"
+ "os/exec"
+)
+
+func init() {
+ _, err := exec.LookPath("gm")
+ if err != nil {
+ log.Print("Could not locate gm (GraphicsMagic) tool in path")
+ os.Exit(1)
+ }
+}
+
+func Resize(out io.Writer, in io.Reader, size string) error {
+ cmd := exec.Command("gm", "convert", "-resize", size, "-colorspace", "RGB", "-", "-")
+ stdin, err := cmd.StdinPipe()
+ if err != nil {
+ return err
+ }
+
+ stdout, err := cmd.StdoutPipe()
+ if err != nil {
+ return err
+ }
+
+ err = cmd.Start()
+ if err != nil {
+ log.Print(err)
+ return err
+ }
+
+ // copy stream ot gm tool, close stream once done and
+ // read the output of the tool back to the out stream
+ io.Copy(stdin, in)
+ stdin.Close()
+ io.Copy(out, stdout)
+
+ return cmd.Wait()
+}
+
+func ResizeFile(filename string, size string) error {
+ cmd := exec.Command("gm", "convert", "-resize", size, "-colorspace", "RGB", filename)
+ return cmd.Run()
+}
+
@@ -0,0 +1,61 @@
+package mogrify
+
+import (
+ "bytes"
+ "io"
+ "log"
+ "os"
+ "testing"
+)
+
+func copyFile(src, dst string) (int64, error) {
+ sf, err := os.Open(src)
+ if err != nil {
+ log.Fatal(err)
+ return 0, err
+ }
+ defer sf.Close()
+ df, err := os.Create(dst)
+ if err != nil {
+ log.Fatal(err)
+ return 0, err
+ }
+ defer df.Close()
+ return io.Copy(df, sf)
+}
+
+func TestResizeFile(t *testing.T) {
+
+ targetPath := os.TempDir() + "/image1.jpg"
+ _, err := copyFile("../assets/image.jpg", targetPath)
+
+ if err != nil {
+ log.Printf("Could not copy file to tmp folder: %s", err)
+ t.Fail()
+ }
+
+ ResizeFile(targetPath, "50x50")
+}
+
+func TestResizeStream(t *testing.T) {
+ image, err := os.Open("../assets/image.jpg")
+ if err != nil {
+ t.Fail()
+ }
+
+ output, err := os.Create( os.TempDir() + "/image2.jpg")
+ if err != nil {
+ t.Fail()
+ }
+
+ ResizeStream(output, image, "50x50")
+}
+
+func TestResizeFileDoesntExist(t *testing.T) {
+ buf := bytes.NewBuffer(nil)
+ in := bytes.NewBuffer(nil)
+ err := Resize(buf, in, "50x50")
+ if err == nil {
+ t.Fail()
+ }
+}
@@ -0,0 +1,62 @@
+package phantom
+
+import (
+ "log"
+ "os"
+ "os/exec"
+ "path/filepath"
+ "strconv"
+ "time"
+)
+
+var phantomPath string
+var rand uint32
+var dir string
+
+func init() {
+ dir = os.TempDir()
+ rand = reseed()
+
+ var err error
+
+ phantomPath, err = exec.LookPath("phantomjs")
+
+ if err != nil {
+ log.Fatalf("Cannot find phantomjs executable in bath")
+ }
+}
+
+func reseed() uint32 {
+ return uint32(time.Now().UnixNano() + int64(os.Getpid()))
+}
+
+func nextSuffix() string {
+ r := rand
+ r = r*1664525 + 1013904223 // constants from Numerical Recipes
+ rand = r
+ return strconv.Itoa(int(1e9 + r%1e9))[1:]
+}
+
+func tempPngFileName() string {
+ return filepath.Join(dir, "img", nextSuffix(), ".png")
+}
+
+func Screenshot(url string) (string, error) {
+ log.Printf("Screenshotting %s", url)
+
+ filename := tempPngFileName()
+
+ cmd := exec.Command(phantomPath, "render.js", url, "1024", "786", filename)
+
+ // connect to STDIN / STDOUT
+ cmd.Stdout = os.Stdout
+ cmd.Stderr = os.Stderr
+ err := cmd.Run()
+
+ if err != nil {
+ log.Printf("Error: %s", err)
+ return "", err
+ }
+
+ return filename, nil
+}
@@ -0,0 +1,10 @@
+package phantom
+
+import (
+ "testing"
+)
+
+func TestScreenshot(t *testing.T) {
+
+ phantomScreenshot("http://www.snowdevil.ca")
+}

0 comments on commit db70925

Please sign in to comment.