PHP imagepng()只产生在服务器上的黑色图像 [英] php imagepng() generating black image only on server

查看:891
本文介绍了PHP imagepng()只产生在服务器上的黑色图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是动态生成从用户上传的声音文件的波形图像,利用我已经基于一个脚本:的 http://andrewfreiday.com/2010/04/29/generating-mp3-waveforms-with-php/

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/

该脚本工作正常在我的开发环境的Windows 7的Apache2,PHP 5。但是,当我把它放在我的服务器Ubuntu Linux操作系统,Apache2的PHP 5,imagepng()输出一个黑盒子。

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.

我已经研究过类似的问题,比如这是为什么创建黑色图像?并已确保我imagealphablending()和imagesavealpha()正在使用中描述的方式。但仍没有运气。我检查了文件夹的权限,并确认LAME可以写入正确的文件夹而不发出一个错误。

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.

我也试着简单地将背景色设置为相同的颜色作为我的网页backgground,因为透明度是一个不错的,而不是必需品。

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.

总之,这里是我的输出图像的PHP:

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


}

这是JavaScript调用它:

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

我一直在敲我的头这两天,任何帮助将AP preciated。

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

推荐答案

尝试添加

$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()只产生在服务器上的黑色图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