gpt4 book ai didi

php imagepng() 仅在服务器上生成黑色图像

转载 作者:搜寻专家 更新时间:2023-10-31 20:46:31 25 4
gpt4 key购买 nike

我正在使用我基于的脚本从用户上传的声音文件动态生成波形图像:http://andrewfreiday.com/2010/04/29/generating-mp3-waveforms-with-php/

该脚本在我的开发环境 Windows 7、Apache2、PHP 5 上运行良好。但是当我将它放在我的服务器 Ubuntu Linux、Apache2 PHP 5 上时,imagepng() 输出一个黑框。

我研究过类似的问题,例如 Why is this creating a black image?并确保我的 imagealphablending() 和 imagesavealpha() 正在按照描述的方式使用。但仍然没有运气。我检查了文件夹权限并确认 LAME 可以写入正确的文件夹而不会引发错误。

我还尝试简单地将背景颜色设置为与我的页面背景颜色相同的颜色,因为透明度是“可有可无”,而不是必需的。

无论如何,这是输出我的图像的 PHP:

// 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.";


}

这是调用它的 Javascript:

//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)}
});
},

两天来我一直在努力解决这个问题,我们将不胜感激。

最佳答案

尝试添加

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

$rimg = imagecreatetruecolor($width, $height);
imagealphablending($rimg, false);
imagesavealpha($rimg, true);

整体部分:

$rimg = imagecreatetruecolor($width, $height);
imagealphablending($rimg, false);
imagesavealpha($rimg, true);
$transparentColor = imagecolorallocatealpha($rimg, 0, 0, 0, 127);
imagefill($rimg, 0, 0, $transparentColor);

关于php imagepng() 仅在服务器上生成黑色图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12521136/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com