我正在使用一个基于 URL 参数 image_id
生成 PNG 图像的脚本。 。
例如:http://www.example.com/image.php?image_id=G554jGTT
现在我想将该图像放入 pdf 中,如下所示:
$imagepath = "www.example.com/image.php?id=" . $id;
$image = Zend_Pdf_Image::imageWithPath($imagepath);
当我使用本地文件时一切顺利,但当我使用 URL 时出现以下错误:
Fatal error: Uncaught exception 'Zend_Pdf_Exception' with message 'Cannot create image resource. File not found.' in E:<...>\library\Zend\Pdf\Resource\ImageFactory.php:38
有人知道怎么做吗?
谢谢!
最佳答案
您不能使用 Zend_Pdf_Image::imageWithPath
来执行此操作。原因是 ImageFacotry
需要一个文件。它使用 is_file
方法来检查文件是否存在。但是,is_file
函数不一定适用于远程文件。
解决这个问题的一种方法是下载图像并将其保存在临时名称下,然后将该临时文件放入 Zend_Pdf_Image::imageWithPath
中。要下载并保存文件,您可以使用,例如file_put_contents('./tempname',file_get_contents($imagepath))
。当然,临时名称应该是随机的,并且之后应该将其删除。
关于image - 来自 URL 的 Zend_Pdf_Image::imageWithPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5016962/