Jelajahi Sumber

Merge pull request #77 from tlaverdure/master

Added the procedure compiler to the Client class to match instruction…
Jonny Wenmoth 10 tahun lalu
induk
melakukan
8c0c2d72ed

+ 11 - 0
src/JonnyW/PhantomJs/Client.php

@@ -197,4 +197,15 @@ class Client implements ClientInterface
     {
         return $this->procedure;
     }
+
+    /**
+     * Get procedure compiler.
+     *
+     * @access public
+     * @return \JonnyW\PhantomJs\Procedure\ProcedureCompilerInterface
+     */
+    public function getProcedureCompiler()
+    {
+        return $this->procedureCompiler;
+    }
 }

+ 2 - 0
src/JonnyW/PhantomJs/Http/AbstractRequest.php

@@ -204,6 +204,8 @@ abstract class AbstractRequest
     {
         $this->viewportWidth  = (int) $width;
         $this->viewportHeight = (int) $height;
+
+        return $this;
     }
 
     /**

+ 91 - 40
src/JonnyW/PhantomJs/Http/CaptureRequest.php

@@ -12,7 +12,7 @@ namespace JonnyW\PhantomJs\Http;
 use JonnyW\PhantomJs\Exception\NotWritableException;
 
 /**
- * PHP PhantomJs
+ * PHP PhantomJs.
  *
  * @author Jon Wenmoth <contact@jonnyw.me>
  */
@@ -20,10 +20,9 @@ class CaptureRequest extends AbstractRequest
     implements CaptureRequestInterface
 {
     /**
-     * Request type
+     * Request type.
      *
      * @var string
-     * @access protected
      */
     protected $type;
 
@@ -31,65 +30,75 @@ class CaptureRequest extends AbstractRequest
      * File to save output.
      *
      * @var string
-     * @access protected
      */
     protected $outputFile;
 
     /**
-     * Rect top
+     * Rect top.
      *
      * @var int
-     * @access protected
      */
     protected $rectTop;
 
     /**
-     * Rect left
+     * Rect left.
      *
      * @var int
-     * @access protected
      */
     protected $rectLeft;
 
     /**
-     * Rect width
+     * Rect width.
      *
      * @var int
-     * @access protected
      */
     protected $rectWidth;
 
     /**
-     * Rect height
+     * Rect height.
      *
      * @var int
-     * @access protected
      */
     protected $rectHeight;
 
     /**
-     * Internal constructor
+     * Capture Format.
+     *
+     * @var string
+     */
+    protected $format;
+
+    /**
+     * Capture Quality.
+     *
+     * @var int
+     */
+    protected $quality;
+
+    /**
+     * Internal constructor.
+     *
+     * @param string $url     (default: null)
+     * @param string $method  (default: RequestInterface::METHOD_GET)
+     * @param int    $timeout (default: 5000)
      *
-     * @access public
-     * @param  string                                $url     (default: null)
-     * @param  string                                $method  (default: RequestInterface::METHOD_GET)
-     * @param  int                                   $timeout (default: 5000)
      * @return \JonnyW\PhantomJs\Http\CaptureRequest
      */
     public function __construct($url = null, $method = RequestInterface::METHOD_GET, $timeout = 5000)
     {
         parent::__construct($url, $method, $timeout);
 
-        $this->rectTop    = 0;
-        $this->rectLeft   = 0;
-        $this->rectWidth  = 0;
+        $this->rectTop = 0;
+        $this->rectLeft = 0;
+        $this->rectWidth = 0;
         $this->rectHeight = 0;
+        $this->format = 'jpeg';
+        $this->quality = 75;
     }
 
     /**
-     * Get request type
+     * Get request type.
      *
-     * @access public
      * @return string
      */
     public function getType()
@@ -102,10 +111,10 @@ class CaptureRequest extends AbstractRequest
     }
 
     /**
-     * Set request type
+     * Set request type.
+     *
+     * @param string $type
      *
-     * @access public
-     * @param  string                                 $type
      * @return \JonnyW\PhantomJs\Http\AbstractRequest
      */
     public function setType($type)
@@ -118,19 +127,19 @@ class CaptureRequest extends AbstractRequest
     /**
      * Set viewport size.
      *
-     * @access public
-     * @param  int                                    $width
-     * @param  int                                    $height
-     * @param  int                                    $top    (default: 0)
-     * @param  int                                    $left   (default: 0)
+     * @param int $width
+     * @param int $height
+     * @param int $top    (default: 0)
+     * @param int $left   (default: 0)
+     *
      * @return \JonnyW\PhantomJs\Http\AbstractRequest
      */
     public function setCaptureDimensions($width, $height, $top = 0, $left = 0)
     {
-        $this->rectWidth  = (int) $width;
+        $this->rectWidth = (int) $width;
         $this->rectHeight = (int) $height;
-        $this->rectTop    = (int) $top;
-        $this->rectLeft   = (int) $left;
+        $this->rectTop = (int) $top;
+        $this->rectLeft = (int) $left;
 
         return $this;
     }
@@ -138,7 +147,6 @@ class CaptureRequest extends AbstractRequest
     /**
      * Get rect top.
      *
-     * @access public
      * @return int
      */
     public function getRectTop()
