2010-02-11 120 views
0

我有这个图像裁切代码,当图像确实被裁剪时,黑色背景被添加到图片中。我如何删除它?感谢图像裁剪显示黑色bg

$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); 
+0

请给出一个例子或更准确地描述它。 – 2010-02-11 23:06:41

+0

对不起,我真的建议你使用ImageMagick(特别是使用exec)而不是GD。 – Kirzilla 2010-02-11 23:08:11

回答

0

看一看这些功能:

您可以定义与第一功能为透明色(你必须分配该颜色) 。在绘制调整后的版本之前,用透明颜色填充新图像。这只会让你的黑色bg不可见,并且不会将图像裁剪到合适的尺寸。 (这只是猜测什么可以帮助你)

+0

找不到那些网站的链接。 – WilliamK 2017-04-29 01:38:11