Php: how to convert a wav file to an image file and display it on the page _ PHP Tutorial

Source: Internet
Author: User
Php converts a wav file into an image file and displays it on the page. Php: convert a wav file to an image file and display it on the page. This document describes how php converts a wav file to an image file and displays it on the page. How php converts wav files into image files and displays them on the page

This example describes how php converts a wav file into an image file and displays it on the page. Share it with you for your reference. The specific analysis is as follows:

Requirement: convert a wav file to a png file and display it.

Wav_To_Png.php:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

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, 36 + $ header ['headsize']-16 );

$ Raw = fread ($ fp, 8 );

$ Data = unpack ('a4data/VDataSize ', $ raw );

Foreach ($ data as $ k => $ v)

$ Str. = $ k. ':'. $ v .'';

$ B = $ header ['samples'];

$ 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 = 200; $ f = pow (2, $ B-1)/$ h ;}

If ($ w = 0)

$ W = round ($ r/1000); // default to show 1 k 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 ');

?>

As follows:

I hope this article will help you with php programming.

Examples in this article describes how php converts wav files into image files and displays them on the page. Share it with you...

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.