aboutsummaryrefslogtreecommitdiffstats
path: root/cropper/copper.html
blob: 6e3f09ebc04dcd4e9daa907649bd6ae2c8fb38cf (plain) (blame)
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
   1.
      <script type="text/javascript" src="scripts/cropper/lib/prototype.js" language="javascript"></script>
   2.
      <script type="text/javascript" src="scripts/cropper/lib/scriptaculous.js?load=builder,dragdrop" language="javascript"></script>
   3.
      <script type="text/javascript" src="scripts/cropper/cropper.js" language="javascript"></script>

Options

ratioDim obj
    The pixel dimensions to apply as a restrictive ratio, with properties x & y.
minWidth int
    The minimum width for the select area in pixels.
minHeight int
    The mimimum height for the select area in pixels.
maxWidth int
    The maximum width for the select areas in pixels (if both minWidth & maxWidth set to same the width of the cropper will be fixed)
maxHeight int
    The maximum height for the select areas in pixels (if both minHeight & maxHeight set to same the height of the cropper will be fixed)
displayOnInit int
    Whether to display the select area on initialisation, only used when providing minimum width & height or ratio.
onEndCrop func
    The callback function to provide the crop details to on end of a crop.
captureKeys boolean
    Whether to capture the keys for moving the select area, as these can cause some problems at the moment.
onloadCoords obj
    A coordinates object with properties x1, y1, x2 & y2; for the coordinates of the select area to display onload

The callback function

The callback function is a function that allows you to capture the crop co-ordinates when the user finished a crop movement, it is passed two arguments:

    * coords, obj, coordinates object with properties x1, y1, x2 & y2; for the coordinates of the select area.
    * dimensions, obj, dimensions object with properities width & height; for the dimensions of the select area.

An example function which outputs the crop values to form fields:
Display code as plain text
JavaScript:

   1.
      function onEndCrop( coords, dimensions ) {
   2.
          $( 'x1' ).value = coords.x1;
   3.
          $( 'y1' ).value = coords.y1;
   4.
          $( 'x2' ).value = coords.x2;
   5.
          $( 'y2' ).value = coords.y2;
   6.
          $( 'width' ).value = dimensions.width;
   7.
          $( 'height' ).value = dimensions.height;
   8.
      }

Basic interface

This basic example will attach the cropper UI to the test image and return crop results to the provided callback function.
Display code as plain text
HTML:

   1.
      <img src="test.jpg" alt="Test image" id="testImage" width="500" height="333" />
   2.
       
   3.
          <script type="text/javascript" language="javascript">
   4.
          Event.observe( window, 'load', function() {
   5.
              new Cropper.Img(
   6.
                  'testImage',
   7.
                  { onEndCrop: onEndCrop }
   8.
              );
   9.
          } );
  10.
      </script>

Minimum dimensions

You can apply minimum dimensions to a single axis or both, this example applies minimum dimensions to both axis.
Display code as plain text
HTML:

   1.
      <img src="test.jpg" alt="Test image" id="testImage" width="500" height="333" />
   2.
       
   3.
      <script type="text/javascript" language="javascript">
   4.
          Event.observe( window, 'load', function() {
   5.
              new Cropper.Img(
   6.
                  'testImage',
   7.
                  {
   8.
                      minWidth: 220,
   9.
                      minHeight: 120,
  10.
                      onEndCrop: onEndCrop
  11.
                  }
  12.
              );
  13.
          } );
  14.
      </script>

Select area ratio

You can apply a ratio to the selection area, this example applies a 4:3 ratio to the select area.
Display code as plain text
HTML:

   1.
      <img src="test.jpg" alt="Test image" id="testImage" width="500" height="333" />
   2.
       
   3.
      <script type="text/javascript" language="javascript">
   4.
          Event.observe( window, 'load', function() {
   5.
              new Cropper.Img(
   6.
                  'testImage',
   7.
                  {
   8.
                      ratioDim: {
   9.
                          x: 220,
  10.
                          y: 165
  11.
                      },
  12.
                      displayOnInit: true,
  13.
                      onEndCrop: onEndCrop
  14.
                  }
  15.
              );
  16.
          } );
  17.
      </script>

With crop preview

You can display a dynamically prouced preview of the resulting crop by using the ImgWithPreview subclass, a preview can only be displayed when we have a fixed size (set via minWidth & minHeight options). Note that the displayOnInit option is not required as this is the default behaviour when displaying a crop preview.
Display code as plain text
HTML:

   1.
      <img src="test.jpg" alt="Test image" id="testImage" width="500" height="333" />
   2.
      <div id="previewWrap"></div>
   3.
       
   4.
      <script type="text/javascript" language="javascript">
   5.
          Event.observe( window, 'load', function() {
   6.
              new Cropper.ImgWithPreview(
   7.
                  'testImage',
   8.
                  {
   9.
                      previewWrap: 'previewWrap',
  10.
                      minWidth: 120,
  11.
                      minHeight: 120,
  12.
                      ratioDim: { x: 200, y: 120 },
  13.
                      onEndCrop: onEndCrop
  14.
                  }
  15.
              );
  16.
          } );
  17.
      </script>

Known Issues

    * Safari animated gifs, only one of each will animate, this seems to be a known Safari issue.
    * After drawing an area and then clicking to start a new drag in IE 5.5 the rendered height appears as the last height until the user drags, this appears to be the related to another IE error (which has been fixed) where IE does not always redraw the select area properly.
    * Lack of CSS opacity support in Opera before version 9 mean we disable those style rules, if Opera 8 support is important you & you want the overlay to work then you can use the Opera rules in the CSS to apply a black PNG with 50% alpha transparency to replicate the effect.
    * Styling & borders on image, any CSS styling applied directly to the image itself (floats, borders, padding, margin, etc.) will cause problems with the cropper. The use of a wrapper element to apply these styles to is recommended.
    * overflow: auto or overflow: scroll on parent will cause cropper to burst out of parent in IE and Opera when applied (maybe Mac browsers too) I'm not sure why yet.

If you use CakePHP you will notice that including this in your script will break the CSS layout. This is due to the CSS rule

form div{
vertical-align: text-top;
margin-left: 1em;
margin-bottom:2em;
overflow: auto;
}

A simple workaround is to add another rule directly after this like so:

form div.no_cake, form div.no_cake div {
margin:0;
overflow:hidden;
}

and then in your code surround the img tag with a div with the class name of no_cake.

Cheers