/** * Get singleton instance * * @access public * @return \JonnyW\PhantomJs\Client */ public static function getInstance($params = []) { if (!self::$instance instanceof ServiceContainer) { self::$instance = new ServiceContainer(); self::$instance->load($params); } return self::$instance; }
require '../vendor/autoload.php'; use JonnyW\PhantomJs\Client; use JonnyW\PhantomJs\DependencyInjection\ServiceContainer; // Create a file with a custom name // e.g. custom_procedure.proc and save it // somewhere. Set the parameters below to // the name of your file and the location of // your file. You can have many files in the // same location and you only need to create // 1 procedure loader with the path to your // files. The only restriction is the extension // your files must be .proc $fileName = 'custom_procedure'; $filePath = '/path/to/your/procedure/'; $serviceContainer = ServiceContainer::getInstance(); $procedureLoaderFactory = $serviceContainer->get('procedure_loader_factory'); $procedureLoader = $procedureLoaderFactory->createProcedureLoader($filePath); // Set the path to your custom procedure(s) $client = Client::getInstance(); $client->getProcedureLoader()->addLoader($procedureLoader); // Add new loader with path to you procedures to the chain loader $request = $client->getMessageFactory()->createRequest(); $response = $client->getMessageFactory()->createResponse(); $request->setType($fileName); // Set the request type to the name of your procedure you want to execute for this request $client->send($request, $response); var_dump($response); // If your debug log contains 'SyntaxError: Parse error' // then your custom procedure has a javascript error. Try // setting $client->debug(true) before making your request
/** * getProcedure function. * * @access protected * @param string $id * @return \JonnyW\PhantomJs\Procedure\ProcedureInterface */ protected function getProcedure($id) { return ServiceContainer::getInstance()->get('procedure_loader')->load($id); }
/** * Get client instance. * * @return \JonnyW\PhantomJs\Client */ protected function getClient() { $serviceContainer = ServiceContainer::getInstance(); $client = new Client($serviceContainer->get('engine'), $serviceContainer->get('procedure_loader'), $serviceContainer->get('procedure_compiler'), $serviceContainer->get('message_factory')); return $client; }
/** * Get dependency injection container. * * @access public * @return \JonnyW\PhantomJs\DependencyInjection\ServiceContainer */ public function getContainer() { return ServiceContainer::getInstance(); }
/** * Rasterizes an SVG image to a PNG. * * Uses phantomjs to save the SVG as a PNG image at the specified size. * * @access public * * @param string $file The path to the file that should be rasterized. * @param string $dest The path to the directory where the output PNG should be saved. * @param integer $columns The number of columns in the output image. 0 = maintain aspect ratio based on $rows. * @param integer $rows The number of rows in the output image. 0 = maintain aspect ratio based on $columns. */ public static function rasterize($file, $dest, $columns, $rows) { // check the input if (!file_exists($file)) { return false; } if (!file_exists($dest) || !is_dir($dest)) { return false; } // figure out the output width and height $svgTmp = new Respimg($file); $width = (double) $svgTmp->getImageWidth(); $height = (double) $svgTmp->getImageHeight(); $new_width = $columns; $new_height = $rows; $x_factor = $columns / $width; $y_factor = $rows / $height; if ($rows < 1) { $new_height = round($x_factor * $height); } elseif ($columns < 1) { $new_width = round($y_factor * $width); } // get the svg data $svgdata = file_get_contents($file); // figure out some path stuff $dest = rtrim($dest, '/'); $filename = substr($file, strrpos($file, '/') + 1); $filename_base = substr($filename, 0, strrpos($filename, '.')); // setup the request $client = Client::getInstance(); $serviceContainer = ServiceContainer::getInstance(); $procedureLoaderFactory = $serviceContainer->get('procedure_loader_factory'); $procedureLoader = $procedureLoaderFactory->createProcedureLoader(__DIR__); $client->getProcedureLoader()->addLoader($procedureLoader); $request = new RespimgCaptureRequest(); $request->setType('svg2png'); $request->setMethod('GET'); $request->setSVG(base64_encode($svgdata)); $request->setViewportSize($new_width, $new_height); $request->setRasterFile($dest . '/' . $filename_base . '-w' . $new_width . '.png'); $request->setWidth($new_width); $request->setHeight($new_height); $response = $client->getMessageFactory()->createResponse(); // send + return $client->send($request, $response); return true; }
/** * Get singleton instance * * @access public * @return \JonnyW\PhantomJs\Client */ public static function getInstance() { if (!self::$instance instanceof ClientInterface) { $serviceContainer = ServiceContainer::getInstance(); self::$instance = new static($serviceContainer->get('engine'), $serviceContainer->get('procedure_loader'), $serviceContainer->get('procedure_compiler'), $serviceContainer->get('message_factory')); } return self::$instance; }