簡體   English   中英

如何在PHP中使用GD設置透明背景?

[英]How to set a transparent background with GD in PHP?

我們有一個腳本可生成MP3波形圖像,並且當前使用指定的前景和背景色。 我想要的是使背景完全透明。

我已經弄亂了以下腳本,但是我不知道自己在做什么。 關於如何使其僅生成前景色而不生成背景的任何想法?

 /**
       * 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";

這就是您要尋找的。

    // fill background with transparent color / white
    $transColor = imagecolortransparent($img, imagecolorallocatealpha($img, 255, 255, 255, 127));
    imagefill($img, 0, 0, $transColor);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM