Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7008937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:47:57+00:00 2026-05-27T21:47:57+00:00

I am rendering audio waveforms directly from MP3’s on the fly as they are

  • 0

I am rendering audio waveforms directly from MP3’s on the fly as they are uploaded to the server. My upload script currently saves both the original mp3 and renders the waveform to a 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:

I am trying to create a transparent png overlay in PHP.

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.

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. So, I need to render the background as #ffffff and the actual waveform itself needs to be transparent. In essence I need the transparency inverted on the produced png’s.

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T21:47:58+00:00Added an answer on May 27, 2026 at 9:47 pm

    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. To do so, you need to allocate the color with the alpha­Docs set to 100% transparent as well:

    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.

    Let me know if this works.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am rendering my div with a javascript from the server side code. The
I'm currently rendering the visible part of a view to a PNG, like so:
When rendering 3D rectangles (i.e. rectangles in 3D space), of course, they are specified
I'm rendering a drop down box that contains a currently selected value using selected=true
I'm looking for a library capable of creating an audio file (mp3 or wav).
I am using the Audio Unit services to set up an output rendering callback
Rendering a 3D-map from an Openstreetmap data can be pretty cumbersome . But what
While rendering the view page, based on some condition in the controller action I
I am rendering a rails partial and I want to alternate the background color
I'm having major rendering issues in Safari with the web application I'm working on.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.