Php 图像裁剪显示黑色背景

Php 图像裁剪显示黑色背景,php,image-processing,Php,Image Processing,我有一个图像裁剪代码,当图像被裁剪时,会在图片中添加一个黑色背景。我怎样才能删除它?谢谢 $fldcategory = $_POST['category']; $flname = $_FILES['upload']['name']; $img_src = $_FILES['upload']['tmp_name']; $thumb = "uploads/" . $flname; $title = $_POST['title']; // Open image $img = imagecreate

我有一个图像裁剪代码,当图像被裁剪时,会在图片中添加一个黑色背景。我怎样才能删除它?谢谢

$fldcategory = $_POST['category'];
$flname = $_FILES['upload']['name'];
$img_src = $_FILES['upload']['tmp_name'];
$thumb = "uploads/" . $flname;
$title = $_POST['title']; 

// Open image
$img = imagecreatefromjpeg($img_src);

// Store image width and height
list($img_width, $img_height) = getimagesize($img_src);

$width = '800';
$height = '600';

// Create the new image
$new_img = imagecreatetruecolor($width, $height);

// Calculate stuff and resize image accordingly
if (($width/$img_width) < ($height/$img_height)) {
    $new_width = $width;
    $new_height = ($width/$img_width) * $img_height;
    $new_x = 0;
    $new_y = ($height - $new_height) / 2;
} else {
    $new_width = ($height/$img_height) * $img_width;
    $new_height = $height;
    $new_x = ($width - $new_width) / 2;
    $new_y = 0;
} 

imagecopyresampled($new_img, $img, $new_x, $new_y, 0, 0, 
  $new_width, $new_height, $img_width, $img_height);

// Save thumbnail
if (is_writeable(dirname($thumb))) {
    imagejpeg($new_img, $thumb, 100);
} 

// Free up resources
imagedestroy($new_img);
imagedestroy($img);
$fldcategory=$\u POST['category';
$flname=$_文件['upload']['name'];
$img_src=$_文件['upload']['tmp_name'];
$thumb=“上传/”$姓名;
$title=$_POST['title'];
//开放图像
$img=imagecreatefromjpeg($img_src);
//存储图像宽度和高度
列表($img_宽度,$img_高度)=getimagesize($img_src);
$width='800';
$height='600';
//创建新图像
$new\u img=ImageCreateTureColor($width,$height);
//计算填充并相应调整图像大小
如果(($width/$img\u width)<($height/$img\u height)){
$new_width=$width;
$new_height=($width/$img_width)*$img_height;
$new_x=0;
$new_y=($height-$new_height)/2;
}否则{
$new_width=($height/$img_height)*$img_width;
$new_height=$height;
$new_x=($width-$new_width)/2;
$new_y=0;
} 
imagecopyresampled($new\u img,$img,$new\u x,$new\u y,0,0,
$new_width、$new_height、$img_width、$img_height);
//保存缩略图
if(可写(dirname($thumb))){
图像JPEG($new_img,$thumb,100);
} 
//释放资源
图像销毁($new_img);
图像处理(img);

看看这些功能:


可以使用第一个函数将颜色定义为透明(必须分配该颜色)。在新图像上绘制已调整大小的版本之前,先用透明颜色填充该图像。这只会使您的黑色背景不可见,并且不会将图像裁剪到正确的大小。(这只是一个对你有帮助的猜测)

请给出一个例子或更准确地描述它。很抱歉,我不想讨论这个话题,但我确实建议你使用ImageMagick(特别是使用exec)而不是GD。找不到这些网站链接。