phpwkhtmltopdf中的破碎图像

时间:2018-04-27 06:20:50

标签: php apache image-processing wkhtmltoimage

需要注意的是,我已经在两个不同的服务器Debian 9和Ubuntu 14.04上对此进行了测试,同样的错误仍然存​​在。现在我正在使用Ubuntu 14.04和PHP 5,我已经安装了composer,我已经正确安装了wkhtmltopdf和phpwkhtmltopdf。我怎么知道这个?好wkhtmltopdf / image通过CLI工作,phpwkhtmltopdf也可以通过PHP工作,但当我尝试将图像作为内联显示器发送到客户端或下载图像时会破坏。例如;

  1. 访问所需的网址,对我们来说是test.php
  2. Phpwkhtmltopdf将发送与CLI版本连接的命令wkhtmltopdf
  3. 一旦页面加载,它将访问google.com,在屏幕上保存屏幕截图/var/www/html/tmp/page.jpg并且该图像打开/显示正常,但是当我尝试使用$image->send('page.jpg');时,发送的图像已损坏/不会打开。
  4. 我对系统进行了两次更改,我在apache2中禁用了mod_deflate,并且还在apache2的php.ini配置文件中增加了max_filesize选项。

    依赖关系

    直播示例

    http://155.254.35.63/test.php // Generate the image
    http://155.254.35.63/tmp/page.png // The image file generated
    

    test.php的

    <?php
    
    ini_set('display_errors', 1);
    ini_set('display_startup_errors', 1);
    error_reporting(E_ALL);
    
    ?>
    
    <?php
    
    $loader = require __DIR__ . '/vendor/autoload.php';
    
    ?>
    
    <?php
    
    use mikehaertl\wkhtmlto\Image;
    
    $image = new \mikehaertl\wkhtmlto\Image('https://www.google.co.uk/search?q=what+is+the+time&oq=what+is+the+time&aqs=chrome.0.69i59j69i60l3j0l2.2977j0j4&sourceid=chrome&ie=UTF-8');
    $image->setOptions(array(
    'binary' => '/usr/local/bin/wkhtmltoimage',
    'type' => 'png'
    ));
    
    $image->saveAs('/var/www/html/tmp/page.png');
    
    header('Content-Type: image/png');
    echo file_get_contents('/var/www/html/tmp/page.jpg');
    
    ?>
    

    File.php(第72至83行)

    <?php
    namespace mikehaertl\tmp;
    
    /**
     * File
     *
     * A convenience class for temporary files.
     *
     * @author Michael Härtl <haertl.mike@gmail.com>
     * @version 1.1.0
     * @license http://www.opensource.org/licenses/MIT
     */
    class File
    {
        /**
         * @var bool whether to delete the tmp file when it's no longer referenced or when the request ends.
         * Default is `true`.
         */
        public $delete = true;
    
        /**
         * @var string the name of this file
         */
        protected $_fileName;
    
        /**
         * Constructor
         *
         * @param string $content the tmp file content
         * @param string|null $suffix the optional suffix for the tmp file
         * @param string|null $prefix the optional prefix for the tmp file. If null 'php_tmpfile_' is used.
         * @param string|null $directory directory where the file should be created. Autodetected if not provided.
         */
        public function __construct($content, $suffix = null, $prefix = null, $directory = null)
        {
            if ($directory===null) {
                $directory = self::getTempDir();
            }
    
            if ($prefix===null) {
                $prefix = 'php_tmpfile_';
            }
    
            $this->_fileName = tempnam($directory,$prefix);
            if ($suffix!==null) {
                $newName = $this->_fileName.$suffix;
                rename($this->_fileName, $newName);
                $this->_fileName = $newName;
            }
            file_put_contents($this->_fileName, $content);
        }
    
        /**
         * Delete tmp file on shutdown if `$delete` is `true`
         */
        public function __destruct()
        {
            if ($this->delete) {
                unlink($this->_fileName);
            }
        }
    
        /**
         * Send tmp file to client, either inline or as download
         *
         * @param string|null $filename the filename to send. If empty, the file is streamed inline.
         * @param string $contentType the Content-Type header
         * @param bool $inline whether to force inline display of the file, even if filename is present.
         */
        public function send($filename = null, $contentType, $inline = false)
        {
            header('Pragma: public');
            header('Expires: 0');
            header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
            header('Content-Type: image/png');
            header('Content-Transfer-Encoding: binary');
    
            if ($filename!==null || $inline) {
                $disposition = $inline ? 'inline' : 'attachment';
                header("Content-Disposition: $disposition; filename=\"$filename\"");
            }
    
            readfile($this->_fileName);
        }
    
        /**
         * @param string $name the name to save the file as
         * @return bool whether the file could be saved
         */
        public function saveAs($name)
        {
            return copy($this->_fileName, $name);
        }
    
        /**
         * @return string the full file name
         */
        public function getFileName()
        {
            return $this->_fileName;
        }
    
        /**
         * @return string the path to the temp directory
         */
        public static function getTempDir()
        {
            if (function_exists('sys_get_temp_dir')) {
                return sys_get_temp_dir();
            } elseif ( ($tmp = getenv('TMP')) || ($tmp = getenv('TEMP')) || ($tmp = getenv('TMPDIR')) ) {
                return realpath($tmp);
            } else {
                return '/tmp';
            }
        }
    
        /**
         * @return string the full file name
         */
        public function __toString()
        {
            return $this->_fileName;
        }
    }
    

    的php.ini

    ;;;;;;;;;;;;;;;;
    ; File Uploads ;
    ;;;;;;;;;;;;;;;;
    
    ; Whether to allow HTTP file uploads.
    ; http://php.net/file-uploads
    file_uploads = On
    
    ; Temporary directory for HTTP uploaded files (will use system default if not
    ; specified).
    ; http://php.net/upload-tmp-dir
    ;upload_tmp_dir = /var/www/html/tmp
    
    ; Maximum allowed size for uploaded files.
    ; http://php.net/upload-max-filesize
    upload_max_filesize = 50M
    
    ; Maximum number of files that can be uploaded via a single request
    max_file_uploads = 20
    

    apache日志文件中有 none(0)错误,这实际上让我无法解决问题。我试图找到一个与开发者的决议,但不看;

    https://github.com/mikehaertl/phpwkhtmltopdf/issues/278

    我非常感谢这方面的帮助。

1 个答案:

答案 0 :(得分:0)

我检查了测试网站生成的page.jpg文件。文件本身完好无损。这意味着您的管道没有任何问题。

文件标题显示您的文件不是标准JPEG文件,而是JFIF版本。看看您是否可以设置库以生成PNG文件以解决此问题。

编辑:现在我看到生成的文件是正确的,看看你是否只能流式传输内容而不是使用$ image-&gt; send。发送给你自己:

www.pipedrive.com