JQuery plug-in jcrop + Fileapi perfectly implements Image Upload + cropping + preview code sharing

Source: Internet
Author: User

JQuery plug-in jcrop + Fileapi perfectly implements Image Upload + cropping + preview code sharing

This article mainly introduces the jQuery plug-in jcrop + Fileapi perfect Image Upload + cropping + preview code, which is very simple and practical, and has great results. If you need it, you can refer to it.

Crop images on the webpage without passing through the server.

This is implemented with a https://github.com/mailru/FileAPI framework. With jcrop.

Advanced browsers use canvas cropping, and ie6 7 8 uses flash excessively.

Core code:

?

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

Var el = $ ('input'). get (0 );

 

Seajs. use (['gallery/jcrop/0.9.12/jcrop.css ', 'gallery/jcrop/0.9.12/jcrop. js'], function (){

 

FileAPI. event. on (el, 'change', function (evt ){

Var files = FileAPI. getFiles (evt); // Retrieve file list

 

FileAPI. filterFiles (files, function (file, info ){

If (! /^ Image/. test (file. type )){

Alert ('incorrect image format ');

Return false;

}

Else if (file. size> 20 * FileAPI. MB ){

Alert ('image must be smaller than 20m ');

Return false;

}

Else {

Return true;

}

 

}, Function (files, rejected ){

Console. log (files );

 

If (files. length ){

Var file = files [0];

Var img0 = FileAPI. Image (file );

Var img1 = FileAPI. Image (file );

Var ratio = 0;

FileAPI. getInfo (file, function (err, info) {// get image ratio

If (! Err ){

If (info. width> info. height ){

Ratio = info. width/500;

}

Else {

Ratio = info. height/500;

}

}

});

 

Img0.resize (500,500, 'max ') // place image and register jcrop

. Get (function (err, img ){

$ ('# Img2'). empty ();

$ ('# Img2'). append ($ (img ));

 

$ ('# Img2'). children (). Jcrop ({

AspectRatio: 1,

BgColor: 'rgba (0,0, 0, 0.4 )',

OnSelect: function (c ){

Img1.matrix. sx = c. x * ratio;

Img1.matrix. sy = c. y * ratio;

Img1.matrix. sw = c. w * ratio;

Img1.matrix. sh = c. h * ratio;

Img1.matrix. dwx = 500;

Img1.matrix. dh = 500;

 

Img1.get (function (err, img ){

// $ ('# Img3'). empty ();

// $ ('# Img3'). append ($ (img ));

('{Img3'{.html ($ (img ));

});

 

}

});

});

$ ('# Btn'). on ('click', function (){

FileAPI. upload ({

// Url: '/testUpFile/upfile ',

// Headers: {'content-type': 'multipart/form-data '},

Files: {images: img1 },

SS: function (evt ){/*...*/},

Complete: function (err, xhr ){/*...*/

// Alert (xhr. responseText );

Console. log (xhr );

}

});

});

}

});

});

});

Complete code:

?

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

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

<! DOCTYPE html>

<Html>

<Head>

<Title> TODO supply a title </title>

<Meta charset = "UTF-8">

<Meta name = "viewport" content = "width = device-width">

<Script src = "./jquery. min. js"> </script>

<Script src = "./jcrop/jquery. Jcrop. min. js"> </script>

<Link href = "./jcrop/jquery.Jcrop.min.css" rel = "stylesheet">

</Head>

<Style>

 

. Upload-btn {

Width: 130px;

Height: 25px;

Overflow: hidden;

Position: relative;

Border: 3px solid # 06c;

Border-radius: 5px;

Background: # 0cf;

 

}

. Upload-btn: hover {

Background: # 09f;

}

. Upload-btn _ txt {

Z-index: 1;

Position: relative;

Color: # fff;

Font-size: 18px;

Font-family: "Helvetica Neue ";

Line-height: 24px;

Text-align: center;

Text-shadow: 0 1px 1px #000;

}

. Upload-btn input {

Top:-10px;

Right:-40px;

Z-index: 2;

Position: absolute;

Cursor: pointer;

Opacity: 0;

Filter: alpha (opacity = 0 );

Font-size: 50px;

}

 

</Style>

 

 

<Body>

<Div>

<! -- "Js-fileapi-wrapper" -- required class -->

<Div class = "js-fileapi-wrapper upload-btn" id = "choose">

 

<Input name = "files" type = "file" multiple/>

<Button id = "btn"> upload </button>

</Div>

<Div id = "images">

 

<P style = "margin-top: 40px;"> </p>

 

<Div id = "img2"> </div>

 

<Div id = "img3"> </div>

</Div>

 

</Div>

 

<Script> window. FileAPI = {staticPath: './fileapi/'}; </script>

<Script src = "./fileapi/FileAPI. min. js"> </script>

<Script>

 

Var el = $ ('input'). get (0 );

 

 

FileAPI. event. on (el, 'change', function (evt ){

Var files = FileAPI. getFiles (evt); // Retrieve file list

 

FileAPI. filterFiles (files, function (file, info ){

If (! /^ Image/. test (file. type )){

Alert ('incorrect image format ');

Return false;

}

Else if (file. size> 20 * FileAPI. MB ){

Alert ('image must be smaller than 20m ');

Return false;

}

Else {

Return true;

}

 

}, Function (files, rejected ){

 

 

If (files. length ){

Var file = files [0];

Var img0 = FileAPI. Image (file );

Var img1 = FileAPI. Image (file );

Var ratio = 0;

FileAPI. getInfo (file, function (err, info) {// get image ratio

If (! Err ){

If (info. width> info. height ){

Ratio = info. width/500;

}

Else {

Ratio = info. height/500;

}

}

});

 

Img0.resize (500,500, 'max ') // place image and register jcrop

. Get (function (err, img ){

$ ('# Img2'). empty ();

$ ('# Img2'). append ($ (img ));

 

$ ('# Img2'). children (). Jcrop ({

AspectRatio: 1,

BgColor: 'rgba (0,0, 0, 0.4 )',

OnSelect: function (c ){

Img1.matrix. sx = c. x * ratio;

Img1.matrix. sy = c. y * ratio;

Img1.matrix. sw = c. w * ratio;

Img1.matrix. sh = c. h * ratio;

Img1.matrix. dwx = 500;

Img1.matrix. dh = 500;

 

Img1.get (function (err, img ){

// $ ('# Img3'). empty ();

// $ ('# Img3'). append ($ (img ));

('{Img3'{.html ($ (img ));

});

 

}

});

});

$ ('# Btn'). on ('click', function (){

FileAPI. upload ({

Url: '/testUpFile/upfile ',

 

Files: {images: img1 },

SS: function (evt ){/*...*/},

Complete: function (err, xhr ){/*...*/

// Alert (xhr. responseText );

 

}

});

 

});

 

}

});

});

 

</Script>

</Body>

</Html>

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.