2012-07-31 10 views
1

我试图读取图像文件,作为base64编码的字符串,并在连接使用HTTPS/SSL时输出数据字符串,否则将URL放在IMG src中属性如果它只在HTTP上。这是我目前的代码,但它不起作用。通过HTTPS服务图像的数据字符串,而在HTTP上的URL

<?php 
function base64_encode_image($filename, $filetype) { 
    if (($_SERVER["HTTPS"] == "on") && $filename) { 
     $file = "/home/content/61/9295861/html/resource/image$filename"; 
     $imgbinary = fread(fopen($file, "r"), filesize($file)); 
     return "data:image/$filetype;base64," . base64_encode($imgbinary); 
    } else { 
     return $filename; 
    } 
} 
?> 
<img src="<?php echo base64_encode_image('/resource/image/logo-96x72.png', 'png'); ?>" width="96" height="72" /> 

它输出:

<img src="<br /> 
<b>Warning</b>: fopen(/home/content/61/9295861/html/resource/image/resource/image/logo-96x72.png) [<a href='function.fopen'>function.fopen</a>]: failed to open stream: No such file or directory in <b>/home/content/61/9295861/html/theme/latest/index.php</b> on line <b>5</b><br /> 
<br /> 
<b>Warning</b>: filesize() [<a href='function.filesize'>function.filesize</a>]: stat failed for /home/content/61/9295861/html/resource/image/resource/image/logo-96x72.png in <b>/home/content/61/9295861/html/theme/latest/index.php</b> on line <b>5</b><br /> 
<br /> 
<b>Warning</b>: fread() expects parameter 1 to be resource, boolean given in <b>/home/content/61/9295861/html/theme/latest/index.php</b> on line <b>5</b><br /> 
data:image/png;base64," width="96" height="72" /> 
+0

看到这个职位它可以帮助你。 http://stackoverflow.com/questions/3011222/dealing-with-http-content-in-https-pages – 2012-07-31 05:33:32

+0

你这样做的任何理由?将图像嵌入base64完全消除了缓存图像的机会 - 您不会在带宽或ssl/tcp开销上保存任何内容 - 您只需大量增加帐单。 – 2012-07-31 05:33:33

+3

请描述'不工作'。 – xdazz 2012-07-31 05:35:13

回答

1

您已经添加了/图像/资源目录两次到您的网址这是导致找不到文件错误

$file = "/home/content/61/9295861/html/resource/image$filename"; 
$filename = "/resource/image/logo-96x72.png" 

所以你的文件URL是/home/content/61/9295861/html/resource/image/resource/image/logo-96x72.png

0

变化

<img src="<?php echo base64_encode_image('/resource/image/logo-96x72.png', 'png'); ?>" width="96" height="72" /> 

<img src="<?php echo base64_encode_image('logo-96x72.png', 'png'); ?>" width="96" height="72" /> 
相关问题