Greetings,

I was wondering if anyone here might have an idea as to why I am having problems with the imagecreatefromjpeg function involving it timing out. I have successfully used this function as part of a website that I have developed on several different servers, but I have recently moved my site to a new server and have found that imagecreatefromjpeg keeps timing out with 'failed to open stream: Operation timed out in ...'. The server php version is > 5, gd is enabled, and the permissions for the folders I want to upload to are all OK. This same code has worked perfectly fine on several different servers, only to fail now. Does anyone have any idea as to what could be causing the problem?

Many thanks,

  • LordTherapy.

    I would have to ask:

    1. How big are the jpegs?

    2. Are you loading them from a URL?

    3. Do you have sufficient memory configured in php.ini for the operation? (relates to question 1) Try seeing what memory_limit is set to when calling phpinfo().

    4. Have you tried bumping up the script execution timeout?

      Hi bretticus,

      Many thanks for the reply. In answer to your questions:

      1) Jpegs are only small, typically less than 100 KB.
      2) They are being loaded via URL.
      3) I don't actually know what memory_limit is set to, as I can't see it listed when I run phpinfo() for the server.
      4) Script execution time is currently set to 30 seconds, which has been ample time for other servers that I have run this on successfully.

      Thanks again,

      • LordTherapy.

        Based on the fact that they are loaded via URL, I'd assume one of two things:

        1. the url you are trying to reach is not availible or the request times out.

        2. Yur new host has allow_url_fopen disabled.

        Try actually browsing to the URL for the image. Is it responsive in your browser? If you have shell access for your account try using wget or lynx from the command line.

          Hi bretticus,

          phpinfo() reports that allow_url_fopen is On. I have also tried the function that calls imagecreatefromjpeg on several different image URLs, all of which can be browsed to, without success.

          Many thanks,

          • LordTherapy.

            Hmmmm...you may have to talk to your hosting provider then. Perhaps they are preventing your web server from initiating http requests. Or perhaps, you need to use a http proxy within that network. Perhaps try using fopen first, just to see if that works before getting your hosting provider involved:

            <?php
            // For PHP 5 and up
            $handle = fopen("http://www.example.com/", "rb");
            $contents = stream_get_contents($handle);
            fclose($handle);
            ?>
            <?php
            $handle = fopen("http://www.example.com/", "rb");
            $contents = '';
            while (!feof($handle)) {
              $contents .= fread($handle, 8192);
            }
            fclose($handle);
            ?>

            By the way, do you have shell access? And if so, did you try wget or lynx (or even curl?)

              Try getting the image using an alternate means (for example, [man]file_get_contents/man) and then using the data with [man]imagecreatefromstring/man instead.

              <?php
              $data = file_get_contents('http://example.com/image.jpg');
              $image = imagecreatefromstring($data);
              header('Content-Type: image/jpeg');
              imagejpeg($image);
              imagedestroy($image);
              ?>
              
                Write a Reply...