Nik Sumeiko
Nik Sumeiko

Reputation: 8741

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

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.

Upvotes: 3

Views: 1974

Answers (3)

MiniGod
MiniGod

Reputation: 3802

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);

Upvotes: 6

exaptis
exaptis

Reputation: 249

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;
?>

Upvotes: 1

ARMBouhali
ARMBouhali

Reputation: 298

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.

Upvotes: 0

Related Questions