Details

Version 1.2.2
Last updated 4th July 2011
Requirements
Demo View demo page
Links
License BSD License
Changelog
1.2.2
  • Support for latest versions of Prototype & script.aculo.us (1.7.0 & 1.9.0 respectively)
  • Tested with & added support notice for IE9
1.2.1
  • Added support for latest versions of Prototype & script.aculo.us (1.6.1.0 & 1.8.2 respectively). Changes provided by Tom Hirashima.
  • No-longer package prototype & script.aculo.us with the release
  • Changed tests to use google ajax libraries api to load prototype & script.aculo.us
  • Added option to not auto include the cropper CSS file
  • #00008 - Fixed bug: Dynamic include of cropper CSS expected cropper.js and failed when using cropper.uncompressed.js
  • #00028 - Fixed bug: Doesn't work with latest script.aculo.us - Fix by Tom Hirashima
  • #00030 - Fixed bug: Doesn't work in Firefox 3.5 (CSS include issue)
  • #00007 - Fixed bug: onEndCrop isn't called when moving with keys
  • #00011 - Fixed bug: The image that is to be cropped does not show in IE6.0 -- included CSS fix
  • Tidied up source code & fixed issues that jslint found so it will compress better
1.2.0
  • Added id to the preview image element using 'imgCrop_[originalImageID]'
  • #00001 - Fixed bug: Doesn't account for scroll offsets
  • #00009 - Fixed bug: Placing the cropper inside differently positioned elements causes incorrect co-ordinates and display
  • #00013 - Fixed bug: I-bar cursor appears on drag plane
  • #00014 - Fixed bug: If ID for image tag is not found in document script throws error
  • Fixed bug with drag start co-ordinates if wrapper element has moved in browser (e.g. dragged to a new position)
  • Fixed bug with drag start co-ordinates if image contained in a wrapper with scrolling - this may be buggy if image has other ancestors with scrolling applied (except the body)
  • #00015 - Fixed bug: When cropper removed and then reapplied onEndCrop callback gets called multiple times, solution suggestion from Bill Smith
  • Various speed increases & code cleanup which meant improved performance in Mac - which allowed removal of different overlay methods for IE and all other browsers, which led to a fix for:
  • #00010 - Fixed bug: Select area doesn't adhere to image size when image resized using img attributes
  • #00006 - Removed default behaviour of automatically setting a ratio when both min width & height passed, the ratioDimensions must be passed in
  • #00005 - Added ability to set maximum crop dimensions, if both min & max set as the same value then we'll get a fixed cropper size on the axes as appropriate and the resize handles will not be displayed as appropriate
  • Switched keydown for keypress for moving select area with cursor keys (makes for nicer action) - doesn't appear to work in Safari
1.1.3
  • Fixed wrong cursor on western handle in CSS
  • #00008 & #00003 - Added feature: Allow to set dimensions & position for cropper on load
  • #00002 - Fixed bug: Pressing 'remove cropper' twice removes image in IE
1.1.2
  • Fixed bugs with ratios when GCD is low (patch submitted by Andy Skelton)
1.1.1
  • Fixed bug with rendering issues fix in IE 5.5
  • Fixed bug with endCrop callback issues once cropper had been removed & reset in IE
1.1.0
  • Fixed bug with IE constantly trying to reload select area background image
  • Applied more robust fix to Safari & IE rendering issues
  • Added method to reset parameters - useful for when dynamically changing img the cropper is attached to
  • Added method to remove cropper from image
1.0.0
  • Initial verison

About

The JavaScript image cropper UI allows the user to crop an image using an interface with the same features and styling as found in commercial image editing software, and is is based on the Prototype JavaScript framework and script.aculo.us.

Initially I performed quite a lot of searching for some ready made solutions to meet my requirements, but found none that had the complete feature set that I required or any complete versions based on Prototype.

So after a week and a half of work, I present the JavaScript image cropper UI, built on Prototype & script.aculo.us.

Features

