开发者

PHP: crop with PNG and trans

开发者 https://www.devze.com 2023-01-23 09:46 出处:网络
Im having issue when a user uploads and try\'s to crop a PNG or transparent image( with .gif) I am getting:

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.p开发者_运维技巧hp 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?


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.


Try This... It will work..

<?php

$image = imagecreatefrompng('photo.png');


$thumb_width = 280;
$thumb_height = 200;

$width = imagesx($image);
$height = imagesy($image);

$original_aspect = $width / $height;
$thumb_aspect = $thumb_width / $thumb_height;

if ( $original_aspect >= $thumb_aspect ){
   // If image is wider than thumbnail (in aspect ratio sense)
   $new_height = $thumb_height;
   $new_width = $width / ($height / $thumb_height);
}else{
   // If the thumbnail is wider than the image
   $new_width = $thumb_width;
   $new_height = $height / ($width / $thumb_width);
}


$crop = imagecreatetruecolor($thumb_width,$thumb_height);

imagealphablending($crop, false);
$white = imagecolorallocatealpha($crop, 0, 0, 0, 127);    //FOR WHITE BACKGROUND
imagefilledrectangle($crop,0,0,$thumb_width,$thumb_height,$white);
imagesavealpha($crop, true);
$userImage = imagecopyresampled($crop, $image, 
                    0 - ($new_width - $thumb_width) / 2, // Center the image horizontally
                    0 - ($new_height - $thumb_height) / 2, // Center the image vertically
                    0, 0, $new_width, $new_height,
                    $width, $height);

header('Content-type: image/png');

imagepng($crop);


?>


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


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


<?php

    $image = imagecreatefrompng('myPhoto.png');

    $crop = imagecreatetruecolor(50,50);

    $userImage = imagecopyresampled($crop, $image, 0, 0, 0, 0, 50, 50, 8, 8);

    header('Content-type: image/png');

    imagepng($crop);

?>

This will return cropped image.

0

精彩评论

暂无评论...
验证码 换一张
取 消