/** * Output the PDF as a string. * * @return string The rendered PDF as string * @throws \InvalidArgumentException */ public function output() { if ($this->html) { return $this->snappy->getOutputFromHtml($this->html, $this->options); } if ($this->file) { return $this->snappy->getOutput($this->file, $this->options); } throw new \InvalidArgumentException('Image Generator requires a html or file in order to produce output.'); }
<?php require_once 'vendor/autoload.php'; use Knp\Snappy\Image; $snappy = new Image('/usr/local/bin/wkhtmltoimage', ['format' => 'png']); $snappy->setDefaultExtension('png'); $snappy->setOptions(['crop-w' => 200, 'crop-h' => 200, 'crop-x' => 400, 'crop-y' => 20]); // Cabeçalho para o navegador entender que o conteúdo é uma imagem PNG header('Content-Type: image/png'); // Se quiser forçar o download, descomente a linha abaixo e // modifica o valor do parâmetro filename para o valor desejado //header('Content-Disposition: attachment; filename="schoolofnet-blog-home.png"'); echo $snappy->getOutput('http://www.schoolofnet.com/blog/'); // Se quiser salvar numa pasta do servidor ao invés mostrar no navegador, // comente a linha do getOutput(), // descomente a linha abaixo e arrume o segundo parâmetro. //$snappy->generate('http://www.schoolofnet.com/blog/', '/app/arquivos/son/home-blog.png');
public static function snapThumbAndSave($url, $snap_width, $snap_height, $save_width, $save_height, $is_text, $is_image, $dest) { set_time_limit(120); try { $snappy = new ThumbDL('/usr/local/bin/wkhtmltoimage'); $snappy->setOption('stop-slow-scripts', true); $snappy->setOption('width', $snap_width); $snappy->setOption('height', $snap_height); $host = parse_url($url, PHP_URL_HOST); if (strcmp($host, "www.youtube.com") == 0) { parse_str(parse_url($url, PHP_URL_QUERY), $vars); $image = $snappy->getOutput(URL::to("/util/imagewrapper?url=" . urlencode("http://img.youtube.com/vi/" . $vars['v'] . "/0.jpg"))); } else { if (strcmp($host, "youtu.be") == 0) { $image = $snappy->getOutput(URL::to("/util/imagewrapper?url=" . urlencode("http://img.youtube.com/vi" . parse_url($url, PHP_URL_PATH) . "/0.jpg"))); } else { if (strcmp($host, "vimeo.com") == 0) { $img_id = parse_url($url, PHP_URL_PATH); $img_hash = unserialize(file_get_contents("http://vimeo.com/api/v2/video{$img_id}.php")); $image = $snappy->getOutput(URL::to("/util/imagewrapper?url=" . $img_hash[0]['thumbnail_large'])); } else { if ($is_text) { $image = $snappy->getOutput($url); } else { if ($is_image) { $image = $snappy->getOutput(URL::to("/util/imagewrapper?url=" . urlencode($url))); } else { Log::error("image neither text nor image"); return false; } } } } } } catch (Exception $e) { Log::error($e->getMessage()); return false; } try { $tmpfname = tempnam("/tmp", "spreadit"); $handle = fopen($tmpfname, "w"); fwrite($handle, $image); fclose($handle); } catch (Exception $e) { Log::error($e->getMessage()); return false; } try { $resizer = Image::make($tmpfname); $resizer->resize($save_width, $save_height); $resizer->save($dest); } catch (Exception $e) { Log::error($e->getMessage()); return false; } return true; }
<?php require_once 'vendor/autoload.php'; use Knp\Snappy\Image; $snappy = new Image('/usr/local/bin/wkhtmltoimage', ['format' => 'png']); $snappy->setDefaultExtension('png'); // Cabeçalho para o navegador entender que o conteúdo é uma imagem PNG header('Content-Type: image/png'); // Se quiser forçar o download, descomente a linha abaixo e // modifica o valor do parâmetro filename para o valor desejado //header('Content-Disposition: attachment; filename="schoolofnet-blog-artigos.png"'); echo $snappy->getOutput(array('http://www.schoolofnet.com/2015/07/trabalhando-com-repository-no-laravel/', 'http://www.schoolofnet.com/2015/04/como-usar-os-metodos-magicos-no-php/', 'http://www.schoolofnet.com/2015/04/enviando-emails-utilizando-swift-mailer/', 'http://www.schoolofnet.com/2015/04/instalando-e-integrando-apache-com-php-no-windows/')); // Se quiser salvar numa pasta do servidor ao invés mostrar no navegador, // comente a linha do getOutput(), // descomente a linha abaixo e arrume o segundo parâmetro. //$snappy->generate('http://www.schoolofnet.com/blog/', '/app/arquivos/son/artigos.png');
function generate_for_gallery($theme_id) { $theme = find_theme($theme_id); if (is_null($theme)) { return false; } if ($theme["share"] != 1) { return false; } $path_image = BASE_PATH . get_thumbnail_path($theme_id); if (!file_exists(dirname($path_image))) { mkdir(dirname($path_image), 0777, true); } if (!file_exists($path_image)) { $snappy = new Image(COMMAND_WKHTMLTOIMAGE, array("format" => "png", "width" => 200, "zoom" => 0.75, "quality" => 50, "disable-javascript" => true)); $snappy->setDefaultExtension("png"); $params = array("api_type" => $theme["api_type"], "id" => $theme["cs_id"], "c" => $theme["_c"], "design" => "thumbnail"); $url = BASE_URL . "preview?" . http_build_query($params); $url = str_replace("%5B", "[", $url); $url = str_replace("%5D", "]", $url); $image = $snappy->getOutput($url); file_put_contents($path_image, $image); } return true; }