3

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);
Community
  • 1
  • 1
user30058
  • 167
  • 1
  • 8

2 Answers2

2

You are requesting an PNG Image from the API service, you should use the appropriate headers to display the image,

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);
Vishnu Nair
  • 2,395
  • 14
  • 16
0

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!

Ikari
  • 3,176
  • 3
  • 29
  • 34
  • 1
    CAUTION: You should note that this solution makes you store in your server memory a potentially huge block of image data (thanks to base 64 encoding) – Dfaure Mar 18 '16 at 10:16
  • Yeah, then you can asynchronously load the image using AJAX! – Ikari Mar 18 '16 at 10:19