私はオーディオ波形をサーバにアップロードする際にオンザフライで直接レンダリングしています。私のアップロードスクリプトは、元のmp3を保存し、波形をpngにレンダリングします。PHPで透明なフォアグラウンドで色付きの背景をレンダリングする
現在、背景を最初に矩形にレンダリングします。このコードは、$ backgroundの値に応じて透明または色付きの背景を生成します。
PHPで透明なpngオーバーレイを作成しようとしています。動的再サンプリングMP3のデータポイントを介して、各点に対する
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));
}
}
Iは、ループは、Iは、波形画像を描画このような背景の上に線を引きます。
私は波形画像を生成するために、このコードを使用します。
// 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)
を常にちょうど目に見える波形と色付きの矩形で終わるように見えると私は私が間違っているつもりですどこか分かりません。
ご協力いただければ幸いです。
イメージにレンダリングする代わりに、HTMLの 'canvas'要素を使用することを検討しましたか?提案よりも多くの考え。 – Treffynnon
最初にすべてを塗りつぶしたら、波形のピクセルを「削除」してその部分をもう一度透明にしたいと思いますか? – hakre
それは正しいhakreです。私はそれが可能であるかどうかも分からないし、もちろん代替の提案にもオープンしている。 – gordyr