PHP implements the method of converting WAV files into image files and displaying them in the page _php tips

Source: Internet
Author: User
Tags fread pow unpack

This article describes the PHP implementation of the WAV file into the image file and display in the page method. Share to everyone for your reference. The specific analysis is as follows:

Requirements: Convert WAV files to PNG files and display them.

wav_to_png.php:

<?php function Wav_graph ($file, $f =0, $w =0) {global $DATA _dir;
  if (!is_file ($file)) return 0;
  $fp = fopen ($DATA _dir. $file, ' R ');
  $raw = Fread ($fp, 36);
  $str = '; $header = Unpack (' a4riff/vsize/a4wav/a4head/vheadsize/vpcm/vchannels/vsamplerate/vbyterate/vblockalign/
  Vsamplebits ', $raw); foreach ($header as $k => $v) $str. = $k. ': '.
  $v. ';
  Fseek ($fp, + $header [' headsize ']-16);
  $raw = Fread ($fp, 8);
  $data = Unpack (' a4data/vdatasize ', $raw); foreach ($data as $k => $v) $str. = $k. ': '.
  $v. ';
  $b = $header [' samplebits '];
  $c = $header [' channels ']; $l = $b * $c/8; Sample frame length in bytes $s = $data [' datasize ']/$l;
  Total number of samples $r = $header [' samplerate '];
  if ($f) $h = POW (2, $b)/$f;
  else {$h = $f = Pow (2, $b-1)/$h;} if ($w = = 0) $w = round ($r/1000);
  Default to show 1k sample frames per minute header ("Content-type:image/png");
  $im = Imagecreate ($s/$w, $h * $c * 2); ImagecolorAllocate ($im, 0xFF, 0xFF, 0xff); White bg $color = imagecolorallocate ($im, 0, 0, 255);
  Black Imagestring ($im, 5, 5, 5, $STR, $color); $x = 0; $y = Array ();
  $yn = Array ();
  for ($i = 0; $i < $c; $i + +) $y [$i] = $h * $i + $h;
  $n = $l * $W;
   while (1) {if ($s = = 0) break;
   if ($s < $n) $n = $s;
   $samples = Fread ($fp, 1000 * $n);
   if ($samples = = FALSE) break;
   $packed = Unpack ("s*", $samples);
    foreach ($packed as $k => $v) {$cnt = ($k-1)% ($w * $l);
    if ($cnt > $c-1) continue;
    $yn [$CNT] = $h * $cnt + $h-$v/$f;
    Imageline ($im, $x, $y [$cnt], $x +1, $yn [$cnt], $color);
    $y [$cnt] = $yn [$cnt];
   $x + +;
  } $s-= $n;
  } imagepng ($im);
 Imagedestroy ($im); } wav_graph (' Getwave.wav ');?>

The effect chart is as follows:

I hope this article will help you with your PHP program design.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.