PHP achieves special effect on Chinese circular seals

Source: Internet
Author: User

PHP achieves special effect on Chinese circular seals

After a whim, I wrote a circular seal generator. It took a lot of effort to rotate the arc shape of the font. Finally, the results were good, and the code was archived.

Method 1:

?

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

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

<? Php

/*

* Chinese circular seals

* @ Author lkk/lianq.net

* @ Create on

* @ Example:

* $ Seal = new circleSeal ('You and I are standing in the middle east, west, north and south ', 40 );

* $ Seal-> doImg ();

*/

 

Class circleSeal {

Private $ sealString; // seal character

Private $ strMaxLeng; // maximum character Length

Private $ sealRadius; // seal radius

Private $ rimWidth; // border Thickness

Private $ innerRadius; // inner radius

Private $ startRadius; // star radius

Private $ startAngle; // The angle of the pentagram tilt.

Private $ backGround; // seal color

Private $ centerDot; // Center Coordinate

Private $ img; // graphical resource handle

Private $ font; // The specified font.

Private $ fontSize; // specify the font size

Private $ width; // Image width

Private $ height; // Image height

Private $ points; // coordinates of each vertex of the pentagram

Private $ charRadius; // string radius

Private $ charAngle; // string Skew Angle

Private $ spacing; // character interval Angle

 

// Constructor

Public function _ construct ($ str = '', $ rad = 75, $ rmwidth = 6, $ strad = 24, $ stang = 0, $ crang = 0, $ fsize = 16, $ inrad = 0 ){

$ This-> sealString = empty ($ str )? 'Seal test string': $ str;

$ This-> strMaxLeng = 12;

$ This-> sealRadius = $ rad;

$ This-> rimWidth = $ rmwidth;

$ This-> startRadius = $ strad;

$ This-> startAngle = $ stang;

$ This-> charAngle = $ crang;

$ This-> centerDot = array ('x' => $ rad, 'y' => $ rad );

$ This-> font = dirname (_ FILE _). '/simkai. ttf ';

$ This-> fontSize = $ fsize;

$ This-> innerRadius = $ inrad; // The default value is 0. No

$ This-> spacing = 1;

}

 

// Create image resources

Private function createImg (){

$ This-> width = 2 * $ this-> sealRadius;

$ This-> height = 2 * $ this-> sealRadius;

$ This-> img = imagecreate ($ this-> width, $ this-> height );

Imagecolorresolvealpha ($ this-> img, 255,255,255,127 );

$ This-> backGround = imagecolorallocate ($ this-> img, 255, 0 );

}

 

// Draw a seal border

Private function drawRim (){

For ($ I = 0; $ I <$ this-> rimWidth; $ I ++ ){

Imagearc ($ this-> img, $ this-> centerDot ['X'], $ this-> centerDot ['y'], $ this-> width-$ I, $ this-> height-$ I, 0,360, $ this-> backGround );

}

}

 

// Draw incircle

Private function drawInnerCircle (){

Imagearc ($ this-> img, $ this-> centerDot ['X'], $ this-> centerDot ['y'], 2 * $ this-> innerRadius, 2 x $ this-> innerRadius, 0,360, $ this-> backGround );

}

 

// Draw a string

Private function drawString (){

// Encoding

$ Charset = mb_detect_encoding ($ this-> sealString );

If ($ charset! = 'Utf-8 '){

$ This-> sealString = mb_convert_encoding ($ this-> sealString, 'utf-8', 'gbk ');

}

 

// Metering

$ This-> charRadius = $ this-> sealRadius-$ this-> rimWidth-$ this-> fontSize; // string radius

$ Leng = mb_strlen ($ this-> sealString, 'utf8'); // String Length

If ($ leng> $ this-> strMaxLeng) $ leng = $ this-> strMaxLeng;

$ AvgAngle = 360/($ this-> strMaxLeng); // average character skew

 

// Split and write strings

$ Words = array (); // character array

For ($ I = 0; $ I <$ leng; $ I ++ ){

$ Words [] = mb_substr ($ this-> sealString, $ I, 1, 'utf8 ');

$ R = 630 + $ this-> charAngle + $ avgAngle * ($ I-$ leng/2) + $ this-> spacing * ($ i-1); // coordinate Angle

$ R = 720-$ this-> charAngle + $ avgAngle * ($ leng-2 * $ i-1)/2 + $ this-> spacing * (1-$ I ); // character Angle

$ X = $ this-> centerDot ['X'] + $ this-> charRadius * cos (deg 2rad ($ r); // x coordinate of the character

$ Y = $ this-> centerDot ['y'] + $ this-> charRadius * sin (deg 2rad ($ r); // y coordinate of the character

Imagettftext ($ this-> img, $ this-> fontSize, $ R, $ x, $ y, $ this-> backGround, $ this-> font, $ words [$ I]);

}

}

 

// Draw a star

Private function drawStart (){

$ Ang_out = 18 + $ this-> startAngle;

$ Ang_in = 56 + $ this-> startAngle;

$ Rad_out = $ this-> startRadius;

$ Rad_in = $ rad_out * 0.382;

For ($ I = 0; $ I <5; $ I ++ ){

// Five vertex coordinates

$ This-> points [] = $ rad_out * cos (2 * M_PI/5 * $ I-deg 2rad ($ ang_out )) + $ this-> centerDot ['X'];

$ This-> points [] = $ rad_out * sin (2 * M_PI/5 * $ I-deg 2rad ($ ang_out )) + $ this-> centerDot ['y'];

 

// Inner Concave Coordinate

$ This-> points [] = $ rad_in * cos (2 * M_PI/5 * ($ I + 1)-deg 2rad ($ ang_in )) + $ this-> centerDot ['X'];

$ This-> points [] = $ rad_in * sin (2 * M_PI/5 * ($ I + 1)-deg 2rad ($ ang_in )) + $ this-> centerDot ['y'];

}

Imagefilledpolygon ($ this-> img, $ this-> points, 10, $ this-> backGround );

}

 

// Output

Private function outPut (){

Header ('content-type: image/png ');

Imagepng ($ this-> img );

Imagedestroy ($ this-> img );

}

 

// Generate externally

Public function doImg (){

$ This-> createImg ();

$ This-> drawRim ();

$ This-> drawInnerCircle ();

$ This-> drawString ();

$ This-> drawStart ();

$ This-> outPut ();

}

}

Method 2:

?

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

64

65

66

67

68

69

70

<? Php

@ $ Hos = iconv ("GBK", "UTF-8", $ _ GET ["hos"]);

If (! Isset ($ hos ))

Exit;

 

$ Im = ImageCreate (150,150 );

$ Gray = ImageColorResolveAlpha ($ im, 200,200,200,127 );

$ Red = ImageColorAllocate ($ im, 230,150,150 );

 

For ($ I = 0; $ I <6; $ I ++)

ImageArc ($ im, 75, 148-$ I, 148-$ I, 0,360, $ red );

 

$ Stock = 'C: \ WINDOWS \ Fonts \ simkai. ttf ';

$ Point ="★";

$ Size = 30;

ImageTTFText ($ im, $ size,-$ size/+ $ size/2, $ red, $ stock, $ point );

 

$ A = 75; $ B =-75; // Center Coordinate

$ R = 65; $ m = 40; // radius, Angle

$ Size = 16; // font size

$ R = $ r-$ size;

 

$ Word = array ();

$ Max = 18;

$ Count = mb_strlen ($ hos, 'utf8 ');

If ($ count> $ max) $ count = $ max;

If ($ count> 12)

$ M = floor (360/$ count );

Else if ($ count> 5)

$ M-= $ count;

 

For ($ I = 0; $ I <$ count; $ I ++)

$ Word [] = mb_substr ($ hos, $ I, 1, 'utf8 ');

 

$ J = floor ($ count/2 );

If ($ j! = $ Count/2)

{

For ($ I = $ j; $ I >=0; $ I --)

{

$ Arc = $ m * ($ j-$ I) + $ size/2;

$ X = round ($ r * cos (90 + $ arc) * M_PI/180) + $;

$ Y =-1 * (round ($ r * sin (90 + $ arc) * M_PI/180) + $ B );

If ($ arc <10) $ arc = 0;

ImageTTFText ($ im, $ size, $ arc, $ x, $ y, $ red, $ stock, $ word [$ I]);

$ Arc = $ m * ($ j-$ I)-$ size/2;

$ X = round ($ r * cos (90-$ arc) * M_PI/180) + $;

$ Y =-1 * (round ($ r * sin (90-$ arc) * M_PI/180) + $ B );

If ($ arc <10) $ arc = 0;

ImageTTFText ($ im, $ size,-$ arc, $ x, $ y, $ red, $ stock, $ word [$ j + $ j-$ I]);

}

}

Else

{

$ J = $ J-1;

For ($ I = $ j; $ I >=0; $ I --)

{

$ Arc = $ m/2 + $ m * ($ j-$ I) + $ size/2;

$ X = round ($ r * cos (90 + $ arc) * M_PI/180) + $;

$ Y =-1 * (round ($ r * sin (90 + $ arc) * M_PI/180) + $ B );

ImageTTFText ($ im, $ size, $ arc, $ x, $ y, $ red, $ stock, $ word [$ I]);

$ Arc = $ m/2 + $ m * ($ j-$ I)-$ size/2;

$ X = round ($ r * cos (90-$ arc) * M_PI/180) + $;

$ Y =-1 * (round ($ r * sin (90-$ arc) * M_PI/180) + $ B );

ImageTTFText ($ im, $ size,-$ arc, $ x, $ y, $ red, $ stock, $ word [$ j + 1-$ I]);

}

}

 

Header ('content-Type: image/png ');

ImagePNG ($ im );

?>

The above is all the content of this article. I hope you will like it.

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.