Screen shot of cropper in action

  • Un-obtrusive
  • Based on Prototype and script.aculo.us
  • Image editing package styling & functionality, the crop area functions and looks like those found in popular image editing software
  • Dynamic inclusion of required styles
  • Drag to draw areas
  • Shift drag to draw/resize areas as squares
  • Selection area can be moved
  • Selection area can be resized using resize handles
  • Allows dimension ratio limited crop areas
  • Allows minimum dimension crop areas
  • Allows maximum dimensions crop areas, if both min & max set as the same value then we'll get a fixed cropper size on the axes as appropriate and the resize handles will not be displayed as appropriate
  • Allows dynamic preview of resultant crop (if minimum width & height are provided), this is implemented as a subclass so can be removed if not required
  • Movement of selection area by arrow keys (shift + arrow key will move selection area by 10 pixels)
  • All operations stay within bounds of image
  • All functionality & display compatible with most popular browsers supported by Prototype, tested in:
    • PC: IE 6 & 5.5, Firefox 1.5, Opera 8.5 (see known issues) & 9.0b
    • MAC: Camino 1.0, Firefox 1.5, Safari 2.0

Usage

Extract to a directory of your choosing e.g. 'scripts/cropper/' and include the script and the required Prototype & script.aculo.us scripts:

HTML:
  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=effects,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:

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.

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.

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.

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

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.

Next Steps

Feature Requests & Bug Reports

Please check the existing list of feature requests & bugs and the discussion list before posting requests or reporting bugs.

Leave a Tip

If you find this code useful you can leave a donation towards the continued development & support.

Discussion

Note: Please only use the comments for general comments and the discussion list to discuss this code project (e.g. implementation queries, change suggestions etc.).

Comments

There have been 750 comments so far, join the discussion.

Pages: « 1 2 [3] 4 5 6 7 8 9 10 1115 » Show All

101. Bill Smith - 28th Sep 2006 - 6:35 pm

Dave,

I’ve been playing around with this some and I figured out the onEndCrop problem. It has to do with the calls to Event.observe. The current implementation is passing in anonymous function refs. I found if I did something like the following, then everything cleaned up.

CODE:

  1. DQp0aGlzLnN0YXJ0RHJhZ0JpbmQ9dGhpcy5zdGFydERyYWcuYmluZEFzRXZlbnRMaXN0ZW5lcih0aGlzKTsNCkV2ZW50Lm9ic2VydmUodGhpcy5kcmFnQXJlYSwibW91c2Vkb3duIix0aGlzLnN0YXJ0RHJhZ0JpbmQpOw0K

The other problem I’m having is that I have a routine that changes the cropper to/from a fixed ratio by clicking a checkbox. In Firefox it works fine, in IE, it seems that it takes an extra mouseclick to get the cropper to reset. Here is the function I’m using in my wrapper class. I’m sure there is a better way.

CODE:

  1. DQogIG9uUGFyYW1DaGFuZ2U6IGZ1bmN0aW9uKCkNCiAgew0KICAgIGRlYnVnKCJvbkNoYW5nZSBvY2N1cnJlZCIpOw0KICAgIA0KICAgIHZhciB3aWR0aCA9ICQoJ2Nyb3B3aWR0aCcpLnZhbHVlOw0KICAgIHZhciBoZWlnaHQgPSAkKCdjcm9waGVpZ2h0JykudmFsdWU7DQogICAgdmFyIHByb3AgPSAkKCdjcm9wcHJvcG9ydGlvbicpLmNoZWNrZWQ7DQogICAgDQogICAgaWYgKHByb3ApDQogICAgICB0aGlzLmNyb3BwZXIub3B0aW9ucy5yYXRpb0RpbSA9IHt4OiB3aWR0aCwgeTogaGVpZ2h0fTsNCiAgICBlbHNlDQogICAgICB0aGlzLmNyb3BwZXIub3B0aW9ucy5yYXRpb0RpbSA9IHt4OiAwLCB5OiAwfTsNCg0KICAJaWYgKHRoaXMuY3JvcHBlci5vcHRpb25zLnJhdGlvRGltLnggPiAwICYmIHRoaXMuY3JvcHBlci5vcHRpb25zLnJhdGlvRGltLnkgPiAwKQ0KICAJew0KCSAgCXZhciBnY2QgPSB0aGlzLmNyb3BwZXIuZ2V0R0NEKCB0aGlzLmNyb3BwZXIub3B0aW9ucy5yYXRpb0RpbS54LCB0aGlzLmNyb3BwZXIub3B0aW9ucy5yYXRpb0RpbS55ICk7DQoJCSAgdGhpcy5jcm9wcGVyLnJhdGlvWCA9IHRoaXMuY3JvcHBlci5vcHRpb25zLnJhdGlvRGltLnggLyBnY2Q7DQoJCSAgdGhpcy5jcm9wcGVyLnJhdGlvWSA9IHRoaXMuY3JvcHBlci5vcHRpb25zLnJhdGlvRGltLnkgLyBnY2Q7DQoJICB9DQoJICBlbHNlDQoJICB7DQoJCSAgdGhpcy5jcm9wcGVyLnJhdGlvWCA9IDA7DQoJCSAgdGhpcy5jcm9wcGVyLnJhdGlvWSA9IDA7DQogICAgfQkgIA0KDQogICAgdGhpcy5jcm9wcGVyLnNldFBhcmFtcygpOyAgICANCiAgfQ0K

