Skip to content

Commit 4759633

Browse files
committed
Original import
0 parents  commit 4759633

File tree

3 files changed

+172
-0
lines changed

3 files changed

+172
-0
lines changed

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License
2+
3+
Copyright (c) 2010 Matthieu Bontemps
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is furnished
10+
to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.markdown

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Snappy
2+
3+
Snappy is a PHP5 library allowing thumbnail, snapshot or PDF generation from a url or a html page. This is a simple PHP wrapper for the wkhtmltopdf/wkhtmltoimage executable.
4+
5+
## Credits
6+
7+
All credits go to the original authors of [wkhtmltopdf](http://github.com/antialize/wkhtmltopdf).

src/SnappyImage.php

+144
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
<?php
2+
3+
/**
4+
* Use this class to create a snapshot / a thumbnail from a url
5+
*
6+
* @package Snappy
7+
* @author Matthieu Bontemps<[email protected]>
8+
*/
9+
class SnappyImage
10+
{
11+
protected $executable;
12+
13+
protected $options = array(
14+
'allow' => null, // Allow the file or files from the specified folder to be loaded (repeatable)
15+
'background' => null, // Do print background (default)
16+
'no-background' => null, // Do not print background
17+
'cookie' => array(), // Set an additional cookie (repeatable)
18+
'cookie-jar' => null, // Read and write cookies from and to the supplied cookie jar file
19+
'crop-h' => null, // Set height for croping
20+
'crop-w' => null, // Set width for croping
21+
'crop-x' => null, // Set x coordinate for croping (default 0)
22+
'crop-y' => null, // Set y coordinate for croping (default 0)
23+
'custom-header' => array(), // Set an additional HTTP header (repeatable)
24+
'custom-header-propagation' => null, // Add HTTP headers specified by --custom-header for each resource request.
25+
'no-custom-header-propagation' => null, // Do not add HTTP headers specified by --custom-header for each resource request.
26+
'debug-javascript' => null, // Show javascript debugging output
27+
'no-debug-javascript' => null, // Do not show javascript debugging output (default)
28+
'encoding' => null, // Set the default text encoding, for input
29+
'f' => null, // Output format
30+
'images' => null, // Do load or print images (default)
31+
'no-images' => null, // Do not load or print images
32+
'disable-javascript' => null, // Do not allow web pages to run javascript
33+
'enable-javascript' => null, // Do allow web pages to run javascript (default)
34+
'javascript-delay' => null, // Wait some milliseconds for javascript finish (default 200)
35+
'load-error-handling' => null, // Specify how to handle pages that fail to load: abort, ignore or skip (default abort)
36+
'disable-local-file-access' => null, // Do not allowed conversion of a local file to read in other local files, unless explecitily allowed with allow
37+
'enable-local-file-access' => null, // Allowed conversion of a local file to read in other local files. (default)
38+
'minimum-font-size' => null, // Minimum font size
39+
'password' => null, // HTTP Authentication password
40+
'disable-plugins' => null, // Disable installed plugins (default)
41+
'enable-plugins' => null, // Enable installed plugins (plugins will likely not work)
42+
'post' => array(), // Add an additional post field
43+
'post-file' => array(), // Post an additional file
44+
'print-media-type' => null, // Use print media-type instead of screen
45+
'no-print-media-type' => null, // Do not use print media-type instead of screen (default)
46+
'proxy' => null, // Use a proxy
47+
'readme' => null, // Output program readme
48+
'scale-h' => null, // Set height for resizing
49+
'scale-w' => null, // Set width for resizing
50+
'disable-smart-shrinking' => null, // Disable the intelligent shrinking strategy used by WebKit that makes the pixel/dpi ratio none constant
51+
'enable-smart-shrinking' => null, // Enable the intelligent shrinking strategy used by WebKit that makes the pixel/dpi ratio none constant (default)
52+
'stop-slow-scripts' => null, // Stop slow running javascripts
53+
'no-stop-slow-scripts' => null, // Do not stop slow running javascripts (default)
54+
'transparent' => null, // Make the background transparrent in pngs *
55+
'use-xserver' => null, // Use the X server (some plugins and other stuff might not work without X11)
56+
'user-style-sheet' => null, // Specify a user style sheet, to load with every page
57+
'username' => null, // HTTP Authentication username
58+
'zoom' => null, // Use this zoom factor (default 1)
59+
);
60+
61+
/**
62+
* Save a url or file location to an image.
63+
*
64+
* @param string Url of the page
65+
* @param string Path of the future image
66+
* @return string Path to the image
67+
*/
68+
public function saveImage($url, $path)
69+
{
70+
$this->mergeOptions($options);
71+
$command = $this->buildImageCommand($url, $path);
72+
$this->exec($command);
73+
return true;
74+
}
75+
76+
public function setExecutable($executable)
77+
{
78+
$this->executable = $executable;
79+
}
80+
81+
/**
82+
* Set a wkhtmltoimage option. Be aware that option values are NOT validated
83+
* and that it is your responsibility to validate user inputs.
84+
*
85+
* @param string Option
86+
* @param string|array Value. Null to unset the option.
87+
* @return void
88+
*/
89+
public function setOption($option, $value = null)
90+
{
91+
if(!in_array($option, $this->options)) {
92+
throw new Exception("Invalid option $option");
93+
}
94+
$this->options[$option] = $value;
95+
}
96+
97+
/**
98+
* Merge wkhtmltoimage options (passed as an array) with current options
99+
*
100+
* @param array Array of options
101+
* @return void
102+
*/
103+
public function mergeOptions(array $options)
104+
{
105+
foreach($options as $key => $value) {
106+
$this->setOption($key, $value);
107+
}
108+
}
109+
110+
/**
111+
* Return the command to wkhtmltoimage using the options attributes
112+
*
113+
* @param string Url or file location of the page to process
114+
* @param string File location to the image-to-be
115+
* @return string The command
116+
*/
117+
protected function buildImageCommand($url, $path)
118+
{
119+
$command = $this->executable;
120+
121+
foreach($this->options as $key => $value) {
122+
if(null !== $value && false !== $value) {
123+
if(true === $value) {
124+
$command .= " --$key";
125+
} elseif(is_array($value)) {
126+
foreach($value as $v) {
127+
$command .= " --$key $v";
128+
}
129+
} else {
130+
$command .= " --$key $value";
131+
}
132+
}
133+
}
134+
135+
$command .= " $url $path";
136+
137+
return $command;
138+
}
139+
140+
protected function exec($command)
141+
{
142+
return shell_exec($command);
143+
}
144+
}

0 commit comments

Comments
 (0)