PHP implements the method of converting WAV files to image files and displaying them in a page, _php tutorial

Source: Internet
Author: User
Tags unpack

PHP implements the method of converting a WAV file into an image file and displaying it in a page.


The examples in this paper are the methods that PHP implements to convert WAV files into image files and display them in a page. 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, $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 ');? >

As follows:

I hope this article is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/987892.html www.bkjia.com true http://www.bkjia.com/PHPjc/987892.html techarticle PHP implements the method of converting WAV files to image files and displaying them on the page, and this article describes the methods that PHP implements to convert WAV files into image files and display them on the page. Share to the big ...

  • Related Article

    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.