@@ -149,7 +157,6 @@ class CaptureRequest extends AbstractRequest
     /**
      * Get rect left.
      *
-     * @access public
      * @return int
      */
     public function getRectLeft()
@@ -160,7 +167,6 @@ class CaptureRequest extends AbstractRequest
     /**
      * Get rect width.
      *
-     * @access public
      * @return int
      */
     public function getRectWidth()
@@ -171,7 +177,6 @@ class CaptureRequest extends AbstractRequest
     /**
      * Get rect height.
      *
-     * @access public
      * @return int
      */
     public function getRectHeight()
@@ -182,9 +187,10 @@ class CaptureRequest extends AbstractRequest
     /**
      * Set file to save output.
      *
-     * @access public
-     * @param  string                                           $file
+     * @param string $file
+     *
      * @throws \JonnyW\PhantomJs\Exception\NotWritableException
+     *
      * @return \JonnyW\PhantomJs\Http\CaptureRequest
      */
     public function setOutputFile($file)
@@ -201,11 +207,56 @@ class CaptureRequest extends AbstractRequest
     /**
      * Get output file.
      *
-     * @access public
      * @return string
      */
     public function getOutputFile()
     {
         return $this->outputFile;
     }
+
+    /**
+     * Get image format of the capture.
+     *
+     * @return string
+     */
+    public function getFormat()
+    {
+        return $this->format;
+    }
+
+    /**
+     * Set image format of capture.
+     * options: pdf, png, jpeg, bmp, ppm, gif.
+     *
+     * @param string $format
+     */
+    public function setFormat($format)
+    {
+        $this->format = $format;
+
+        return $this;
+    }
+
+    /**
+     * Get quality of capture.
+     *
+     * @return string
+     */
+    public function getQuality()
+    {
+        return $this->quality;
+    }
+
+    /**
+     * Set quality of the capture.
+     * example: 0 - 100.
+     *
+     * @param int $format
+     */
+    public function setQuality($quality)
+    {
+        $this->quality = (int) $quality;
+
+        return $this;
+    }
 }

+ 31 - 8
src/JonnyW/PhantomJs/Http/CaptureRequestInterface.php

@@ -10,7 +10,7 @@
 namespace JonnyW\PhantomJs\Http;
 
 /**
- * PHP PhantomJs
+ * PHP PhantomJs.
  *
  * @author Jon Wenmoth <contact@jonnyw.me>
  */
@@ -19,7 +19,6 @@ interface CaptureRequestInterface
     /**
      * Set viewport size.
      *
-     * @access public
      * @param int $width
      * @param int $height
      * @param int $top    (default: 0)
@@ -30,7 +29,6 @@ interface CaptureRequestInterface
     /**
      * Get rect top.
      *
-     * @access public
      * @return int
      */
     public function getRectTop();
@@ -38,7 +36,6 @@ interface CaptureRequestInterface
     /**
      * Get rect left.
      *
-     * @access public
      * @return int
      */
     public function getRectLeft();
@@ -46,7 +43,6 @@ interface CaptureRequestInterface
     /**
      * Get rect width.
      *
-     * @access public
      * @return int
      */
     public function getRectWidth();
@@ -54,7 +50,6 @@ interface CaptureRequestInterface
     /**
      * Get rect height.
      *
-     * @access public
      * @return int
      */
     public function getRectHeight();
@@ -62,7 +57,6 @@ interface CaptureRequestInterface
     /**
      * Set file to save output.
      *
-     * @access public
      * @param string $file
      */
     public function setOutputFile($file);
@@ -70,8 +64,37 @@ interface CaptureRequestInterface
     /**
      * Get output file.
      *
-     * @access public
      * @return string
      */
     public function getOutputFile();
+
+    /**
+     * Get image format of the capture.
+     *
+     * @return string
+     */
+    public function getFormat();
+
+    /**
+     * Set image format of capture.
+     * options: pdf, png, jpeg, bmp, ppm, gif.
+     *
+     * @param string $format
+     */
+    public function setFormat($format);
+
+    /**
+     * Get quality of capture.
+     *
+     * @return string
+     */
+    public function getQuality();
+
+    /**
+     * Set quality of the capture.
+     * example: 0 - 100.
+     *
+     * @param int $format
+     */
+    public function setQuality($quality);
 }

+ 7 - 5
src/JonnyW/PhantomJs/Resources/procedures/procedure_capture.partial

@@ -1,14 +1,17 @@
 
 if (status === 'success') {
-    
+
     try {
     
-        page.render('{{ input.getOutputFile() }}');
-        
+        page.render('{{ input.getOutputFile() }}', {
+            format: '{{ input.getFormat() }}',
+            quality: {{ input.getQuality() }},
+        });
+
         response.content = page.evaluate(function () {
             return document.getElementsByTagName('html')[0].innerHTML
         });
-    
+
     } catch(e) {
 
         response.status  = 500;
@@ -22,4 +25,3 @@ system.stderr.write(debug.join('\\n') + '\\n');
 system.stdout.write(JSON.stringify(response, undefined, 4));
 
 phantom.exit();
-