Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

imagecreatefromjpeg is silently terminating scripts

Tags:

php

thumbnails

gd

Like so many before me, I'm writing a PHP script to do some image thumbnailing. The script has earned WOMM (works on my machine) certification, but when I move it to my host (1&1 Basic), there's an issue: images above a certain filesize cannot be processed. I've moved all operations to the filesystem, to be certain it's not some latent POST issue. Here's the relevant code:


function cropAndResizeImage( $imageLocation )
{
  //
  // Just to be certain
  //
  ini_set('display_errors','on');
  error_reporting(E_ALL);
  ini_set('memory_limit','128M'); 
  ini_set('max_execution_time','300');  

  $image_info   = getimagesize($imageLocation);
  $image_width  = $image_info[0];
  $image_height = $image_info[1];
  $image_type   = $image_info[2];

  switch ( $image_type )
  {
    // snip...
    case IMAGETYPE_JPEG:
      $image = imagecreatefromjpeg($imageLocation);
      break;

    default:
      break;
  }

  // snip...
}

Using my mystical powers of println debugging, I've been able to determine that imagecreatefromjpeg is not returning; in fact, the script halts completely when it gets to it. Some facts:

  • This is correlated to filesize. Images under 1MB appear to be fine (spot-checked), but images around 3MB barf. No clue what the precise cutoff is, though.
  • This is not due to server timeouts; wget returns in <1s on 3MB images, significantly longer on "appropriately small" images (indicating no processing of large images).
  • Prefixing the function call with @ to suppress errors has no effect. This matches well with the fact that the script is not throwing an error, it's simply silently terminating upon this function call.

If I had to guess, there may be some GD parameter that I don't know about (or have access to) that limits input file sizes on 1&1's servers — the config variable guess is due to the fact that it barfs immediately, and doesn't appear (heuristically) to do any actual loading of or computations on the image.

Any suggestions? Thanks for the help.

Update (courtesy @Darryl's comments): calls to phpinfo indicate that PHP is updating the max_execution_time and memory_limit variables correctly. This doesn't necessarily mean that these resources are being allocated, simply that they appear to be functioning as expected.

Update 2: following some references from The Google, I tried optimizing the JPEG (reduced in quality from 3MB to 200KB) with no luck, so it's not an image filesize issue. I then tried reducing the number of pixels of the original 3888x2592 image, and the first successful size is 1400x2592 (1401x and 1402x both result in half-parses and errors indicating "malformed JPEG", which doesn't make much sense unless the entire image isn't being loaded). By reducing further to 1300x2592, I can instantiate the 400x300 thumbnail image that I'm actually looking for; at 1400x2592, the imagecreatetruecolor call I'm using to take care of that task fails silently in the same manner as imagecreatefromjpeg.

As to why this is, I'm a little uncertain. 1400 * 2592 == 3.5MB gives nothing particularly meaningful, but I have to imagine this is a limit on the number of pixels GD + PHP will process.

like image 498
kyle Avatar asked Mar 21 '09 05:03

kyle


Video Answer


1 Answers

Please see this note regarding memory usage on the php website.

*"The memory required to load an image using imagecreatefromjpeg() is a function of the image's dimensions and the images's bit depth, multipled by an overhead.

It can calculated from this formula:

Num bytes = Width * Height * Bytes per pixel * Overhead fudge factor"*

like image 81
J.C. Inacio Avatar answered Oct 29 '22 03:10

J.C. Inacio