我正在尝试代理在 HTTPS 网站中提供 HTTP 图片我听说我可以在我的一个端点中执行此文件的代理服务并渲染它在我的网站而不是显示实际的文件网址。以下是我的所作所为,但我收到
以下的错误下面是我创建自定义外观以显示图像的HTML代码
country <- read.table(text = " Name Code
algeria dz
canada ca
japan jp
kenya ke",
stringsAsFactors = FALSE, header = TRUE)
data <- data.frame(Institution = c("edmonton general hospital",
"ontario, canada",
"miyazaki, japan",
"department of head"))
以下是我在Laravel PHP中的代码
<div><img src="{{ MyFacade.proxyImage("http://unsecure-image.com/image.jpeg") }}" /></div>
有关如何实现这一目标的任何想法?我只是想显示图像
答案 0 :(得分:0)
好,这是我的问题的最终答案,并感谢@Lawrence的想法。希望这可以帮助其他有相同问题的人
public function proxyImage(Request $request, $key)
{
try {
$referer = $request->headers->get('referer');
// Let's remove the param query since we need
$sanitized_referer = substr($referer,0,strpos($referer, '?'));
if (!$referer) {
return abort('404');
}
if ($sanitized_referer != null) {
$referer = $sanitized_referer;
}
$image = $request->get('image');
if (filter_var($image, FILTER_VALIDATE_URL) === false) {
return abort('404');
}
$key = urldecode($key);
$decrypted_key = decrypt($key);
if (!isset($key) || $decrypted_key != $image) {
Log::info("Decripted key " . $decrypted_key . " referer " . $referer);
if ($decrypted_key != $referer) {
return abort('404');
}
}
if (!MyFacade::image_exist($image)) {
$image = 'images/no-images.jpg';
}
$file = file_get_contents($image);
$type = pathinfo($image, PATHINFO_EXTENSION);
header('Content-Type:'.$type);
echo $file;
} catch (Exception $e) {
Log::info($e->getMessage());
return abort(404);
}
}
在我下面的代码中
class MyFacade {
/**
* Generate an encrypted url
*
* @param string $url - The url to be encrypted
* @return string - The encrypted url
*/
public function generateToken($url)
{
return urlencode(encrypt($url));
}
/**
* Check the image header if the image url contains an actual image or the file really does exist
* this is special handling for a bug with file_exist that returns false even if file does exist
*
* @param string $url - The image url to check
* @return boolean - true if the file exist, false otherwise
*/
public function image_exist($url)
{
$file_headers = @get_headers($url);
if($file_headers[0] == 'HTTP/1.1 404 Not Found') {
return false;
}
return true;
}
/**
* Generate a url to return to the client. If the protocol is HTTP it will return the original image url
* but if the url is not http it will proxy serve the url and return an encrypted value
*
* @param string $image_url - The url to be check and encrypted if not https
* @return string - The newly generated url
*/
public function generateUrl($image_url)
{
if (parse_url($image_url, PHP_URL_SCHEME) == 'https') {
return $image_url;
}
return url('proxy-image/' . $this->generateToken($image_url) . '?image=' . $image_url);
}
}
现在在我的刀片服务器模板上,我仅调用 generateUrl 。如果图像不是https格式,则它将返回带有加密令牌的URL和$ image参数,以检查令牌和参数中的图像是否为相同URL。在刀片中,我正在做类似的事情
<img src="{{ Application::generateUrl('http://unsecuresite.com') }}"