How to build a PHP/Node proxy to render external http images on https website?

纵饮孤独 提交于 2020-01-02 07:26:34

问题


I have a website running on https. I have to load images from external server (external domain) which doesn't have https, but single http protocol.

Is there's any way to handle proxy for http images via PHP or Node? So I can render images like this:

<img src="https://domain.com/proxy?url=http://externaldomain.com/image.jpg" />

The idea is to avoid saving images on local, but only display them.

When I try to render http served images inside https domain, I get this console message:

The page at https://domain.com/ displayed insecure content from http://externaldomain.com/image.jpg.

As well, my SSL (/https) lock icon inside address bar becomes grey.


回答1:


You can use node, which will just pipe the image instead of loading the whole image into memory before sending to the client (like file_get_contents would do in php). Using request in this example for simplicity in streaming:

var https = require('https');
var url = require('url');
var request = require('request');

var server = https.createServer(function (req, res) {
  var queryData = url.parse(req.url, true).query;

  if (queryData.url) {
    var x = request(queryData.url);
    req.pipe(x).pipe(res);
  } else {
    res.writeHead(400, {"Content-Type": "text/plain"});
    res.end("No url");
  }
});

// Listen on port 443
server.listen(443);



回答2:


something like this should work:

<?php
    header('Content-type: image/jpeg;');
    $p = "http://i.imgur.com/jB3xD75.jpg";
    $a = file_get_contents($p);
    echo $a;
?>



回答3:


You should first download them using php cURL library. If you don't want to store images on hard drive. You can add them as data URIs in the src attribute.



来源:https://stackoverflow.com/questions/17085460/how-to-build-a-php-node-proxy-to-render-external-http-images-on-https-website

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!