1

when i use library intervention/image in laravel 4, with piece of code $image = Image::make($url) i meet error:

file_get_contents(http://ticket.at-newyork.com/image/tenboudai/rockfeller-top.jpg): failed to open stream: HTTP request failed! HTTP/1.1 410 Gone

I have researched many solution on stackoverflow and try it ex : use urlencode but i have another error :

file_get_contents(http%3A%2F%2Fticket.at-newyork.com%2Fimage%2Ftenboudai%2Frockfeller-top.jpg): failed to open stream: No such file or directory

Please don't suggest me use curl, because file_get_contents() is code in library and i can't change. Thank you

phan ngoc
  • 191
  • 3
  • 14
  • Has the server that hosts that image prevented hotlinking? – frankle Jan 07 '16 at 07:53
  • i don't understand, i only know curl can solve problem but i can't use because it is code in library and so i can't change – phan ngoc Jan 07 '16 at 07:59
  • I've just tried cURL and it didn't work, it returns 410 status code too. The owner of the image/site has prevented you from accessing the image remotely like this. Google 'prevent hotlinking'. – frankle Jan 07 '16 at 08:01
  • when i change use cURL, it completely correct , i don't know why in your side it return 410 status code, maybe it's not reason – phan ngoc Jan 07 '16 at 08:09
  • 1
    Look [at this question about cURL and images](http://stackoverflow.com/questions/6476212/save-image-from-url-with-curl-php). Look at @bbullis comment about user-agent detection. If I set the user agent in cURL I get the image, if I don't I get 410 status. – frankle Jan 07 '16 at 08:27
  • @Objective_d completely agree – Chetan Ameta Jan 07 '16 at 09:23

2 Answers2

1

domain ticket.at-newyork.com need user-agent header in request to get content remotely. have a look on below solution it works for me.

$opts = array(
    'http'=>array(
        'method'=>"GET",
        'header'=>"Host: ticket.at-newyork.com\r\n" .
            "User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.80 Safari/537.36"
    )
);

$context = stream_context_create($opts);

$data = file_get_contents('http://ticket.at-newyork.com/image/tenboudai/rockfeller-top.jpg', false, $context);
file_put_contents('test.jpg', $data);

you can also use curl by setting CURLOPT_USERAGENT

Chetan Ameta
  • 7,696
  • 3
  • 29
  • 44
  • 1
    Your method can't resolve my problem because i can't change anything in file in library which contain function file_get_contents() but it help me understand more about it, thank you very much for your support. I appreciate it ! +1 – phan ngoc Jan 07 '16 at 10:08
  • 1
    Chetan Ameta's code is correct.You can't get image file using file_get_contents() without setting those options. header("Content-type: image/jpeg"); print_r($data); to show the image. – Tanvir Jan 07 '16 at 11:16
1

This is my solution that help resolve it. Instead pass url in param, i use cURL in controller to get binary image data and pass it to param, this is my code:

    curl_handle = curl_init();
    curl_setopt($curl_handle, CURLOPT_URL,$url);
    curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
    curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl_handle, CURLOPT_USERAGENT, 'Vivo app');
    $query = curl_exec($curl_handle);
    curl_close($curl_handle);

    try {
        $image = Image::make($query)->save($destinationPath . $img_name);
phan ngoc
  • 191
  • 3
  • 14