我正在使用PHP和GD来裁剪并输出一张图片,以下是代码。它可以正常工作,但是当我传递一个透明的PNG图像时,会生成黑色背景。我该如何解决这个问题?
//setup
switch ($source_type) {
case IMAGETYPE_JPEG: $source = imagecreatefromjpeg($img_path); break;
case IMAGETYPE_PNG: $source = imagecreatefrompng($img_path); break;
}
// setup cropped destination
$cropped = imagecreatetruecolor($cropped_width, $cropped_height);
// create cropped image
$x = (($source_width / 100) * IMAGE_X) - ($cropped_width / 2);
$y = (($source_height / 100) * IMAGE_Y) - ($cropped_height / 2);
imagecopy(
$cropped,
$source,
0, 0,
$x, $y,
$cropped_width, $cropped_height
);
// output inc header
header('Content-type: image/jpeg');
imagejpeg($cropped);