1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
|
<?php use Knp\Snappy\Pdf; use Knp\Snappy\Image;
require 'vendor/autoload.php';
$baseDir = __DIR__; $pdfBinaryFile = $baseDir . '/vendor/wemersonjanuario/wkhtmltopdf-windows/bin/64bit/wkhtmltopdf.exe'; $imageBinaryFile = $baseDir . '/vendor/wemersonjanuario/wkhtmltopdf-windows/bin/64bit/wkhtmltoimage.exe';
if (strpos(PHP_OS, 'Linux') !== false) { $h4ccPath = $baseDir . '/vendor/h4cc/'; $systemBit = exec('getconf LONG_BIT'); if ($systemBit == '32') { $pdfBinaryFile = $h4ccPath . 'wkhtmltopdf-i386/bin/wkhtmltopdf-i386'; $imageBinaryFile = $h4ccPath . 'wkhtmltoimage-i386/bin/wkhtmltoimage-i386'; } else { $pdfBinaryFile = $h4ccPath . 'wkhtmltopdf-amd64/bin/wkhtmltopdf-amd64'; $imageBinaryFile = $h4ccPath . 'wkhtmltoimage-amd64/bin/wkhtmltoimage-amd64'; } }
$html = <<<HTML <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8"> </head> <body> <p style="font-family: 'Microsoft YaHei'">Hello World, 你好 世界!</p> </body> </html> HTML;
$pdfSnappy = new Pdf($pdfBinaryFile); $pdfSnappy->generateFromHtml($html, $baseDir . '/test.pdf', [], true);
$imageSnappy = new Image($imageBinaryFile); $imageSnappy->generateFromHtml($html, $baseDir . '/test.png', [ 'width' => 100, 'height' => 100, 'quality' => 100 ], true);
$backgroundImage = $baseDir . '/bg.jpg'; $qrcodeImage = $baseDir . '/qrcode.png'; $logoImage = $baseDir . '/logo.png'; $title = "海报标题AAAABBBBCCCCDDDD"; $outputImage = $baseDir . '/poster.png';
list ($width, $height) = getimagesize($backgroundImage); $snappy = new Image($imageBinaryFile); $snappy->generateFromHtml(generateHtml($backgroundImage, $qrcodeImage, $logoImage, $title), $outputImage, [ 'width' => $width, 'height' => $height, 'quality' => 100 ], true);
function generateHtml($backgroundImage, $qrcodeImage, $logoImage, $title) { $html = <<<HTML <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"> <title></title> <style> body, html { width: 100%; height: 100%; margin: 0; padding: 0; font-family: "Microsoft YaHei" } .container { position: relative; display: flex; width: 100%; height: 100%; } .img-bg { position: absolute; top: 0; left: 0; width: 100%; } .content { position: relative; z-index: 999; left: 25%; top: 37.5%; width: 50%; height: 25%; border-radius: 7px; } .img-qrcode { width: 100%; height: 100%; } .img-logo { position: absolute; top: 40%; left: 40%; width: 20%; height: 20%; } .title { width: 90%; margin: 2px auto; overflow: hidden; font-size: 20px; display: -webkit-box; -webkit-line-clamp: 2; text-overflow: ellipsis; -webkit-box-orient: vertical; text-align: center; } </style> </head> <body> <div class="container"> <img src="{$backgroundImage}" class="img-bg"> <div class="content"> <img src="{$qrcodeImage}" class="img-qrcode"> <img src="{$logoImage}" class="img-logo"> <div class="title">{$title}</div> </div> </div> </body> </html> HTML;
return $html; }
|