PHP implementation of Chinese round stamp effect _php tutorial

Source: Internet
Author: User

PHP for Chinese round stamp effect


Whim, wrote a round stamp generator, the font of the arc rotation is a lot of effort. Finally, the effect is good, code archiving.

Method One:

?

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

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

/*

* Chinese round seal type

* @author lkk/lianq.net

* @create on 10:03 2012-5-29

* @example:

* $seal = new Circleseal (' You and me he sits East ', 75,6,24,0,0,16,40);

* $seal->doimg ();

*/

Class Circleseal {

Private $sealString; Stamp character

Private $strMaxLeng; Maximum characters length

Private $sealRadius; Seal radius

Private $rimWidth; Border thickness

Private $innerRadius; Radius of Inner Circle

Private $startRadius; Pentagram radius

Private $startAngle; Pentagram Tilt Angle

Private $backGround; Seal color

Private $centerDot; Center coordinates

Private $img; Graphics resource Handle

Private $font; The specified font

Private $fontSize; Specify font size

Private $width; Picture width

Private $height; Picture height

Private $points; Point coordinates of the pentagram

Private $charRadius; String radius

Private $charAngle; String Tilt angle

Private $spacing; Character Interval angle

Construction method

Public function __construct ($str = ", $rad = $rmwidth = 6, $strad =, $stang = 0, $crang = 0, $fsize = +, $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; Default 0, no

$this->spacing = 1;

}

Create a picture resource

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,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 Inner Circle

Private Function drawinnercircle () {

Imagearc ($this->img, $this->centerdot[' x '], $this->centerdot[' y '],2* $this->innerradius,2* $this innerradius,0,360, $this->background);

}

Draw a String

Private Function drawstring () {

Encoding processing

$charset = mb_detect_encoding ($this->sealstring);

if ($charset! = ' UTF-8 ') {

$this->sealstring = mb_convert_encoding ($this->sealstring, ' UTF-8 ', ' GBK ');

}

Related 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 =/($this->strmaxleng); Average character tilt

Splitting and writing 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 (Deg2rad ($r)); The x-coordinate of the character

$y = $this->centerdot[' y ') + $this->charradius * sin (Deg2rad ($r)); The y-coordinate of the character

Imagettftext ($this->img, $this->fontsize, $R, $x, $y, $this->background, $this->font, $words [$i]);

}

}

Draw a Pentagram

Private Function Drawstart () {

$ang _out = + $this->startangle;

$ang _in = + $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-deg2rad ($ang _out)) + $this->centerdot[' x '];

$this->points[] = $rad _out * sin (2*m_pi/5* $i-deg2rad ($ang _out)) + $this->centerdot[' y '];

The point coordinates of the inner concave

$this->points[] = $rad _in * cos (2*m_pi/5* ($i + 1)-Deg2rad ($ang _in)) + $this->centerdot[' x '];

$this->points[] = $rad _in * sin (2*m_pi/5* ($i + 1)-Deg2rad ($ang _in)) + $this->centerdot[' y '];

}

Imagefilledpolygon ($this->img, $this->points, $this->background);

}

Output

Private Function OutPut () {

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

Imagepng ($this->img);

Imagedestroy ($this->img);

}

External generation

Public Function doimg () {

$this->createimg ();

$this->drawrim ();

$this->drawinnercircle ();

$this->drawstring ();

$this->drawstart ();

$this->output ();

}

}

Method Two:

?

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

64

65

66

67

68

69

70

@ $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,75,148-$i, 148-$i, 0,360, $red);

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

$point = "★";

$size = 30;

Imagettftext ($im, $size, 0,72-$size/2,72+ $size/2, $red, $stock, $point);

$a =75; $b =-75;//Center Point coordinates

$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) + $a;

$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) + $a;

$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) + $a;

$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) + $a;

$y =-1* (Round ($r *sin ((90-$arc) *m_pi/180) + $b);

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

}

}

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

Imagepng ($im);

?>

The above is the whole content of this article, I hope you can enjoy

http://www.bkjia.com/PHPjc/1018929.html www.bkjia.com true http://www.bkjia.com/PHPjc/1018929.html techarticle PHP to achieve the Chinese circular seal special effects of the whim, wrote a round stamp generator, the font of the arc rotation quite a lot of effort. Finally, the effect is good, code archiving. Method One:? 1 2 3 4 ...

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