【问题标题】:PHP: crop with PNG and transPHP:使用 PNG 和 trans 进行裁剪
【发布时间】:2011-05-03 05:07:49
【问题描述】:

当用户上传并尝试裁剪 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 等创建...?然后以某种方式转换它..

应该如何解决?

【问题讨论】:

    标签: php image upload crop


    【解决方案1】:

    函数imagecreatefromjpeg() 从一个jpeg 图像创建一个通用PHP 图像object。然后,您可以对对象做任何您想做的事情。

    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 值(也许您正在另一个脚本中执行此操作?)。如果没有,here's a good tutorial on handling file uploads。它还涵盖文件扩展名。

    【讨论】:

    • 您好,感谢您的回答。如何检测文件扩展名? $_POST['fname'];是文件名,例如 asddsa.jpg
    • 一个非常简单的方法是查看 strpos($_POST['fname'], '.jpg');返回真。只需将 '.jpg' 更改为 '.png','.gif','.jpeg' 即可测试其他类型。
    【解决方案2】:

    试试这个...它会工作..

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

    【讨论】:

      【解决方案3】:

      图片加载失败,导致 imagecreatefromjpeg() 返回 false。确保您正在尝试打开有效的图像。

      【讨论】:

        【解决方案4】:

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

        【讨论】:

          【解决方案5】:
          <?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);
          
          ?>
          

          这将返回裁剪后的图像。

          【讨论】:

            猜你喜欢
            • 2014-08-03
            • 2014-06-03
            • 2019-01-15
            • 2012-08-19
            • 1970-01-01
            • 1970-01-01
            • 2015-03-02
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多