views:

104

answers:

2

I'm trying to read the width and height of a locally loaded image. This seems to work for images that do not exceed the dimensions limited by the Flash Player 10 (http://kb2.adobe.com/cps/496/cpsid_49662.html), but as soon as the images are bigger, the width and height remain 0. The strange thing is that now and then, I can read the dimension of these bigger images, but most of the times not. I understand that this might be because of the player limitation, but then I would at least expect the error to be consistent.

I want to check this since there is no use in loading such a big image as it will not be displayed anyway, but it would be good to provide a detailed error message to the user.

Any ideas on this?

Here's the code that I use to load the image locally and read the dimension:

private function chooseImageButton_clickHandler(event:Event):void {
  var allowedTypes:String = "*.jpg;*.png";
  m_uploadFileReference = new FileReference();
  m_uploadFileReference.addEventListener(Event.SELECT, uploadFileReference_selectHandler);
  m_uploadFileReference.addEventListener(Event.COMPLETE, uploadFileReference_completeHandler);
  m_uploadFileReference.browse([new FileFilter("Image Files (" + allowedTypes + ")", allowedTypes)]);
}

private function uploadFileReference_selectHandler(event:Event):void {
  m_uploadFileReference.load();
}

private function uploadFileReference_completeHandler(event:Event):void {
  var loader:Loader = new Loader();
  loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onImageLoaded);
  loader.loadBytes(m_uploadFileReference.data);
}

private function onImageLoaded(e:Event):void {
  trace(e.target.content.width);
}
A: 

I would at least expect the error to be consistent.

Well, at least Adobe are pretty clear on that point: "...if you choose to develop beyond these boundaries we cannot guarantee consistent behavior."

Could you perhaps upload your image to a php pre-processor? (Here's one from Google)

Richard Inglis
Lol, I didn't read that comment. I could upload it, but I want to load it locally so I can prevent having to upload it.
Christophe Herreman
...in that case grapefrukt's answer looks like a good bet.
Richard Inglis
+4  A: 

You can skip loading the entire image and just reading the headers with this class.

var je : JPGSizeExtractor = new JPGSizeExtractor( );
je.addEventListener( JPGSizeExtractor.PARSE_COMPLETE, sizeHandler );
je.extractSize( your_jpg_file.jpg );
 
function sizeHandler( e : Event ) : void {
    trace( "Dimensions: " + je.width + " x " + je.height );
}

Should be both faster and more reliable.

grapefrukt
+1. great class.
back2dos
I found this earlier today but it only works for JPG formats. We need to support some other formats as well like PNG.
Christophe Herreman
+1 very classy! Since PNG files also have an image header with width and height information, perhaps the code could be adapted?
Richard Inglis
I created a similar class for reading the dimensions of a PNG file. http://www.herrodius.com/blog/265
Christophe Herreman
Quick work Christophe - always nice to see my idle suggestions realised through someone else's hard work ;)
Richard Inglis