102. Chris - 30th Sep 2006 - 1:55 am

I’m not very experienced with Javascipt, but I didn’t think I was a tard. Cropper has me thinking otherwise. I have followed the examples very closely and no matter what I do, the img I pass to Cropper is MOVED to the end of my layout (upon loading the page) and RESIZED slightly. Furthermore, while I can see the callback is being triggered, no selection box is created. It also seems as though the active area becomes the whole canvas -the callback is triggered when I click ANYWHERE on my page.

I have gone so far as to remove all my CSS and pull the img out of the enclosing td. No go. Any ideas? Please? please…

-Chris

103. Ben Schwarz - 2nd Oct 2006 - 7:08 am

Due to the way that this library is written you are not able to resize your image and crop it within the same interface.

This is because the image src is written as a background image of a div, sized and positioned accordingly, unfortunatly to my knowledge you cannot resize a background image.

Thanks for the library David, I may end up writing my own implementation of it in order to resize the image in the DOM, I’ll try to remember to post back if I get to it.

104. Dave - 2nd Oct 2006 - 10:43 am

Ben:
The overlays in the croper UI are actually implemented differently for IE and all other browsers. This is because the original implementation was very sluggish in all the Mac browsers and the new implementation caused problems in IE.

However at the weekend I spent some time playing around with the cropper and I have improved the performance of the original implementation so all browsers now use the original implementation. This change fixes bug #00010 and will allow me to implement some other changes, I am now aiming to get the next release out sometime within the next week.

105. Lucas Young - 2nd Oct 2006 - 11:19 am

Hi Dave
Any chance of that div overflow bug being resolved in the next release?

cheers

Lucas

106. Dave - 2nd Oct 2006 - 1:24 pm

Lucas:
Yes the overflow bug, plus the other bugs due to different CSS positioning of the parent element(s) have been fixed.

107. Chris - 2nd Oct 2006 - 8:17 pm

I think I found the problem I was having earlier. Cropper does not do well if the image is less than 100% of the size of the enclosing div and right aligned. I had my image in a floating div of 50% of the canvas, and Cropper got sick when the image was a small one taking up less than the full div.

Dave: if you are working on updates, please consider this case.

Also, please consider parameterizing the location of the cropper.css file instead of assuming it is in the same directory as cropper.js.

108. max - 5th Oct 2006 - 8:35 am

Mike Burrell, I resolved this problem!
Replace string:
imagecopyresampled($thumb, $big, 0, 0, $x1, $y1, $x2, $y2, $ow, $oh);
with
imagecopyresampled($thumb, $big, 0, 0, $x1, $y1, $width*2, $y2, $ow, $oh);

Enjoy!

109. Ravikiran - 9th Oct 2006 - 5:18 am

hi,

i couldn’t find setParamsAfterZoom() in crooper.js file. Actually my requirement is we placed image in div, that div in a table. i am unable to get correct cropping area and if i scroll div down, i am unable to do select the area where scrolled area of div.

can u let me know how to solve this problem.

Thanking u,
Ravikiran

110. Ravikiran - 9th Oct 2006 - 8:10 am

your’s example test1.html is not working well, after image got small size the cropping is not working well and also after image zooming the cropping is not working will. plz fix the bugs , bcoz i want the test1 solution in my project.

111. Dave - 9th Oct 2006 - 9:11 am

