Search code examples
phpcharacter-encodingfile-get-contents

print file_get_content gives special characters in url, php


I want to print an image url, but it gives me special characters. I tried every solution in file_get_contents - Special characters in URL - Special case but it didnt solve it.

my code:

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

Solution

  • If you want to show the image try this:

    <?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)) . "'>";
    
    ?>
    

    It gets the image and encodes in base64.

    And later if you need the raw image data, just base64_decode it like:

    $raw_image = base64_decode($encodedImg):
    

    Where $encodedImg is the base64 encoded string. The $raw_imag would contain the raw image data!