public function testImagePngCreating() { $path = realpath(dirname(__FILE__) . '/../tests/tmp/'); $webshot = new Webshot(); $png = $webshot->setUrl('https://github.com')->setWidth(1200)->setHeight(800)->setTimeout(5)->saveToPng('github', $path); $this->assertFileExists($png); }
public function testTimeout() { $path = realpath(dirname(__FILE__) . '/../tests/tmp/'); $webshot = new Webshot(); $this->setExpectedException('hotrush\\Webshotter\\Exception\\TimeoutException', 'Page load timeout.'); $webshot->setUrl('http://httpbin.org/delay/10')->setWidth(1200)->setHeight(800)->setTimeout(5)->saveToPng('delay', $path); }
/** * @param string $url * @param string $path * * @return string */ public static function capturar($url = '', $path = '') { try { $webshot = new Webshot('/usr/local/bin/phantomjs'); return $webshot->setUrl($url)->setWidth(400)->setHeight(300)->setTimeout(300)->setFullPage(true)->saveToPng(md5($url), $path); } catch (\Exception $e) { return 'ERROR_NO_CONTENT'; } }
public function testTemplateRendering() { $webshot = new Webshot(); $webshot->setUrl('https://github.com')->setWidth(1200)->setHeight(800); $mock = file_get_contents(realpath(dirname(__FILE__) . '/../tests/data/template.php')); $reflection = new ReflectionClass('hotrush\\Webshotter\\Webshot'); $method = $reflection->getMethod('renderTemplate'); $method->setAccessible(true); $this->assertEquals($mock, $method->invokeArgs($webshot, array('tests/tmp/github.png'))); }
/** * Create a webshot by link * * @param Request $request * @return \Symfony\Component\HttpFoundation\Response * @throws \Exception */ public function store(Request $request) { $this->validate($request, ['url' => 'required|url', 'extension' => 'in:jpg,png,pdf', 'width' => 'integer', 'height' => 'integer', 'full_page' => 'boolean', 'filename' => 'alpha_dash', 'timeout' => 'integer', 'path' => 'string']); $filename = $request->input('filename', sha1($request->input('url'))); $fullPath = trim(str_replace(' ', '-', $request->input('path', date('Y/m/d'))), '/') . '/' . $filename . '.' . $request->input('extension', 'jpg'); $tmpPath = storage_path('app'); $webshot = new Webshot(env('PHANTOM_JS_BIN')); try { $tmpFile = $webshot->setUrl($request->input('url'))->setWidth($request->input('width', 1200))->setHeight($request->input('height', 800))->setFullPage($request->input('full_page', false))->setTimeout($request->input('timeout', 30))->{'saveTo' . ucfirst($request->input('extension', 'jpg'))}($filename, $tmpPath); // Put file to it's destination Storage::put($fullPath, file_get_contents($tmpFile)); unlink($tmpFile); return response()->json(['path' => $fullPath, 'url' => app('filesystemPublicUrl')->publicUrl(null, $fullPath)]); } catch (TimeoutException $e) { return response()->json(['message' => 'Link timeout.'], 500); } }