2010-02-11 117 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