Developer Network Home - Help

Yahoo! UI Library: ImageCropper

ImageCropper

The ImageCropper Control gives you an interactive interface for getting the dimensions to crop an image.

Note: The ImageCropper control is being released as a beta component. Please refer to the FAQ for what we mean by this designation. We look forward to your feedback in the YUI Forums.

Getting Started

To use the ImageCropper Control, include the following source files in your web page:

YUI dependency configurator.

YUI Dependency Configurator:

Instead of copying and pasting the filepaths above, try letting the YUI dependency Configurator determine the optimal file list for your desired components; the Configurator uses YUI Loader write out the full HTML for including the precise files you need for your implementation.

Note: If you wish to include this component via the YUI Loader, its module name is imagecropper. (Click here for the full list of module names for YUI Loader.)

Where these files come from: The files included using the text above will be served from Yahoo! servers; see "Serving YUI Files from Yahoo!" for important information about this service. JavaScript files are minified, meaning that comments and white space have been removed to make them more efficient to download. To use the full, commented versions or the -debug versions of YUI JavaScript files, please download the library distribution and host the files on your own server.

Order matters: As is the case generally with JavaScript and CSS, order matters; these files should be included in the order specified above. If you include files in the wrong order, errors may result.

Setting up an ImageCropper

Setting up an ImageCropper Control is as easy as this:

Using the above code will get you a rich control that looks something like this:

Configuring an ImageCropper

The ImageCropper Control is based on the Resize Utility and it inherits some of it's configuration options.

Name Type Default Description
initialXY Array [10, 10] Array of the XY position that we need to set the crop element to when we build it.
keyTick Number 1 The pixel tick for the arrow keys.
shiftKeyTick Number 10 The pixel tick for shift + the arrow keys.
useKeys Boolean true Should we use the Arrow keys to position the crop element.
status Boolean true Show the Resize Utility status
minHeight Number 50 MinHeight of the crop area
minWidth Number 50 MinWidth of the crop area
ratio Boolean false Set the ratio config option of the Resize Utlility
initHeight Number 1/4 of the image height Set the initlal height of the crop area
initWidth Number 1/4 of the image width Set the initlal width of the crop area

ImageCropper Events

Here is a list of the Custom Events that the ImageCropper Control fires:

Name Payload Description
dragEvent MouseMove Event When the DragDrop dragEvent is fired for the config option draggable.
startResizeEvent Resize Object When when a resize action is started.
beforeResize Height, Width, Top, Left Fires before every element resize after the size calculations, returning false will stop the resize.
resizeEvent Height, Width, Top, Left On every element resize (only fires once when used with proxy config setting).
moveEvent Height, Width, Top, Left Fires on every element move. Inside these methods: _handleKeyPress, _handleDragEvent, _handleResizeEvent

Getting the crop area

Now that we have an interface, we need to get the crop data from the ImageCropper Control. We can do this with the getCropCoords method, like this:

Now that we have the top, left, height & width of the crop reagion, here is some sample PHP code for cropping that image based on the data from the ImageCropper Control.

Take a look at the ImageCropper examples section for more details..

Skinning ImageCropper

The ImageCropper Control comes with a default presentation or "skin," part of the "Sam Skin" visual treatment that accompanies most YUI controls. You can read more about the general approach to skinning YUI components in this in-depth article.

The CSS provided with the ImageCropper Control is comprised of core, functional CSS as well as the Sam Skin visual treatment.

To explore the CSS which controls the ImageCropper's presentation, please review the ImageCropper's Skinning Example wherein the full CSS for the control is displayed.

Known Issues in 2.5.1

Currently the ImageCropper Control doesn't support scaled images. All images must be used at full scale.

YUI on Mobile: Using ImageCropper with "A-Grade" Mobile Browsers

About this Section: YUI generally works well with mobile browsers that are based on A-Grade browser foundations. For example, Nokia's N-series phones, including the N95, use a browser based on Webkit — the same foundation shared by Apple's Safari browser, which is found on the iPhone. The fundamental challenges in developing for this emerging class of full, A-Grade-derived browsers on handheld devices are:

  • Screen size: You have a much smaller canvas;
  • Input devices: Mobile devices generally do not have mouse input, and therefore are missing some or all mouse events (like mouseover);
  • Processor power: Mobile devices have slower processors that can more easily be saturated by JavaScript and DOM interactions — and processor usage affects things like battery life in ways that don't have analogues in desktop browsers;
  • Latency: Most mobile devices have a much higher latency on the network than do terrestrially networked PCs; this can make pages with many script, css or other types of external files load much more slowly.

There are other considerations, many of them device/browser specific (for example, current versions of the iPhone's Safari browser do not support Flash). The goal of these sections on YUI User's Guides is to provide you some preliminary insights about how specific components perform on this emerging class of mobile devices. Although we have not done exhaustive testing, and although these browsers are revving quickly and present a moving target, our goal is to provide some early, provisional advice to help you get started as you contemplate how your YUI-based application will render in the mobile world.

More Information:

The YUI ImageCropper Control does not currently support the iPhone or the Nokia N95. Due to the interaction model (specifically, the inability to drag and drop), the ImageCropper Control will not function as expected.

Support & Community

The YUI Library and related topics are discussed on the on the ydn-javascript mailing list.

In addition, please visit the YUIBlog for updates and articles about the YUI Library written by the library's developers.

Filing Bugs & Feature Requests

The YUI Library's public bug tracking and feature request repositories are located on the YUI SourceForge project site. Before filing new feature requests or bug reports, please review our reporting guidelines.

Copyright © 2008 Yahoo! Inc. All rights reserved.

Privacy Policy - Terms of Service - Copyright Policy - Job Openings