网页裁剪图片(FileAPI)

2020-12-13 06:13

阅读:534

标签:des   style   http   color   使用   os   io   strong   

网页端裁剪图片(FileAPI),兼容谷歌火狐IE6/7/8。
高版本浏览器用canvas 裁剪,低版本用flash过度。
FileAPI 的应用实例。

效果

soscw.com,搜素材

 
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
var ImgCropUtil = function(config) {
 
    var imgos = config.imgos;
    var cropMain = config.cropMain;
    var imgPreviewWidth = config.imgPreviewWidth;
    var ratio = 0;
 
    function getRatio(imgorigin, imgafter) {
        return imgafter.width / imgafter.height > imgorigin.width / imgorigin.height ? imgafter.height / imgorigin.height : imgafter.width / imgorigin.width;
    }
 
    function getWrapPaddings(image, imagewrapper) {
        return {
            ‘padding-top‘: (imagewrapper.height - image.matrix.dh) / 2,
            ‘padding-left‘: (imagewrapper.width - image.matrix.dw) / 2
        };
    }
 
    function getWrapPaddingsFull(image, imagewrapper) {
        return {
            ‘padding-top‘: (imagewrapper.height - image.matrix.dh) / 2,
            ‘padding-bottom‘: (imagewrapper.height - image.matrix.dh) / 2,
            ‘padding-left‘: (imagewrapper.width - image.matrix.dw) / 2,
            ‘padding-right‘: (imagewrapper.width - image.matrix.dw) / 2
        };
    }
 
    /**
     * 使用jcrop裁剪
     * @param {type} file
     * @param {type} $imgwrap 要建材的包裹img对象
     * @param {type} callback 回调 @param domimg , jcrop c object , image.matrix
     * @returns {undefined}
     */
    function getCropArea(file, $imgwrap, callback) {
        FileAPI.getInfo(file, function(err, info) {
            var image = FileAPI.Image(file);
            ratio = getRatio(info, imgos);
            image.matrix.dw = info.width * ratio;
            image.matrix.dh = info.height * ratio;
            cropMain.css(getWrapPaddingsFull(image, imgos));
 
            image.get(function(error, img1) {
                if (error) {
                    alert("图片读取出错!");
                    // return;
                }
 
                $imgwrap.empty();
                $(
).appendTo($imgwrap)
                        .html($(img1))
                        .Jcrop({
                            onSelect: function(c) {
                                image.matrix.sx = c.x / ratio;
                                image.matrix.sy = c.y / ratio;
                                image.matrix.sw = c.w / ratio;
                                image.matrix.sh = c.h / ratio;
                                var r = c.h / c.w;
                                image.matrix.dw = imgPreviewWidth;
                                image.matrix.dh = imgPreviewWidth * r;
                                image.get(function(error, domimg) {
                                    //  $(‘
‘).appendTo(imageLists).html($(domimg));
                                    callback.call(domimg, c, image.matrix);
                                    file.cropMatrix = $.extend(true, {}, image.matrix);
                                    file.cropMatrix.dw = 0;
                                    file.cropMatrix.dh = 0;
                                });
 
                            }
                        });//end jcrop
            }); //end get
 
        });  //end getInfo
    }
 
    /**
     * 生成居中的图像
     * @param {FileAPI.image} fileapi的 file 对象
     * @param  {width: *, height: *} 要生成的宽和高
     * @param {function} callback回调 @param $imgobj
     * @returns {undefined}  异步方法,不返回值
     */
    function getCenterImage(file, imgtoList, callback) {
        var $imagewrap;
        FileAPI.getInfo(file, function(err, info) {
 
            var r = getRatio(info, imgtoList);
            var img = FileAPI.Image(file);
 
            img.matrix.dw = info.width * r;
            img.matrix.dh = info.height * r;
 
            img.get(function(error, domimg) {
                if (error) {
                    alert("图片读取出错!");
                    return;
                }
                // $imagewrap = $(‘
‘).css( getWrapPaddingsFull(img,imgtoList)).append($(domimg));
                $imagewrap = $(
).append($(domimg).css({
                    ‘width‘: img.matrix.dw,
                    ‘height‘: img.matrix.dh,
                    ‘display‘: ‘block‘
                })).css(getWrapPaddingsFull(img, imgtoList));
                callback($imagewrap);
            });
 
        });
    }
    /**
     * 生成居中的img图像 ,注意设置传入img的matrix对象 sw和sh值
     * @param {FileAPI.image} fileapi的image对象
     * @param  {width: *, height: *} 要生成的宽和高
     * @param {function} callback回调 @param $imgobj
     * @returns {undefined} 异步方法,不返回值
     */
    function getCenterCropImage(img, imgtoList, callback) {
        var $imagewrap;
        var info = {width: img.matrix.sw, height: img.matrix.sh};
        var r = getRatio(info, imgtoList);
        //var img = FileAPI.Image(file);
 
        img.matrix.dw = info.width * r;
        img.matrix.dh = info.height * r;
 
        img.get(function(error, domimg) {
            if (error) {
                alert("图片读取出错!");
                return;
            }
            // $imagewrap = $(‘
‘).css( getWrapPaddingsFull(img,imgtoList)).append($(domimg));
            $imagewrap = $(
).append($(domimg).css({
                ‘width‘: img.matrix.dw,
                ‘height‘: img.matrix.dh,
                ‘display‘: ‘block‘
            })).css(getWrapPaddingsFull(img, imgtoList));
            callback($imagewrap);
        });
 
    }
 
    /**
     * 生成原始img图像,有一层div包裹
     * @param {FileAPI.file}  fileapi的file对象
     * @param  {width: *, height: *}  imgtoList 要生成的宽和高
     * @param {function} callback回调 @param $imgobj
     * @returns {undefined} 异步方法,不返回值
     */
 
    function getDomImage(file, imgtoList, callback) {
        var $imagewrap;
        FileAPI.getInfo(file, function(err, info) {
            


评论


亲,登录后才可以留言!