S L
S L

Reputation: 14328

php image resize error

I am trying to resize an uploaded image. I am getting error

Warning: imagecreatefromjpeg() [function.imagecreatefromjpeg]: gd-jpeg, libjpeg: recoverable error: Premature end of JPEG file in /home/rumdood/lib/photograph.php on line 309

Warning: imagecreatefromjpeg() [function.imagecreatefromjpeg]: '/home/rumdood/public_html/uploads/13018946005603.jpg' is not a valid JPEG file in /home/rumdood/lib/photograph.php on line 309

Warning: imagecopyresampled(): supplied argument is not a valid Image resource in /home/rumdood/lib/photograph.php on line 410

Warning: Cannot modify header information - headers already sent by (output started at /home/rumdood/lib/photograph.php:309) in /home/rumdood/application.php on line 22

And the image is not being resized. The last error is due to header function.

The line 309 is like this

$this->image['render'] = imagecreatefromjpeg( $this->s_image );

Line 410 is like this

imagecopyresampled( $this->image['composite'], $this->image['render'],
                     0, 0, 0, 0, $new_width, $new_height,
                     $this->image['width'], $this->image['height'] );

And my php version is PHP Version 5.2.6

My GD from phpinfo

GD Support          enabled
GD Version          bundled (2.0.34 compatible)
FreeType Support    enabled
FreeType Linkage    with freetype
FreeType Version    2.1.9
GIF Read Support    enabled
GIF Create Support  enabled
JPG Support         enabled
PNG Support         enabled
WBMP Support        enabled
XPM Support         enabled
XBM Support         enabled

Upvotes: 1

Views: 3238

Answers (3)

xkeshav
xkeshav

Reputation: 54060

As @charles suggested.. both error are self explanitory

check for valid image from

 if($_FILES["userPicture"]["error"] == 0) {
 // File was uploaded ok, so it's ok to proceed with the filetype check.
 $uploaded_type = exif_imagetype($_FILES["userPicture"]["tmp_name"]);
 // What we have now is a number representing our file type.

 switch($uploaded_type) {
    case "1":
        $uploaded_type = "gif";
    break;
    case "2":
        $uploaded_type = "jpg";
    break;
    case "3":
        $uploaded_type = "png";
    break;
}

}


For imagecreatefromjpeg() : gd-jpeg, libjpeg: recoverable error: Premature end of JPEG

it was a problem with php 5 and gd2. Heres how to fix it

  • php 4 : No action neccesarry it should work fine
  • php 5.0 – 5.1.2 = Upgrade to the latest php 5
  • php 5.1.3 – current = Declare this variable in your file before calling imagecreatefromjpeg()
  • ini_set(‘gd.jpeg_ignore_warning’, 1);

for Cannot modify header information write ob_start(); at the top of page

Reference

Upvotes: 4

Robik
Robik

Reputation: 6127

Looks like image you are trying to load is not true JPG (probably someone have just renamed or something). Try to resave it with some image manipulation program (like GIMP). Or if you have uploaded it to the server, probably there was some error in uploading. Also if file weight more than one file size limit on server it could be brutally cutted.

Warning: Cannot modify header information - headers already sent by (output started at /home/rumdood/lib/photograph.php:309) in /home/rumdood/application.php on line 22

You have to send header at beggining of the code. there can't be even a space before <?php tag.

Upvotes: 2

Charles
Charles

Reputation: 51421

The errors,

libjpeg: recoverable error: Premature end of JPEG file

and

...is not a valid JPEG file

are pretty self-explanatory. The image you are trying to work with is not being recognized by the underlying JPEG parser as valid. It is very likely that the file is corrupt or truncated.

This is a problem with the image itself, not your code. Your code looks fine.

Upvotes: 3

Related Questions