简体   繁体   English

在PHP中渲染具有透明前景的彩色背景

[英]Rendering a coloured background with transparent foreground in PHP

I am rendering audio waveforms directly from MP3's on the fly as they are uploaded to the server. 我正在直接从MP3实时渲染音频波形,因为它们已上传到服务器。 My upload script currently saves both the original mp3 and renders the waveform to a png. 我的上传脚本目前同时保存了原始mp3并将波形渲染为png。

Currently I render the background first to a rectangle. 目前,我先将背景渲染为矩形。 This code produces either a transparent or coloured background dependant upon the value of $background: 此代码根据$ background的值生成透明或彩色背景:

I am trying to create a transparent png overlay in PHP. 我正在尝试在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));
        }
      }

I then loop through the data points of a dynamically resampled MP3 and for each point I draw a line onto this background which renders the waveform image. 然后,我遍历动态重新采样的MP3的数据点,并为每个点在此背景上绘制一条线,以呈现波形图像。

I use this code to produce the waveform image: 我使用以下代码生成波形图像:

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

This works perfectly, however I need to create the waveform as a transparent overlay in order to place it over a div with a CSS gradient. 这可以完美地工作,但是我需要将波形创建为透明的覆盖层,以便将其放置在具有CSS渐变的div上。 So, I need to render the background as #ffffff and the actual waveform itself needs to be transparent. 因此,我需要将背景渲染为#ffffff,并且实际波形本身必须是透明的。 In essence I need the transparency inverted on the produced png's. 本质上,我需要在生成的png上反转透明度。

I have tried using: 我试过使用:

imagecolorallocatealpha($img, 0, 0, 0, 127)

On the waveform rendering portion but always seem to just end up with a coloured rectangle with no visible waveform and I'm not sure where I am going wrong. 在波形渲染部分,但总是看起来总是以一个彩色矩形结束,没有可见的波形,我不确定哪里出了问题。

Any help would be greatly appreciated. 任何帮助将不胜感激。

Please try the following. 请尝试以下方法。 Disable blending mode Docs for the image: 禁用图像的混合模式文档

imagealphablending($img, FALSE);

This will enable that you can set pixels with alpha information directly. 这将使您可以直接设置具有alpha信息的像素。 To do so, you need to allocate the color with the alpha Docs set to 100% transparent as well: 要做到这一点,你需要分配阿尔法文档的颜色设置为100%透明,以及:

imagecolorallocatealpha($img ,$r, $g, $b, $alpha = 127);

BTW, you can allocate the color once and then re-use it, so you don't need to call the imagecolorallocatealpha function that often. 顺便说一句,您可以分配一次颜色然后再使用它,因此您不需要经常调用imagecolorallocatealpha函数。

Let me know if this works. 让我知道这个是否奏效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM