0

So I have a server running windows on it, which is running a PHP environment to run a blog website.

Now the following code runs fine on my localhost using xampp, but does not work on the windows server.

$image = get_the_post_thumbnail_url($postId, array(150,150));
$ext = pathinfo($image, PATHINFO_EXTENSION);
$base64 = 'data:image/' . $ext . ';base64,' . base64_encode(file_get_contents($image));

The code gets the post thumbnail from the loop and creates a base64 image, which I use in a blur up image technique(Like what happens to the first two images on this site)

When I echo out the $ext and $image variables I get data, but when I echo the file_get_contents($image) I get nothing. I can confirm the file does exist.

echo $ext . ':' . $image;
echo '<br />' . file_get_contents($image);

I came across the suggestion to allow_url_fopen but when I check by echoing out phpinfo() it is enabled

echo  phpinfo();

The php version is 5.6 so that wouldn't be the problem either.

Also when I turn wordpress debug on for the windows server I get this error: Warning: file_get_contents(): php_network_getaddresses: getaddrinfo failed: No such host is known.

Anyone know of other potential causes for this preblom. Cheers

enter image description here

Web Dev Guy
  • 1,693
  • 3
  • 18
  • 42
  • 1
    Is there a valid url in the `$image` variable? Is there actually a domain that exists at that address? That error usually means the URL doesn't exist or can't be found. – Jonathan Kuhn Aug 03 '17 at 22:49
  • @JonathanKuhn Thanks for the help. The image does exist. when I copy the output from the image variable and paste into my browser it works – Web Dev Guy Aug 03 '17 at 22:53
  • 1
    `getaddrinfo` is a system function unrelated to php except for the fact that php uses it like many other programs do. Can you ping the domain from the server? – Jonathan Kuhn Aug 03 '17 at 22:56
  • 1
    A lot of stack questions come up if you google that error message. It looks like most of them point to DNS issues. Here's one that looks pretty popular: https://stackoverflow.com/questions/20064372/file-get-contents-php-network-getaddresses-getaddrinfo-failed-name-or-servi – Don't Panic Aug 03 '17 at 22:57
  • Nope not DNS issue, see my answer. I looked at those answers but to no avail. Thanks @JonathanKuhn & Don'tPanic for the help :) – Web Dev Guy Aug 03 '17 at 23:03

1 Answers1

0

Omg so I figured it out.

The windows server is a test site, with url https://test.domain.com.au So is a subdomain.

So when I updated my code to replace the test with www in the url, it works.

$base64 = 'data:image/' . $ext . ';base64,' . base64_encode(file_get_contents(str_replace('test', 'www', $image)));

So even though the image url https://test.domain.com.au/blog/wp-content/uploads/2017/03/my-image-150x150.jpg is a valid url and was displaying an image when pasted into the browser address bar, the file_get_contents() function didn't like it.

Not sure why this is as the test site is a live site, so live as in on the internet.

I hope this helps someone else :)

Web Dev Guy
  • 1,693
  • 3
  • 18
  • 42