1

I’m trying to do 2 things:

1) Get thumbnails from Bing and display it. The URL looks like this http://tse4.mm.bing.net/th?id=OIP.M764a682297c84c4e519c91b4d39a5731o0

I tried doing the following but it didn’t work.

<?php
    $file = 'http://tse4.mm.bing.net/th?id=OIP.M764a682297c84c4e519c91b4d39a5731o0';

    header('Content-Type: image/jpg');
    header('Content-Length: ' . filesize($file));
    echo file_get_contents($file);
?>
  1. the second thing I’m trying to do is to download thumbnails from bing (the same as the previous URL)
    <?php

$content = file_get_contents('http://tse4.mm.bing.net/th?id=OIP.M27a2faeba4dbe75a45036d09675745dfH0&h=200&w=210');

echo file_put_contents('/images/image.jpg', $content);
 ?>

Non of the above worked, it might be cause the URL doesn't end with a JPEG extension but I'm not sure. any ideas how can i fix this?

Thanks.

user2334436
  • 949
  • 5
  • 13
  • 34

1 Answers1

2

In your first try, you write filesize($file), but $file is a string, not a file. So your header is Content-Length: False. In addition,if you have error reporting enabled, your script echoes something like “Warning: filesize(): stat failed for ...” that corrupt JPEG output.

In your second try, you save the content to a local file, then you echo the result of file_get_contents, that is an integer, as you can see in the documentation.

First retrieve contents, then output it:

$file = 'http://tse4.mm.bing.net/th?id=OIP.M764a682297c84c4e519c91b4d39a5731o0';

$data = file_get_contents( $file );

header('Content-Type: image/jpg');
header('Content-Length: ' . strlen($data));
echo $data;
exit;

Tested as working.

fusion3k
  • 11,568
  • 4
  • 25
  • 47
  • I just tested it, it didn't work. the out put was something like this:����JFIF``��C $ &%# #"(-90(*6+"#2D26;=@@@&0FKE>J9?@=��C =)#)==================================================��,�"�� ���}!1AQa"q2���#B��R��$3br� %&'()* – user2334436 Apr 27 '16 at 21:47
  • The script works. Double ckeck-it. Also note that NO OTHER output is allowed. Even a single space before opening ` – fusion3k Apr 27 '16 at 21:55