Ravikiran:
The test1.html should not have been included as part of the release and I will ensure it is removed in the next release.

As for the functionality this test page provides (zooming) it is not something that I currently have as part of the official release as I’ve been concentrating on fixing the major bugs for now. The test1.html file was submitted by someone else but is quite buggy in it’s implementation. I do intend to add zooming at some point but will start from scratch with that functionality once I’ve cleared out all the major bugs.

112. Dave Marks - 13th Oct 2006 - 1:39 pm

This cropper is absolutly brilliant – far better than anything else I’ve seen!

A feature that would be nice – rotating…

Flipping between portrait and landscape – this can be done already by submitting the current crop area and ratio to a dynamic page which does a bit of maths and then starts the crop again with set postions and reversed ration but it would be nice if this could be handled by js

Rotating by an arbitary degree would also be nice but not sure if this at all possible…

Thanks! Keep up the good work!

Oh and Multiple monitors are aweseom – check out multimon at http://www.realtimesoft.com – i couldn’t live without it on multi monitor setups!

113. Andrea - 17th Oct 2006 - 2:39 pm

I was looking for a nice way to build avatars for my site users, your solutions looks amazing, can’t wait to implement! The preview box is damn good too! :)

114. Justin - 19th Oct 2006 - 11:48 am

$t1 = $ow / $width * ‘120’;
$t2 = $oh / $height * ‘160’;

imagecopyresampled($thumb, $big, 0, 0, $x1, $y1, $t1, $t2, $ow, $oh);

This is a fix for that PHP script that mike made… the fix that max posted didnt work for me. I am using a fixed size for my output of 120×160 so replace the 120 and 160 with what you are using. Also my source image was the same ratio as my new one, i haven’t tested for stretching if the ratio is different but i would guess it to be an issue… Thanks for the great tool!

115. Frank - 21st Oct 2006 - 3:58 am

How do you actually do something to the image instead of just selecting certain areas?

116. Geartjan Meijer - 22nd Oct 2006 - 12:53 pm

This works fine

117. Geartjan Meijer - 22nd Oct 2006 - 12:55 pm

I mean:
$file = “test.jpg”;
header(‘Content-type: image/jpeg’);

$x1 = $_POST[‘x1’];
$y1 = $_POST[‘y1’];
$width = $_POST[‘width’];
$height = $_POST[‘height’];

list($ow, $oh) = getimagesize($file);

$image = imagecreatefromjpeg($file);
$thumb = imagecreatetruecolor($_POST[‘width’],$_POST[‘height’]);

imagecopyresized($thumb,$image,0,0,$x1,$y1,$width,$height,$width,$height);
imagejpeg($thumb, null,100);

imagedestroy($image);
imagedestroy($thumb);

118. David Kingston - 23rd Oct 2006 - 10:17 am

I love the interface. By far the best I have come across. But has anyone developed the basic version with PHP to load $image then output the cropped image result to a specified directory?

119. Andrea - 23rd Oct 2006 - 3:13 pm

In post 117, at the line
imagejpeg($thumb, null,100);
just replace “null” with a path+filename

120. Jason - 23rd Oct 2006 - 7:53 pm

Awesome tool, but I was wondering…is there an easy way for me to clear the crop markers and the translucent overlay after I’ve finished doing what I need to do with the cropping?

121. Jason - 23rd Oct 2006 - 7:56 pm

Dear me, I just saw the demo and that there is already a way to remove the cropper. I swear I didn’t see that before!!!

Thanks much.

122. Chad - 24th Oct 2006 - 9:19 pm

Using the PHP code from post 21 I changed:

imagecopyresampled($thumb, $big, 0, 0, $x1, $y1, $x2, $y2, $ow, $oh);
to
imagecopyresampled($thumb, $big, 0, 0, $x1, $y1, $x2, $y2, $x2, $y2);

A typical image crop results in an image with new content (the selection) and a new width/height (the width and height of the selection). The original PHP code would stretch the crop to be as large as the original image and acted more like a ‘zoom’ effect. Changing $ow to $x2 and $oh to $y2 will create the new images’ width/height based off the selection which is atleast what I was looking for.

Thanks for the great script, it was exactly what I was looking for.

Chad

123. Chad - 25th Oct 2006 - 4:12 pm

Whoops:

