How to set a transparent background with GD in PHP? -
we have script generates mp3 waveform images, , uses specified foreground , background color. i'd have background transparent.
i've messed below script bit have no idea i'm doing. ideas on how generates foreground color , not background?
/** * image generation */ // how detail want. larger number means less detail // (basically, how many bytes/frames skip processing) // lower number means longer processing time define("detail", 5); // user vars 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 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 , 255 $v = (int) ($data[$d] / 255 * $height); // draw line on image using $v value , centering vertically on canvas imageline($img, $d / detail, 0 + ($height - $v), $d / detail, $height - ($height - $v), imagecolorallocate($img, $r, $g, $b)); } $outpngname= $outputname.".png";
this looking for.
// fill background transparent color / white $transcolor = imagecolortransparent($img, imagecolorallocatealpha($img, 255, 255, 255, 127)); imagefill($img, 0, 0, $transcolor);
Comments
Post a Comment