PHP:使用 PNG 和 trans 进行裁剪

发布于 2024-09-30 04:54:42 字数 1341 浏览 5 评论 0原文

当用户上传并尝试裁剪 PNG 或透明图像(带有 .gif)时,

我遇到问题:

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

这可能是因为我的 php 裁剪功能:

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;
}

它从 jpg 创建,我不能这样做,所以它从 gif 和 png 创建和 bmp 等等..?然后以某种方式转换它..

这个应该如何解决?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

忆梦 2024-10-07 04:54:42

函数 imagecreatefromjpeg() 从 jpeg 图像创建通用 PHP 图像对象。然后您可以对该对象执行任何您想要的操作。

imagecreatefromjpeg() 只会从 JPEG 创建通用图像对象。如果要从 PNG 或 GIF 创建通用图像对象,则需要使用它们各自的函数: imagecreatefrompng()imagecreatefromgif()。检测图像类型的一种快速方法是读取其文件扩展名。

一旦有了通用图像对象,您就可以用它执行您想要的操作(包括使用 imagecopyresampled() ),然后使用 imagejpeg() 从它创建 jpeg 图像>。

编辑:

您可以使用pathinfo() 检测文件的扩展名:

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

如果您从 $_POST 值获取文件名,则需要确保将临时图像文件复制到该文件名。我没有看到您的代码中使用了任何 $_FILES 值(也许您正在另一个脚本中执行此操作?)。如果没有,这里有一个关于处理文件上传的好教程。它还涵盖文件扩展名。

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.

酷遇一生 2024-10-07 04:54:42

试试这个...它会起作用..

<?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);


?>

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);


?>
默嘫て 2024-10-07 04:54:42

它无法加载图像,导致 imagecreatefromjpeg() 返回 false。确保您尝试打开的图像有效。

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

久夏青 2024-10-07 04:54:42

对于 gif 和 png 文件,您需要使用 imagecreatefromgif 和 imagecreatefrompng 获取图像标识符

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

今天小雨转甜 2024-10-07 04:54:42
<?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);

?>

这将返回裁剪后的图像。

<?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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文