imagecopyresampled($thumb, $big, 0, 0, $x1, $y1, $x2, $y2, $x2-$x1, $y2-$y1);

124. Jakob - 26th Oct 2006 - 8:28 am

Hello.

First off, this is a GREAT piece of code, and I’ve already had great use for it. Many thanks!

I just have a few questions:

1) I’ve had some problems with Internet Explorer timing out on crop requests, but not with anything else on the site. Could this be a problem with the Javascript?

2) Is there a way to change the size of the crop area by putting/changing the values in text boxes, without refreshing the page?

Again, thanks.

125. Mike - 26th Oct 2006 - 11:41 pm

Hi ,
Is it possible to have more than one cropper on a page?

Thank You

Mike

126. John Walter - 30th Oct 2006 - 7:30 pm

David—
Thanks so much for posting a tool that hundreds of people have shown a need for. That’s a very generous gift of your talent…

There seems to be a limit of 640×480 for the images to be manipulated. Larger than that the auto-highlight-within-shadow turns off (at least in Mozilla 2). Is that a scriptaculous limitation?
—John

127. Dave - 30th Oct 2006 - 8:55 pm

Firstly thankyou all for your kind comments, secondly sorry for not replying sooner to you… a lot has been going on, will you accept version 1.2.0 of the cropper as an apology?

Starting in reverse chronolgical order:
Dave Marks:
Rotating would be nice, but HTML doesn’t allow that, try doing it server side as per what it sounds like you’re doing with the flipping.

Andrea:
Thanks for the kind words.

Frank:
The image cropper UI is just the UI for gathering the cropping co-ordinates, the crop itself would have to be performed server side with your chosen language; see some of the other comments for PHP examples.

David Kingston:
As per Frank, there has been some discussion in these comments, if anyone wants to provide a solution or example file I’d be happy to post it and provide appropriate credits etc.

Jakob:
Do you mean when you submit an image to be cropped by some server-side script? If so then that’s nothing to do with the cropper UI, if not – then I have no idea, do you have an example URL?

Mike:
It is possible to have more than one cropper on a page, I had a test page running with about 5 but they did slow down a little.

John:
I don’t know of an issue with an image size, but the way the cropper is implemented in Firefox (and all browsers except IE) has changed in the latest version, so I suggest giving that a try and let me know if you’re still experiencing problems.

Once again, thank you all for your kind comments.

-D

128. Lucas Young - 30th Oct 2006 - 10:38 pm

Hi Dave, awesome work, it’s great in FF now.
I see the overflow: auto problem in IE is still there, what do you think might be causing that?
cheers

Lucas

129. DJ - 6th Nov 2006 - 8:11 am

Hi, can anyone provide me with the PHP code using this javascript GUI.. I saw the code here in PHP, but I don’t know how to implement it :D Like how do you past the variables from js to PHP?

email mastahswabe at gmail.com

130. Justin - 6th Nov 2006 - 2:06 pm

DJ, just put form tags around those input fields from the cropper and put a submit button so that you can send them to the PHP page. After you get that working you can set them to hidden or whatever… Then for the PHP page use $_POST[‘var’], or $_GET[‘var’] for each of the variables depending on what method you chose for the form.

Thanks for the new version David!

Justin

131. Dale - 7th Nov 2006 - 8:51 pm

E.X.C.E.L.L.E.N.T.
thank you very much for this awesome piece of work

132. Hessner - 9th Nov 2006 - 12:17 am

I used the “good old”:

But it turned out that the lines below are very important for the cropper to work correct:

(my picture disappeared now and again)

Just a tip :-)

133. Hessner - 9th Nov 2006 - 12:21 am

Ups, here are the lines:

“Bad”:
!DOCTYPE HTML PUBLIC ”-//W3C//DTD HTML 4.0 Transitional//EN”

“Good”:
!DOCTYPE HTML PUBLIC ”-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”

134. Ben Kittrell - 13th Nov 2006 - 6:51 pm

Very cool, Thank you very much.

I was able to get this working easily with Rails.

135. Me - 15th Nov 2006 - 5:30 pm

that’s great!
script works perfectly but what about loading the page WITH MY coordinates of cropping box?

(i want to save a crop result for future demonstration when page is opened again)

thank you and wish a lot of success

