0

I have a long function that uses imagecreatefromjpeg().

function myFunction() {
  ...
  ...       
  $im = imagecreatetruecolor(600, 400);
  $myImage = imagecreatefromjpeg("http://example.com/file.jpeg");
  imagecopy($im, $myImage , 5, 5, 0, 0, 48, 48);
  ...
  ...
  ...
  ...
  }

I load jpeg file from remote URL. So due to server load sometimes I get:

Warning: Warning (2): imagecreatefromjpeg(http://example.com/file.jpeg): 
 failed to open stream: Connection timed out in 
 [/var/www/vhosts/example2.com/httpdocs/myfile.php, line 1851]

All execution time is spent for this file, so request becomes unsuccessfull for the rest of my function

Although I need to download up to date jpeg file, running remaining code is acceptable for me.

I look for such a solution:
- Try this: create image from jpeg file
- If not successfull after 5 seconds, skip
- Run remaining code.

Edit:
- I get this error occasionally. Most of the requests successfull. So allow_url_fopen isn't a problem.
- This jpeg file changes frequently, like once an hour.

trante
  • 33,518
  • 47
  • 192
  • 272
  • check your php ini settings for allow_url_fopen, maybe you're not allowed to open files from a URL. `Warning Windows versions of PHP prior to PHP 4.3.0 do not support access of remote files via this function, even if allow_url_fopen is enabled.` http://php.net/manual/en/function.imagecreatefromjpeg.php – Dave Goten Jan 22 '15 at 19:20
  • if that file is on your own server, then is literally NO point in doing an http request to yourself. PHP doesn't need to do http to access the filesystem. all you need is `imagecreatefromjpeg('/path/on/filesystem/file.jpg')` – Marc B Jan 22 '15 at 19:20
  • Before you create a jpg from remote file, download it to local first. – harrrrrrry Jan 22 '15 at 19:24
  • Use an HTTP client library to download the file, that will allow you to set a timeout for the download, once the timeout is reached an exception will be thrown and you'll be able to act on that. –  Jan 22 '15 at 19:36

1 Answers1

1

In a situation where a piece of code may not work properly, whether it is due to an error or an exception, you can use try/catch statements to handle the problem. That way, if your code breaks or does not work, for whatever reason, you can program a way to handle a given error or exception.

However, try/catch only works on Exceptions, and imagecreatefromjpeg() throws a warning, which is a type of error. You can solve this by using set_error_handler() to turn Errors into Exceptions! (see this StackOverflow thread for more info)

At the top of your php file, insert

set_error_handler(function($errno, $errstr, $errfile, $errline, array, $errcontext) {
    // error was suppressed with the @-operator
    if (0 === error_reporting()) {
        return false;
    }

    throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
});

In your function, you can then do:

function myFunction() {
  ...
  ...
  try{ 
    $im = imagecreatetruecolor(600, 400);
    $myImage = imagecreatefromjpeg("http://example.com/file.jpeg");
    imagecopy($im, $myImage , 5, 5, 0, 0, 48, 48);
  } catch (ErrorException $ex){
    // Do Nothing
    // Or Handle the error somehow
  }
  // Code continues to run 
  ...
  ...
  ...
  ...
}
Community
  • 1
  • 1
Jarwain
  • 65
  • 1
  • 4