【问题标题】:print file_get_content gives special characters in url, phpprint file_get_content 在 url、php 中给出特殊字符
【发布时间】:2016-03-18 09:54:47
【问题描述】:

我想打印一个图像 url,但它给了我特殊字符。我尝试了file_get_contents - Special characters in URL - Special case 中的所有解决方案,但都没有解决。

我的代码:

$urlgeo = "http://geodata.nationaalgeoregister.nl/top25raster/wms?request=GetMap&service=WMS&version=1.3.0&request=GetMap&layers=top25raster&styles=&bbox=146000,451000,149000,454000&width=600&height=600&srs=EPSG:28992&format=image/png";
print file_get_contents($urlgeo);

【问题讨论】:

  • 可以base64编码,比如... base64_encode(file_get_contents($urlgo));
  • 您不是在打印 URL,而是在打印内容。
  • 这也给出了特殊字符而不是我的图片网址

标签: php character-encoding file-get-contents


【解决方案1】:

您正在从 API 服务请求 PNG 图像,您应该使用适当的标头来显示图像,

header('Content-Type: image/png'); 
$urlgeo = "http://geodata.nationaalgeoregister.nl/top25raster/wms?request=GetMap&service=WMS&version=1.3.0&request=GetMap&layers=top25raster&styles=&bbox=146000,451000,149000,454000&width=600&height=600&srs=EPSG:28992&format=image/png";
print file_get_contents($urlgeo);

【讨论】:

    【解决方案2】:

    如果你想显示图片试试这个:

    <?php
    $urlgeo = "http://geodata.nationaalgeoregister.nl/top25raster/wms?request=GetMap&service=WMS&version=1.3.0&request=GetMap&layers=top25raster&styles=&bbox=146000,451000,149000,454000&width=600&height=600&srs=EPSG:28992&format=image/png";
    echo "<img src='data:image/png;base64," . base64_encode(file_get_contents($urlgeo)) . "'>";
    
    ?>
    

    它获取图像并以base64编码。

    稍后如果您需要原始图像数据,只需 base64_decode 即可:

    $raw_image = base64_decode($encodedImg):
    

    其中$encodedImg 是base64 编码的字符串。 $raw_imag 将包含原始图像数据!

    【讨论】:

    • 注意:您应该注意,此解决方案会使您在服务器内存中存储一​​个潜在的巨大图像数据块(感谢 base 64 编码)
    • 是的,那你就可以使用AJAX异步加载图片了!
    猜你喜欢
    • 2014-06-19
    • 2013-05-14
    • 2012-02-15
    • 2010-09-21
    • 2013-02-12
    • 1970-01-01
    • 1970-01-01
    • 2015-10-21
    • 1970-01-01
    相关资源
    最近更新 更多