views:

31

answers:

3

Im having issue when a user uploads and try's to crop a PNG or transparent image( with .gif)

I am getting:

Warning: imagecreatefromjpeg(): gd-jpeg: JPEG library reports unrecoverable error:  in statusUpFunctions.php on line 100

Warning: imagecreatefromjpeg(): 'images/status/photo/1-6.jpg' is not a valid JPEG file in statusUpFunctions.php on line 100

Warning: imagecopyresampled() expects parameter 2 to be resource, boolean given in statusUpFunctions.php on line 114

This may be because of my php crop function:

case 'crop':
 if ($_SERVER['REQUEST_METHOD'] == 'POST')  {

 ini_set('memory_limit', '256M');
  $jpeg_quality = 90;

  $src = "images/status/photo/".$_POST['fname'];
  $img_r = imagecreatefromjpeg($src);

        if($_POST['fixed'] == 0) {
           $targ_w = $_POST['w'];
           $targ_h = $_POST['h'];
        }
        else {
            $targ_h = $_POST['sizeh']; 
   $targ_w = $_POST['sizew']; 
        }

        $dst_r = ImageCreateTrueColor( $targ_w, $targ_h );

  imagecopyresampled($dst_r,$img_r,0,0,$_POST['x'],$_POST['y'],
  $targ_w,$targ_h,$_POST['w'],$_POST['h']);

 $path_thumbs = "images/status/photo";
  $filename = $newfilename;
     $thumb_path = $path_thumbs . '/' . $filename;

  imagejpeg($dst_r,$thumb_path,$jpeg_quality);
}
break;
}

It creates from jpg, cant i make so it creates from gif and png and bmp and so..? and then convert it somehow..

How should this be solved?

A: 

It fails to load the image, causing imagecreatefromjpeg() to return false. Make sure that you're trying to open an valid image.

Emil H
A: 

For gif and png files you need to get the image identifier using imagecreatefromgif and imagecreatefrompng

Divlancer
A: 

The function imagecreatefromjpeg() creates a generic PHP image object from a jpeg image. You can then do whatever you want with the object.

imagecreatefromjpeg() will ONLY create a generic image object FROM a JPEG. If you want to create a generic image object FROM a PNG or GIF, you need to use their respective functions: imagecreatefrompng() and imagecreatefromgif(). One quick way to detect an image's type is by reading its file extension.

Once you have that generic image object, then you can do what you want with it (including using imagecopyresampled()), and then create a jpeg image from it using imagejpeg().

EDIT:

You can use pathinfo() to detect the file's extension:

$filename = pathinfo($_POST['fname']); // Returns an array of file details
$extension = $filename['extension'];

If you are getting the filename from a $_POST value, you need to make sure that you are copying the temporary image file to that filename. I don't see any $_FILES values used in your code (maybe you're doing it in another script?). If not, here's a good tutorial on handling file uploads. It also covers file extensions.

willell
Hi thanks for your answer. How can i detect an file extension? $_POST['fname']; is the filename e.g asddsa.jpg
Johnson