3

I am using imagecreatefromjpeg on a shared host, so if the image is to big then imagecreatefromjpeg will give me an "out of memmory error" and terminate the script.

Is there anyway to catch the out of memmory error, and simply let imagecreatefromjpeg fail(Return false) instead of terminating the script?

Martin Tilsted
  • 699
  • 5
  • 10

5 Answers5

3

Try working out the size of the image first: http://uk3.php.net/function.getimagesize I believe imagecreatefromjpeg will fall over if the image is bigger than 3000px in any dimension.

Lizard
  • 43,732
  • 39
  • 106
  • 167
2

Prevent imagecreatefromjpeg from stopping script, you should probably check for set memory limit. You can determine memory required as follows: memory required=image pixels * 3 bytes

e.g. if you are having an image with resolution 1000 x 2000 then memory required will be

memory required= ((1000 * 2000) * 3)=600000 bytes =5.72205MB

if your set memory limit is 5242880 bytes (5MB), you can perform the following check before making call to imagecreatefromjpeg()

if($memory_required < 5242880)
{
    imagecreatefromjpeg();
    // continue with your image related operations
}
else
{
  //handle error message, perhaps show such message as "provide image is too big"
}

you can use PHP available functions in PHP to determine the image resolution (height, width)

Rahul
  • 2,189
  • 7
  • 40
  • 60
0

see this thread:

A fail-safe way to prevent GD image library from running out of memory? (PHP)

simple answer is its not easily possible with any degree of certainty

Community
  • 1
  • 1
robjmills
  • 18,438
  • 15
  • 77
  • 121
0

does adding @ before the function do any help?

example:

$x=@imagecreatefromjpeg .......

see also how to suppress errors in php: http://us3.php.net/manual/en/language.operators.errorcontrol.php

dusoft
  • 11,289
  • 5
  • 38
  • 44
0

Simply enclose your function inside if statements, instructing it to redirect when the function fails.

i.e. if (!imagecreatefromjpeg){//redirect commands here};

George Violaris
  • 315
  • 1
  • 10