привел в порядок свою реализацию. В архиве есть исходники с примером использования. Критика и предложения по улучшению приветствуются
что ли код показать (actionscript)...
import com.adobe.images.JPGEncoder; // [url]https://github.com/mikechambers/as3corelib[/url]
import ImageUtils;
import flash.external.ExternalInterface;
ExternalInterface.addCallback("load", load);
ExternalInterface.addCallback("crop", crop);
ExternalInterface.addCallback("resize", resize);
ExternalInterface.addCallback("upload", upload);
if( this.loaderInfo.parameters['onReady'] )
ExternalInterface.call( this.loaderInfo.parameters['onReady'] );
var img;
function load( url, onSuccess, onFailure ){
loadResource( url, {'onSuccess': _onSuccess, 'onFailure': _onFailure});
function _onSuccess( ldr ){
img = Bitmap(ldr.content).bitmapData;
ExternalInterface.call(onSuccess);
}
function _onFailure(){
ExternalInterface.call(onFailure, 'load-error');
}
}
function loadResource( url, options ){
var req = new URLRequest(url);
var ldr = new Loader();
if( options['onSuccess'] )
ldr.contentLoaderInfo.addEventListener( Event.COMPLETE, onComplete );
if( options['onFailure'] )
ldr.contentLoaderInfo.addEventListener( IOErrorEvent.IO_ERROR, options['onFailure'] );
ldr.load( req );
function onComplete(){
options['onSuccess']( ldr );
}
}
function crop( x, y, width, height ){
img = ImageUtils.crop(img, x, y, width, height);
}
function resize( width ){
img = ImageUtils.resize(img, width);
}
function upload( url, quality, onSuccess, onFailure ){
var encoder = new JPGEncoder(quality);
img = encoder.encode(img);
sendData( url, {
'content-type': 'application/octet-stream',
'method': URLRequestMethod.POST,
'data': img,
'onSuccess': _onSuccess,
'onFailure': _onFailure
});
function _onSuccess(){
ExternalInterface.call( onSuccess );
}
function _onFailure(){
ExternalInterface.call( onFailure, 'send-error' );
}
}
function sendData( url, options ){
var req = new URLRequest(url);
if( options['content-type'] ){
var header = new URLRequestHeader('Content-Type', options['content-type']);
req.requestHeaders.push( header );
}
if( options['method'] )
req.method = options['method'];
if( options['data'] )
req.data = options['data'];
var ldr = new URLLoader();
if( options['onSuccess'] )
ldr.addEventListener(Event.COMPLETE, onComplete);
if( options['onFailure'] )
ldr.addEventListener(IOErrorEvent.IO_ERROR, options['onFailure']);
ldr.load( req );
function onComplete(){
if( ldr.data.length ){
if( options['onFailure'] )
options['onFailure']();
} else
options['onSuccess']();
}
}
ImageUtils:
package {
import com.adobe.images.JPGEncoder;
import flash.display.BitmapData;
import flash.geom.Matrix;
public class ImageUtils {
static const _IDEAL_RESIZE_PERCENT = 0.5;
static function resize( image, width ){
var dstScale = width/image.width;
var firstScale = ImageUtils._getFirstScale(dstScale);
image = ImageUtils._resize1(image, firstScale);
var step = ImageUtils._IDEAL_RESIZE_PERCENT;
for( var s=firstScale*step; s>=dstScale; s*=step )
image = ImageUtils._resize1(image, step);
return image;
}
static function _getFirstScale( dstScale ){
var r = dstScale;
while( r < 1 )
r /= ImageUtils._IDEAL_RESIZE_PERCENT;
if( dstScale < ImageUtils._IDEAL_RESIZE_PERCENT )
r *= ImageUtils._IDEAL_RESIZE_PERCENT;
return r;
}
static function _resize1( image, scale ){
var w = Math.round(image.width*scale);
var h = Math.round(image.height*scale);
var r = new BitmapData(w, h);
var matrix = new Matrix(scale, 0, 0, scale);
r.draw( image, matrix, null, null, null, true);
return r;
}
static function crop( image, x, y, width, height ){
var r = new BitmapData(width, height);
var matrix = new Matrix(1, 0, 0, 1, -x, -y);
r.draw( image, matrix, null, null, null, true );
return r;
}
}
}