首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >PHP:使用PNG和trans进行裁剪

PHP:使用PNG和trans进行裁剪
EN

Stack Overflow用户
提问于 2010-11-01 07:10:56
回答 5查看 7.9K关注 0票数 0

当用户上传并尝试裁剪PNG或透明图像(使用.gif)时,我遇到了问题

我得到了:

代码语言:javascript
复制
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裁剪功能:

代码语言:javascript
复制
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创建,等等?然后以某种方式将其转换..

这个问题应该如何解决?

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2010-11-01 07:34:51

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

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

一旦拥有了这个通用图像对象,您就可以对它做任何您想做的事情(包括使用imagecopyresampled()),然后使用imagejpeg()从它创建一个jpeg图像。

编辑:

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

代码语言:javascript
复制
$filename = pathinfo($_POST['fname']); // Returns an array of file details
$extension = $filename['extension'];

如果从$_POST值获取文件名,则需要确保将临时图像文件复制到该文件名。我没有看到在您的代码中使用了任何$_FILES值(可能是在另一个脚本中?)。如果不是,则返回here's a good tutorial on handling file uploads。它还涵盖了文件扩展名。

票数 2
EN

Stack Overflow用户

发布于 2014-02-05 23:12:13

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

代码语言:javascript
复制
<?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);


?>
票数 1
EN

Stack Overflow用户

发布于 2010-11-01 07:22:27

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4065659

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档