136. Me - 15th Nov 2006 - 5:45 pm

seems like I’am dumb
no question at all ;)
thank you again

137. Ant - 3rd Dec 2006 - 10:16 pm

Awesome work Guys!!

I was wondering if there is a way to add snapping to the selection area. As in as the user selects the desired area, it snaps to either 10 pixel increments or some other predefined value.

again, awesome work. Best of the web as far as image tools I’ve seen.

138. jurciks - 5th Dec 2006 - 10:34 am

I guess you have a small glych with roundings (ImgWithPreview).
Example.
I add image 800×600 and specify width 500px (to fit in page), and use JS – new Cropper.ImgWithPreview(‘testImage’, {
previewWrap: ‘previewWrap’,
minWidth: 175,
minHeight: 140,
ratioDim: {
x: 750,
y: 600},
onEndCrop: onEndCrop
});
(I need to resize with ratio 1.25)
Then I get new coords :

[x1] => 32
[y1] => 0
[x2] => 500
[y2] => 375
[w] => 468
[h] => 375
But actually x1 should be 31.25 or rounded to 31.
Besides this, preview area is displayed 187×140 but it should be 175×140

139. Luis Bolson - 7th Dec 2006 - 1:02 pm

Hi. First of all, Great Work! People like you make the web easier.

About question #125, how can I put multiple croppers in the same page?

I’ve tested with two images, it works fine, except by the fact that one of them actually is not calling the callback function, which is the same for both images. Only the last image calls it.

Thank you.

140. John Aldridge - 14th Dec 2006 - 3:16 pm

Just made a quick function for PHP to crop a stored image and save it to a new path. Might be helpfull, hopefully!

HTML:

  1. function CropImage($imagePath, $sizes) {
  2.     $originalPath = $imagePath; // location of image to be cropped
  3.     $newPath = "/new/".$imagePath; // where to put cropped image
  4.     @unlink($newPath); // unlink destination image if possible
  5.     $originalImage = imagecreatefromjpeg($originalPath); // create image from original
  6.     $thumbImage = imagecreatetruecolor($sizes[‘width’], $sizes[‘height’]); // create thumbnail image
  7.     // perform crop (modified from code posted here, eg. this one doesn’t stretch the image)
  8.     imagecopyresampled($thumbImage, $originalImage, 0, 0, $sizes[‘x1’], $sizes[‘y1’], $sizes[‘width’], $sizes[‘height’], $sizes[‘x2’]-$sizes[‘x1’], $sizes[‘y2’]-$sizes[‘y1’]);
  9.     imagedestroy($originalImage); // destroy image resource
  10.     imagejpeg($thumbImage, $newPath, 100); // save thumbnail to newPath
  11.     imagedestroy($thumbImage); // destroy thumbnail resource
  12. }
  13.  
  14. /* Usage:
  15. $sizes[‘x1’] = $_POST[‘x1’];
  16. $sizes[‘y1’] = $_POST[‘y1’];
  17. $sizes[‘x2’] = $_POST[‘x2’];
  18. $sizes[‘y2’] = $_POST[‘y2’];
  19. $sizes[‘width’] = $_POST[‘width’];
  20. $sizes[‘height’] = $_POST[‘height’];
  21. CropImage("/path/to/image.jpg", $sizes);
  22. */

141. Rainer Schleevoigt - 15th Dec 2006 - 7:52 pm

Excellent!
here is my realisation in combination with ‘convert -crop’ from Imagemagick.
http://familientagebuch.de/rainer/2006/50.html#5
I looking for a solution to generate USEMAPS.

Rainer

142. pablitobs - 19th Dec 2006 - 10:41 am

for those who beside jpg also use PNG or gif this a modified version of some code posted here, this version overrides the original picture.

