php的优势和背景,渲染彩色背景与透明前景在PHP中

我在上传到服务器时直接从MP3中直接渲染音频波形。我的上传脚本当前保存了原始的mp3并将波形渲染为png。

目前我首先将背景渲染成矩形。此代码根据$ background的值生成透明或彩色背景:

我正在尝试在PHP中创建一个透明的png叠加层。

if (!$img) {

// create original image width based on amount of detail

// each waveform to be processed with be $height high, but will be condensed

// and resized later (if specified)

$img = imagecreatetruecolor($data_size / DETAIL, $height * sizeof($wavs_to_process));

// fill background of image

if ($background == "") {

// transparent background specified

imagesavealpha($img, true);

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

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

} else {

list($br, $bg, $bb) = html2rgb($background);

imagefilledrectangle($img, 0, 0, (int) ($data_size / DETAIL), $height * sizeof($wavs_to_process), imagecolorallocate($img, $br, $bg, $bb));

}

}然后,我循环遍历一个动态重采样MP3的数据点,并为每个点绘制一条线到这个呈现波形图像的背景上。

我使用此代码来生成波形图像:

// don't print flat values on the canvas if not necessary

if (!($v / $height == 0.5 && !$draw_flat))

// draw the line on the image using the $v value and centering it vertically on the canvas

imageline(

$img,

// x1

(int) ($data_point / DETAIL),

// y1: height of the image minus $v as a percentage of the height for the wave amplitude

$height * $wav - $v,

// x2

(int) ($data_point / DETAIL),

// y2: same as y1, but from the bottom of the image

$height * $wav - ($height - $v),

imagecolorallocate($img, $r, $g, $b)

);

} else {

// skip this one due to lack of detail

fseek($handle, $ratio + $byte, SEEK_CUR);

}

}这工作得很好,但是我需要将波形创建为透明覆盖图,以便将其放置在具有CSS渐变的div上。所以,我需要将背景渲染为#ffffff,而实际的波形本身需要透明。实质上,我需要在制作的png上反转透明度。

我曾尝试使用:

imagecolorallocatealpha($img, 0, 0, 0, 127)在波形渲染部分,但总是看起来最后只是一个没有可见波形的彩色矩形,我不知道我出错的地方。

任何帮助将不胜感激。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值