├── HtmlToJpeg.php ├── README.md ├── download.php ├── index.php ├── js └── render.js ├── temp └── index.html └── test.html /HtmlToJpeg.php: -------------------------------------------------------------------------------- 1 | config = array( 30 | 'folder' => 'temp', 31 | 'action_url' => 'download.php', 32 | 'hidden_image_names' => 'img_values', 33 | 'content_class_name' => 'htmltojpeg-container', 34 | 'form_id' => 'myForm', 35 | 'download_button_label' => 'Save and Download Images', 36 | 'append_scripts' => array( 37 | 'js/render.js' 38 | )); 39 | } 40 | 41 | /** 42 | * Output Method 43 | * Form Creator 44 | * @return string 45 | */ 46 | 47 | 48 | public function output() 49 | { 50 | $output = '
'; 58 | 59 | $output .= ""; 64 | 65 | $output .= $this->appendJsFiles(); 66 | 67 | return $output; 68 | } 69 | 70 | 71 | /** 72 | * @param string $html 73 | */ 74 | 75 | public function renderHtml($html = '') 76 | { 77 | $this->html_contents[] = $html; 78 | } 79 | 80 | /** 81 | * View renderer 82 | * @param string $src 83 | */ 84 | 85 | public function renderView($src = '') 86 | { 87 | try { 88 | 89 | if (!file_exists($src)) { 90 | throw new Exception ($src . ' does not exist'); 91 | } else { 92 | ob_start(); 93 | include($src); 94 | $content = ob_get_contents(); 95 | ob_end_clean(); 96 | } 97 | 98 | } catch (Exception $e) { 99 | echo "Message : " . $e->getMessage(); 100 | echo "Code : " . $e->getCode(); 101 | die(); 102 | } 103 | 104 | return $this->renderHtml($content); 105 | } 106 | 107 | /** 108 | * Download images as a zip 109 | */ 110 | 111 | public function download() 112 | { 113 | $zipname = $this->saveImages(); 114 | 115 | if ($zipname == "") { 116 | echo "error"; 117 | die; 118 | } 119 | 120 | header('Content-Type: application/zip'); 121 | header('Content-disposition: attachment; filename=' . $zipname); 122 | header('Content-Length: ' . filesize($zipname)); 123 | readfile($zipname); 124 | } 125 | 126 | /** 127 | * Save Images as a Zip From Post 128 | * @return string 129 | */ 130 | public function saveImages() 131 | { 132 | $posts = isset($_POST[$this->config['hidden_image_names']]) ? $_POST[$this->config['hidden_image_names']] : array(); 133 | 134 | if (empty($posts)) { 135 | return ""; 136 | } 137 | 138 | $imageprefix = uniqid(rand(), true); 139 | $zipname = uniqid(rand(), true) . '.zip'; 140 | 141 | 142 | foreach ($posts as $key => $post) { 143 | $files[] = $this->base64_to_jpeg($post, $imageprefix . '-' . $key . '.jpg', $this->config['folder']); 144 | } 145 | 146 | if (!$this->create_zip($files, $zipname, $this->config['folder'])) { 147 | return ""; 148 | } 149 | 150 | return $zipname; 151 | } 152 | 153 | /** 154 | * @param string $base64_string 155 | * @param string $output_file 156 | * @param string folder 157 | * @return mixed 158 | */ 159 | 160 | private function base64_to_jpeg($base64_string, $output_file, $folder = "") 161 | { 162 | $ifp = fopen($folder . '/' . $output_file, "wb"); 163 | $data = explode(',', $base64_string); 164 | 165 | fwrite($ifp, base64_decode($data[1])); 166 | fclose($ifp); 167 | 168 | return $output_file; 169 | } 170 | 171 | /** 172 | * @param array $files 173 | * @param string $destination 174 | * @param string $folder 175 | * @return bool 176 | */ 177 | 178 | private function create_zip($files = array(), $destination = '', $folder = "") 179 | { 180 | 181 | $valid_files = array(); 182 | 183 | if (is_array($files)) { 184 | 185 | foreach ($files as $file) { 186 | //make sure the file exists 187 | if (file_exists($folder . '/' . $file)) { 188 | $valid_files[] = $file; 189 | } 190 | } 191 | } 192 | 193 | if (count($valid_files)) { 194 | 195 | $zip = new ZipArchive(); 196 | if ($zip->open($destination, ZIPARCHIVE::CREATE) !== true) { 197 | 198 | return false; 199 | } 200 | 201 | foreach ($valid_files as $file) { 202 | $zip->addFile($folder . '/' . $file, $file); 203 | } 204 | 205 | $zip->close(); 206 | 207 | return file_exists($destination); 208 | } else { 209 | return false; 210 | } 211 | } 212 | 213 | /** 214 | * @return string 215 | */ 216 | 217 | private function appendJsFiles() 218 | { 219 | $htm = ''; 220 | $scripts = $this->config['append_scripts']; 221 | 222 | foreach ($scripts as $script) { 223 | $htm .= ''; 224 | } 225 | 226 | return $htm; 227 | } 228 | 229 | /** 230 | * @param string $key 231 | * @param string $value 232 | */ 233 | 234 | public function setConfig($key, $value = "") 235 | { 236 | $this->config[$key] = $value; 237 | } 238 | } 239 | 240 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Html to Jpeg with Php and html2canvas 2 | Multiple html pages convert to jpeg with html2canvas and PHP. 3 | You can easily convert your html or php pages to jpeg with this library. 4 | 5 | [Demo](http://eray.info/demo/html-to-jpeg-php) 6 | 7 | ##Example 8 | 9 | You need to create 2 php files.I named "index.php" and "download.php". You have to define your html pages into the "index.php" and after that you should call download function on "download.php" .You can easily change file names with library config. 10 | 11 | ###Index.php 12 | 13 | Define to html2canvas and jquery scripts. 14 | ```html 15 | 16 | 17 | 18 | 19 | ``` 20 | 21 | Add these codes into to body 22 | ```php 23 | include('HtmlToJpeg.php'); 24 | $html2Jpeg = new HtmlToJpeg(); 25 | 26 | //Pages 27 | $html2Jpeg->renderHtml("This is a new paragraph! 10 |
This is a new paragraph!
11 |
This is a new sentence without a paragraph break, in bold italics.
12 |