Dan Ramos
Dan Ramos

Reputation: 1102

Use PHP to proxy images

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.

Upvotes: 1

Views: 6293

Answers (2)

Sven
Sven

Reputation: 70933

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.

Upvotes: 1

Zoey Mertes
Zoey Mertes

Reputation: 3144

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.

Upvotes: 1

Related Questions