PHP implementation _php tutorial for converting WAV files to image files and displaying them in a page

Source: Internet
Author: User
Tags fread 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:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21st

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

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/987722.html www.bkjia.com true http://www.bkjia.com/PHPjc/987722.html techarticle PHP implements a method of converting WAV files to image files and displaying them in a page This article describes the methods that PHP implements to convert WAV files into image files and display them in a page. Share to everyone ...

  • 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.