0

We have a script that generates MP3 waveform images, and it currently uses a specified foreground and background color. What I'd like is to have the background completely transparent.

I've messed with the below script a bit but I have no idea what I'm doing. Any ideas on how to get it to where it only generates the foreground color and not the background?

 /**
       * Image generation
       */
      // how much detail we want. Larger number means less detail
      // (basically, how many bytes/frames to skip processing)
      // the lower the number means longer processing time
      define("DETAIL", 5);

      // get user vars from form
      $width = isset($_POST["width"]) ? (int) $_POST["width"] : 775;
      $height = isset($_POST["height"]) ? (int) $_POST["height"] : 122;
      $background = isset($_POST["background"]) ? $_POST["background"] : $background_color;
      $foreground = isset($_POST["foreground"]) ? $_POST["foreground"] : $foreground_color;

      // create original image width based on amount of detail
      $img = imagecreatetruecolor(sizeof($data) / DETAIL, $height);

      // fill background of image
      list($r, $g, $b) = html2rgb($background);
      imagefilledrectangle($img, 0, 0, sizeof($data) / DETAIL, $height, imagecolorallocate($img, $r, $g, $b));

      // generate background color
      list($r, $g, $b) = html2rgb($foreground);

      // loop through frames/bytes of wav data as genearted above
      for($d = 0; $d < sizeof($data); $d += DETAIL) {
        // relative value based on height of image being generated
        // data values can range between 0 and 255
        $v = (int) ($data[$d] / 255 * $height);
        // draw the line on the image using the $v value and centering it vertically on the canvas
        imageline($img, $d / DETAIL, 0 + ($height - $v), $d / DETAIL, $height - ($height - $v), imagecolorallocate($img, $r, $g, $b));
      }

      $outPngName= $outputName.".png";
halfer
  • 19,824
  • 17
  • 99
  • 186
user1610904
  • 397
  • 3
  • 20
  • You never bothered filling the canvas with a transparent color, so by default it's an opaque image: http://php.net/manual/en/function.imagecolorallocatealpha.php – Marc B Jun 27 '14 at 18:23
  • Gotcha thanks Marc. Any idea how to implement imagecolorallocatealpha in the code I posted? I don't really know php very well at all. – user1610904 Jun 27 '14 at 18:25
  • Possible duplicate of [PHP/GD - transparent background](http://stackoverflow.com/questions/15246813/php-gd-transparent-background). Other [possible dups here](http://stackoverflow.com/search?q=php+gd+transparent+background). – halfer Jun 27 '14 at 18:41

1 Answers1

0

This is what you are looking for.

    // fill background with transparent color / white
    $transColor = imagecolortransparent($img, imagecolorallocatealpha($img, 255, 255, 255, 127));
    imagefill($img, 0, 0, $transColor);
cmorrissey
  • 8,493
  • 2
  • 23
  • 27
  • Thanks so much. I've tried adding this in certain places and all I'm getting is a black background. If possible could you tell me where to place this and/or what to delete in my above code? – user1610904 Jun 27 '14 at 19:07