function CropImage($originalImage, $sizes, $imagetype) {
switch ( $imagetype) {
case “image/gif”:
$src = imageCreateFromGIF($originalImage);
break;
case “image/pjpeg” :
case “image/jpeg” :
$src = imageCreateFromJPEG($originalImage);
break;
case “image/x-PNG”:
case “image/png”:
$src = imageCreateFromPNG($originalImage);
break;
}

$newPath = $originalImage;
$thumbImage = imagecreatetruecolor($sizes[‘width’], $sizes[‘height’]); // create thumbnail image
@unlink($imagePath); // unlink destination image if possible

// perform crop (modified from code posted here, eg. this one doesn’t stretch the image)
imagecopyresampled($thumbImage, $src, 0, 0, $sizes[‘x1’], $sizes[‘y1’], $sizes[‘width’], $sizes[‘height’], $sizes[‘x2’]- $sizes[‘x1’], $sizes[‘y2’]- $sizes[‘y1’]);

imagedestroy($src); // destroy image resource
unlink($newPath);
if($imagetype "image/jpeg" || $imagetype “image/pjpeg”){
imagejpeg($thumbImage,$newPath,100);
}
if($imagetype "image/gif"){
imagegif($thumbImage,$newPath);
}
if($imagetype “image/png” || $imagetype == “image/x-PNG”){
imagepng($thumbImage,$newPath);
}

imagedestroy($thumbImage); // destroy thumbnail resource
}

143. djkaat - 21st Dec 2006 - 3:24 pm

Hi

Very nice cropper.
Is there a way I can make the crop area resize keeping the center of the area the same as the center of the image?

Thanks

144. Mikkel - 30th Dec 2006 - 3:19 pm

Hi

Great script, but how do you reset/remove the crop tool?

What function should i call when i use a button onclick=”” ?

Best regards, Mikkel

145. Dave - 31st Dec 2006 - 3:31 pm

djkaat:
That would be a custom piece of functionality, I’m pretty sure it would be fairly easy to fix the crop area to the centre of the image – but it’s not something I’m currently thinking of adding as a main feature.

Mikkel:
There are two public methods available via the API named remove() and reset() which remove or reset the cropper, note however the ImgWithPreview class does not implement these methods at the moment.

146. Adam B - 8th Jan 2007 - 8:23 pm

here’s the PHP code, someone posted some half working code that distorted the images
this uses the castle demo image but will work with jpg or PNG.

cheers
-Adam

147. Adam B - 8th Jan 2007 - 8:25 pm

here’s the PHP code, someone posted some half working code that distorted the images
this uses the castle demo image but will work with jpg or PNG.

$x1 = $_POST[‘x1’];
$y1 = $_POST[‘y1’];
$x2 = $_POST[‘x2’];
$y2 = $_POST[‘y2’];
$width = $_POST[‘width’];
$height = $_POST[‘height’];

//this must be the same as our basic aspect ratio used on the javascript, modify the js and this to make that dynamic.
$thumb_width = 200;
$thumb_height = 120;

// define image filename
$image_filename = “jscripts/cropper/tests/castle.jpg”;

// find out original width / height
list($ow, $oh) = getimagesize($image_filename);

// setup orig photo
$big = imagecreatefromjpeg($image_filename);
// setup cropped photo
$thumb = imagecreatetruecolor($thumb_width,$thumb_height);
imagecopyresampled($thumb, $big, 0, 0, $x1, $y1, 200, 120, $width, $height);

$imgname = “test1a.jpg”;

// save image to disk
imagejpeg($thumb, ’../photo_album/’.$imgname,100);

// free up resources
imagedestroy($big);
imagedestroy($thumb);

(repost. apaprently the comment system doesn’t like PHP code…)

148. bjarke - 9th Jan 2007 - 2:18 pm

“31st May 2006 – 7:28 pm” you wrote:

“It is possible to apply a new cropper to a new image (or the same image but replaced) but there are a couple of errors that are related to the fact that the HTML that was added to the DOM for the cropper isn’t been removed.”

When I try to remove a cropper, and attach a new cropper to a new picture, it looks strange. What can I do?

Please help.

149. Dave - 9th Jan 2007 - 2:21 pm

bjarke:

Since then the API has been updated to add a couple of new methods to allow this and you should use those.

150. bjarke - 9th Jan 2007 - 2:37 pm

When I try to remove a cropper attached to a picture, and then attach a new cropper to the same picture, it looks funny. You can’t se the picture, only a grey box where the picture should be.

Everything else works as a charm :)

what to do??

Pages:« 1 2 [3] 4 5 6 7 8 9 10 1115 » Show All

Leave a comment

No HTML please, only textile. For code please use [lang]...[/lang] tags (e.g. [html]...[/html] for HTML)