【问题标题】:How to set a transparent background with GD in PHP?如何在 PHP 中使用 GD 设置透明背景?
【发布时间】:2014-06-27 18:19:05
【问题描述】:

我们有一个生成 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";

【问题讨论】:

标签: php background transparency gd transparent


【解决方案1】:

这就是你要找的。​​p>

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

【讨论】:

  • 非常感谢。我试过在某些地方添加这个,我得到的只是黑色背景。如果可能的话,你能告诉我在我上面的代码中放置这个和/或删除什么吗?
猜你喜欢
  • 2013-02-21
  • 1970-01-01
  • 2015-04-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-19
  • 1970-01-01
  • 2017-06-30
相关资源
最近更新 更多