4

I have a PHP script that fetches an image from a remote server so that I can manipulate it using HTML5 canvas API.

<?php
if ((isset($_GET['url']))) {
    $url = $_GET['url'];
    $file_format = pathinfo($url, PATHINFO_EXTENSION);
    try
    {   
        header("Content-Type: image/$file_format");
        header("Content-disposition: filename=image.$file_format");
        $img = file_get_contents($url);
        echo $img;
    }

    catch(Exception $e)
    {
        echo $e->getMessage();
    }
}

else die('Unknown request');
?>

A typical request would look like this:

fetch_image.php?url=http://example.com/images/image.png

Everything works fine on my local server but the production server gives me this error:

NetworkError: 500 Internal Server Error.

The error log registers this message:

PHP Warning: Cannot modify header information - headers already sent.

I have tried some of the suggestions but its not working:

allow_url_fopen = 1
random
  • 9,774
  • 10
  • 66
  • 83
Q_Mlilo
  • 1,729
  • 4
  • 23
  • 26
  • Please paste the complete script (including ` – Dogbert Jul 18 '11 at 07:32
  • sounds like a wrong path thing – Elzo Valugi Jul 18 '11 at 07:34
  • What lines does the error point to? Please also post the exact error message. – Dogbert Jul 18 '11 at 07:40
  • If you are just presenting an image on the remote server with no extra processing, why not just redirect to that image? – meouw Jul 18 '11 at 07:50
  • @Meouw - HTML5 canvas api does not allow you to manipulate the pixels of images from another domain. – Q_Mlilo Jul 18 '11 at 08:17
  • Does this answer your question? [file\_get\_contents(): SSL operation failed with code 1, Failed to enable crypto](https://stackoverflow.com/questions/26148701/file-get-contents-ssl-operation-failed-with-code-1-failed-to-enable-crypto) – Jay Patel Oct 19 '21 at 17:39

6 Answers6

11

Check that the server allows you to open remote URLs with the file functions (the php.ini "allow_url_fopen" setting must be "true").

Steffen
  • 2,235
  • 13
  • 19
1

Try

ob_start()

in the beginning and

ob_end_flush()

at the end of the script. Also make sure that the script contains no characters before the <?php.

marc
  • 6,103
  • 1
  • 28
  • 33
1

You should make sure that your hosting provider has not disabled remote URL fetching for security reasons. The setting is allow_url_fopen and you can inspect current configuration with phpinfo(). In such case, file_get_contents() should return FALSE so you must test $img against false with the === operator.

Álvaro González
  • 142,137
  • 41
  • 261
  • 360
1

Try this way

<?php
if ((isset($_GET['url']))) {
    $url = $_GET['url'];
    $file_format = pathinfo($url, PATHINFO_EXTENSION);
    try
    {   
        ob_clean();
        ob_start();

        header("Content-Type: image/$file_format");
        header("Content-disposition: filename=image.$file_format");
        $img = file_get_contents(urlencode($url));
       // as per manual "If you're opening a URI with special characters, such as spaces, you need to encode the URI with urlencode(). " 
        echo $img;
        echo ob_get_clean();
        exit();
    }

    catch(Exception $e)
    {
        echo $e->getMessage();
    }
}

else die('Unknown request');
?>

one more solution from manual

Sometimes you might get an error opening an http URL. even though you have set "allow_url_fopen = On" in php.ini

For me the the solution was to also set "user_agent" to something.

Bhavik Shah
  • 2,300
  • 1
  • 17
  • 32
yogs
  • 788
  • 2
  • 11
  • 29
0

The headers gets sent when you start outputting content. So somewhere before the code you've supplied above, content gets echoed out (from PHP or in plain HTML or javascript). You need to hunt down where that happens.

Stefan H Singer
  • 5,469
  • 2
  • 25
  • 26
0
  1. you should check for encoding of the file as utf8 will break the headers
  2. check if your file does not print any other data before it runs the script.
Mihai Iorga
  • 39,330
  • 16
  • 106
  • 107