0

I have a file upload class which returns an image resize object if the file upload is believed to be an image. The construct of the image resize class starts with this:

$this->resource = imagecreatefromstring($this->getData());

if (!is_resource($this->resource)) {
  return $this->error(IMAGE_ERR_SOURCE_CREATION_ERROR);
}

$this->getData() returns the string returned from file_get_contents from tmp_name from $_FILES[..] array.

The promlem is as follows. It works perfectly fine on my development server 5.3.0 but imagecreatefromstring() fails on the production server, 5.3.5.

Why? I cannot get my head around it, I have compared strings of images from development and product using base64 and they are identical. What do I need to look at thats changed from .0 to .5?

Edit: Yes, GD2 is installed and working correctly. Incidentally, WordPress (3.1) is also installed on this server and all image functionality is working correctly, which is also why I am so puzzled.

Edit 2

After debugging with display_errors (I know should have done this first), the error returned is:

Warning: imagecreatefromstring(): No JPEG support in this PHP build

Does this mean I have to reinstall GD with JPEG support or can it be enabled once installed, IE now?

hakre
  • 193,403
  • 52
  • 435
  • 836
Matt Humphrey
  • 1,554
  • 1
  • 12
  • 30
  • 1
    Does the PHP installation actually have support for the image format you're trying to use? Check phpinfo for the supported codecs. – Steven Don Mar 30 '11 at 10:00
  • Can you define "fails" more precisely for us? What image format is the data? – Matt Gibson Mar 30 '11 at 10:02
  • run this code $handle = fopen('img.jpg', 'r'); $contents = fread($handle, filesize('img.jpg')); $image = imagecreatefromstring($contents); header("Content-type: image/jpeg"); imagejpeg($image); fclose($handle); – S L Mar 30 '11 at 10:06
  • and Test if this function is working or not. – S L Mar 30 '11 at 10:07
  • I wish I could.. It just returns false, no errors at all. The function just returns false.. – Matt Humphrey Mar 30 '11 at 10:12

1 Answers1

2
  1. Ensure you do have GD on the production server (using <?php phpinfo(); ?> or $> php -m) and that is configured correctly (using $> php --re GD and $> php --ri GD).
  2. Enable display errors on production server (temporally)
  3. If [2] fails, use error_reporting(E_ALL)
  4. Run script testcase through SSH on production server and see if it fails as well (it should)
  5. Using [4], run it through strace (if on linux) or TraceNT (on windows) and see where the error started out from.
Christian
  • 27,509
  • 17
  • 111
  • 155
  • Ahhhhh. I may have used error_reporting(E_ALL); but not display_errors. Massive fail on my part... See question for update. – Matt Humphrey Mar 30 '11 at 10:16
  • If you can't get it to write anything (check logs as well) you'll have to try my 4/5 methods. Strace is quite powerful, though I'm afraid you need someone quite knowledgeable in linux (me excluded). – Christian Mar 30 '11 at 10:17
  • 1
    Well, the error is pretty self-explanatory. Mission accomplished. :). With regards to your last question, I'm not sure, it depends on the build. In my case, I could just replace the GD library, but I'm not sure about yours. I suppose you could back up your GD copy and throw in a new one. – Christian Mar 30 '11 at 10:21
  • Not quite, as how come WordPress is able to upload JPEGs fine? – Matt Humphrey Mar 30 '11 at 10:22
  • 1
    Well, you can upload exe files if you wanted to, PHP won't complain ;). Did you tried resizing them (from WP)? WP is clever, it might have some fail overs - such as resizing by HTML/CSS if it failed via PHP. – Christian Mar 30 '11 at 10:25
  • 1
    @Matt WordPress has a lot of fairly smart code for falling back to dumber behaviour if the PHP version it's running on lacks support for certain image functions (see WordPress's `wp-includes/media.php` code.) It's perfectly possible to upload images, just as files, without GD support for JPEG; WordPress is probably just coping as best it can after that point (it might, say, be displaying full-size images with the display sizes set to thumbnail size on your page, instead of rescaling to generate real thumbnails, for example.) – Matt Gibson Mar 30 '11 at 10:31
  • You are indeed correct. WordPress hasnt been resizing JPEGs, only PNGs (and probably GIFs but we dont use them ;)). Thanks alot, we are looking into enabled JPEG support now. +1 – Matt Humphrey Mar 30 '11 at 10:34
  • Welcome, glad to be of help. :) Good luck. – Christian Mar 30 '11 at 10:41