dataimagepng php_PHP imagepng()只产生在服务器上的黑色图像

I am dynamically generating a waveform image from a user-uploaded sound file, using a script I've based on: http://andrewfreiday.com/2010/04/29/generating-mp3-waveforms-with-php/

The script works fine on my dev environment Windows 7, Apache2, PHP 5. But when I put it on my server Ubuntu Linux, Apache2 PHP 5, imagepng() outputs a black box.

I've looked into similar problems, such as Why is this creating a black image? and have made sure that my imagealphablending() and imagesavealpha() are being used the way described. But still no luck. I've checked folder permissions and confirmed that LAME can write into the proper folder without throwing an error.

I've also tried to simply set the background color to the same color as my pages backgground, as the transparency is a "nice to have," not a necessity.

Anyway, here is the PHP that outputs my image:

// want it resized?

if ($width) {

// resample the image to the proportions defined in the form

$rimg = imagecreatetruecolor($width, $height);

// save alpha from original image

imagealphablending($rimg, false);

imagesavealpha($rimg, true);

// copy to resized

imagecopyresampled($rimg, $img, 0, 0, 0, 0, $width, $height, imagesx($img), imagesy($img));

imagepng($rimg, "../img/" . $genFileName .".png");

imagedestroy($rimg);

} else {

imagealphablending($img, false);

imagesavealpha($img, true);

imagepng($img, "../img/" . $genFileName .".png");

}

imagedestroy($img);

echo "img/" . $genFileName . ".png";

} else {

echo "An error.";

}

And this is the Javascript that calls it:

//pass the audio data to the server to have the wave drawn

_generateWaveForm = function(_file){

//create the form data

_form.append('file',_file); //mp3 to be sent to the server

_form.append('height',300); //height of image to be returned

_form.append('width',window.innerWidth - 20); //width of image to be returned

_form.append('foreground','#FFFF51'); //color of image to be returned

_form.append('background',''); //background (left empty for transparent BG)

_form.append('flat',true); //setting flat to true

//pass it on

$.ajax({

url: "php/php-waveform-png_3.php",

type: "POST",

data: _form,

processData: false,

contentType: false,

success: function(_result){_gotTheWaveForm(_result)}

});

},

I've been banging my head against this for two days now, any help will be appreciated.

解决方案

Try adding

$transparentColor = imagecolorallocatealpha($rimg, 0, 0, 0, 127);

imagefill($rimg, 0, 0, $transparentColor);

to

$rimg = imagecreatetruecolor($width, $height);

imagealphablending($rimg, false);

imagesavealpha($rimg, true);

Whole part:

$rimg = imagecreatetruecolor($width, $height);

imagealphablending($rimg, false);

imagesavealpha($rimg, true);

$transparentColor = imagecolorallocatealpha($rimg, 0, 0, 0, 127);

imagefill($rimg, 0, 0, $transparentColor);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值