图像作物显示黑bg

时间:2010-02-11 23:05:23

标签: 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 = 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);

1 个答案:

答案 0 :(得分:0)

看看这些功能:

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