Zend_Pdf_Image::imageWithPath from URL

2019-07-17 20:56发布

问题:

I'm using a script that generates a PNG image based on a URL parameter image_id. For example: http://www.example.com/image.php?image_id=G554jGTT

Now I want to place that image in a pdf like so:

$imagepath = "www.example.com/image.php?id=" . $id;
$image = Zend_Pdf_Image::imageWithPath($imagepath);

When I use a local file all goes well, but when I use an URL I get the following error:

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

Does anybody know how to do this?

Thanks!

回答1:

You cannot do this using Zend_Pdf_Image::imageWithPath. The reason is that the ImageFacotry expects a file. And it uses is_file method to check if file exists. However, is_file function does not necessarily work with remote files.

One way to overcome this problem would be to download the image and save it under temporary name, and this temporary file then put into Zend_Pdf_Image::imageWithPath. To download and save the file, you could use, e.g. file_put_contents('./tempname',file_get_contents($imagepath)). Off course, the tempname should be random and it should be removed afterwards.