/** * {@inheritDoc} */ public function generate($path, $html) { $image = new Image(); $image->setBinary($this->_getBinDir() . 'wkhtmltoimage-' . $this->_getBinaryType()); foreach ($this->_options as $key => $value) { $image->setOption($key, $value); } if (is_file($path)) { unlink($path); } $image->generateFromHTML($html, $path); return new File($path); }
<?php require_once 'vendor/knplabs/knp-snappy/src/autoload.php'; $size = isset($_POST['size']) ? $_POST['size'] : 1; use Knp\Snappy\Image; $snappy = new Image('/usr/local/bin/wkhtmltoimage'); $snappy->setOption('disable-javascript', true); $snappy->setOption('disable-local-file-access', true); $snappy->setOption('quality', 100); $snappy->setOption('zoom', $size); $description = $size == 1 ? 'Normal' : ($size == 2 ? 'Big' : 'Large'); $filename = 'My NUSMods.com Timetable (' . $description . ').jpg'; header('Content-Type: image/jpeg'); header('Content-Disposition: attachment; filename=' . $filename); echo $snappy->getOutputFromHtml(urldecode($_POST['html']));
/** * @param $name * @param $value * * @return $this */ public function setOption($name, $value) { $this->snappy->setOption($name, $value); return $this; }
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; }