Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php PDF:无法从给定输入创建图像_Php_Symfony_Pdf - Fatal编程技术网

Php PDF:无法从给定输入创建图像

Php PDF:无法从给定输入创建图像,php,symfony,pdf,Php,Symfony,Pdf,我在Symfony应用程序中使用它将PDF更改为图像。以下是我正在使用的函数: public function savePdfPreviewImage($fullFilePath, $thumbnailPath) { $pdf = new Pdf($fullFilePath); $pdf->saveImage($thumbnailPath); return $this; } 当给定PDF的路径时,库将返回以下消息: 无法从给定的输入创建图像 我如何着手找到解决

我在Symfony应用程序中使用它将PDF更改为图像。以下是我正在使用的函数:

public function savePdfPreviewImage($fullFilePath, $thumbnailPath)
{
    $pdf = new Pdf($fullFilePath);
    $pdf->saveImage($thumbnailPath); 

    return $this;
}
当给定PDF的路径时,库将返回以下消息:

无法从给定的输入创建图像

我如何着手找到解决方案?

到目前为止,我已尝试使用
ls
验证该文件是否存在于应用程序认为存在的位置。我还尝试在pdf阅读器中打开该文件(文件扩展名为
.pdf
),以验证它是否已损坏。这两次行动都没有产生任何线索

=====

编辑1:我将此消息追溯到
Imagine.php
文件,在该文件中删除了一条错误抑制行。这给了我一个稍微不那么晦涩的信息:

警告:imagecreatefromstring():数据的格式不可识别

====


编辑2:我还验证了是否安装了ghostscript。
gs
命令可从我的服务器环境中获得。我还验证了为
$thumbnailPath
提供的路径是一个以.jpg结尾的有效路径/文件名。

您不需要lib,仅使用imagick即可

$pdf = new \Imagick();
$pdf->setColorspace(\Imagick::COLORSPACE_SRGB);
$pdf->readImage($srcPath);
$pdf->setIteratorIndex($pageNo);
$pdf->writeImage($outPath);

(Imagick将从$outPath识别文件扩展名)

我找到了问题所在

转换为PDF实际上表现得很好。行为不端的是应用程序中后来对Imagine.php库的调用,未能成功地调整代码成功创建的图像的大小。下面是允许我看到以下内容的代码:

public function savePdfPreviewImage($fullFilePath, $thumbnailPath)
{

      //$pdf = new Pdf($fullFilePath);
      //$pdf->saveImage($thumbnailPath);    //This gives us "An image could not be created from the given input" and "Data is not in a recognized format"
                                            //Let's try it with a manual call to GhostScript instead ...

    exec(
        'gs -o ' .                //This creates the image successfully, but the error still shows up.
        $thumbnailPath .                    //That means the error isn't coming from here, since we're no longer calling any external PHP libraries.
        ' -sDEVICE=jpeg ' .
        $fullFilePath
    );

    return $this;
}

谢谢你的回答。我仍然得到“无法从给定输入创建图像”这充其量只是一个解决办法,但可能根本不是答案。当5行代码允许您删除依赖项时,这绝对是值得的。根据OP的说法,问题在于尺寸的调整,但问题中并没有任何迹象表明这一点。嗯,可能是这样的。但是在这个项目中使用这个库可能有一个很好的理由,但是这个问题本身并没有提供足够的信息。一个好的建议并不是一个好的答案。你是否验证了输入图像的格式是否正确?您是否可以提高调试级别以查看是否打印了任何更明显的消息?您是否尝试过其他pdf文件?尝试使用一个简单的单页文件来验证它是否完全不起作用,或者只是不适用于某些文件。我已经使用了四个不同的PDF文件,包括一个单页文件。