Use PHP to proxy images

2019-08-07 18:32发布

问题:

I have a web application that runs through HTTPS, but we are loading external images on servers that don't support SSL. For this reason we need to proxy all external images through a PHP script so they can be loaded over SSL.

I tried following but i get "the image cannot be displayed because it contains errors.".

$remoteImage = "http://www.blog.qarea.com/wp-content/uploads/2012/01/code.jpg";
$imginfo = getimagesize($remoteImage);
header("Content-type: ".$imginfo['mime']);
readfile($remoteImage);

Any thoughts on where I'm going wrong?

MORE INFO: The content length of the request matches the size of the original image. UPDATE: I just tried the script in a stand alone file and it worked fine. Looks like it's an issue with Zend Framework. Now I just need to debug that, any input would be appreciated.

回答1:

Do you really need PHP to proxy requests? The easiest solution is to use the proxy module of your webserver, and this also comes with the top performance.

See the documentation for Apache, Nginx.



回答2:

Is there a space or linebreak before the <?php tag? If so that would cause the image to be invalid.

Additionally, if you have a ?> closing the code (which is pointless), make sure there are no spaces/linebreaks after that either.



标签: php ssl