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);