A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: [RESOLVED] Loading a PHP Generated image.

  1. #1
    Lunatic
    Join Date
    Nov 2002
    Location
    AS3 Forum
    Posts
    342

    resolved [RESOLVED] Loading a PHP Generated image.

    I'm using a PHP script to generate a thumb dynamically and then using AS3 to retrive the thumb.

    Flash is not throwing any errors but it's never firing the complete event for some reason.

    What's going wrong here?

    Code:
    		private function buildThumbnail():void
    		{
    			container.graphics.beginFill(0x000000);
    			container.graphics.drawRect(0, 0, paramW, paramH);
    			container.graphics.endFill();
    			container.alpha = .5;
    			addChild(container);
    						
    			thumbLoading = new cubesAni();
    			thumbLoading.x = (paramW - thumbLoading.width) * 0.5;
    			thumbLoading.y = (paramH - thumbLoading.height) * 0.5;
    			addChild(thumbLoading);
    			
    			var thumbParameters:String = "src=" + paramP + "&w=190&h=190&zc=1";
    						
    			var thumbVariables:URLVariables = new URLVariables(thumbParameters);
    			thumbVariables.dataFormat = URLLoaderDataFormat.TEXT;
    			
    			var thumbReq:URLRequest = new URLRequest("http://www.mmphotography.ca/content/phpThumb.php");
    			thumbReq.method = URLRequestMethod.POST;
    			thumbReq.data = thumbVariables;
    						
    			loader = new Loader();
    			loader.addEventListener(Event.COMPLETE, displayThumb);
    			loader.load(thumbReq);
    		}
    		
    	
    		private function displayThumb(e:Event):void
    		{
    			trace("yo");
    			
    			var pic:Bitmap = new Bitmap(e.currentTarget.content);
    			pic.smoothing = true;
    			
    			thumb = new MovieClip();
    			thumb.addChild(pic);
    			thumb.x = -10;
    			thumb.y = -10;			
    			thumb.scaleX = thumb.scaleY = 1;
    			
    			if ((thumb.width / thumb.height) > 1)
    			{
    				thumb.height = paramH + 20;
    				thumb.scaleX = thumb.scaleY;
    			} else {
    				thumb.width = paramW + 20;
    				thumb.scaleY = thumb.scaleX;
    			}
    			
    			addChild(thumb);
    			
    			thumbMask.graphics.beginFill(0x000000);
    			thumbMask.graphics.drawRect(0, 0, paramW, paramH);
    			thumbMask.graphics.endFill();
    			addChild(thumbMask);
    			
    			thumbMask.cacheAsBitmap = true;
    			
    			thumb.mask(thumbMask);
    			
    			whiteFlash.graphics.beginFill(0xFFFFFF);
    			whiteFlash.graphics.drawRect(0, 0, paramW, paramH);
    			whiteFlash.graphics.endFill();
    			addChild(whiteFlash);
    			
    			removeChild(thumbLoading);
    			thumbLoading = null;
    			
    			whiteFade = new Tween(whiteFlash, "alpha", Strong.easeIn, 1, 0, 200, false);
    			
    			addEventListener(MouseEvent.MOUSE_OVER, hOver);
    			addEventListener(MouseEvent.MOUSE_OUT, hOut);
    
    			loader.removeEventListener(Event.COMPLETE, displayThumb);
    			loader = null;
    		}
    It never traces "yo".

    Here's an example of how the script is called:

    http://www.mmphotography.ca/content/...190&w=190&zc=1

    You can actually try it, it works in the browser and will return a picture for a thumb.

    Tried using the URLLoader class instead of Loader but it didn't work either. Any ideas?

  2. #2
    Lunatic
    Join Date
    Nov 2002
    Location
    AS3 Forum
    Posts
    342
    OK, just made it a little further.

    Used URLLoader class instead.

    Now the Bitmap class doesn't recognize the "PNG" format returned. I'm getting the following error:


    TypeError: Error #1034: Type Coercion failed: cannot convert "‰PNG

    
    to flash.display.DisplayObject.
    at com.berubegraphics.martinemartell::Thumbnail/displayThumb()
    at flash.events::EventDispatcher/dispatchEventFunction()
    at flash.events::EventDispatcher/dispatchEvent()
    at flash.net::URLLoader/onComplete()


    (there's 2 square characters in that error but they don't display in this post for some reason. I'm assuming they're just part of the header of the picture file)

  3. #3
    Lunatic
    Join Date
    Nov 2002
    Location
    AS3 Forum
    Posts
    342
    So after digging around the net, this is the final solution. I had to use URLLoader to get the data in Binary format, then pass it to a Loader class object using loadBytes. Then convert loadBytes into BitmapData, and then BitmapData into bitmap... This is the relevant part of code:

    Code:
    			var thumbParameters:String = "src=" + paramP + "&w=190&h=190&zc=1";
    						
    			var thumbVariables:URLVariables = new URLVariables(thumbParameters);
    			
    			var thumbReq:URLRequest = new URLRequest("http://www.mmphotography.ca/content/phpThumb.php");
    			thumbReq.method = URLRequestMethod.GET;
    			thumbReq.data = thumbVariables;
    						
    			loader = new URLLoader();
    			loader.dataFormat = URLLoaderDataFormat.BINARY;
    			loader.addEventListener(Event.COMPLETE, getBytes);
    			loader.load(thumbReq);
    		}
    		
    		private function getBytes(e:Event):void
    		{
    			var byteLoader:Loader = new Loader();
    			byteLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, displayThumb);
    			byteLoader.loadBytes(e.target.data);
    		}
    		
    	
    		private function displayThumb(e:Event):void
    		{
    			var picData:BitmapData = Bitmap(e.target.content).bitmapData;
    			var pic:Bitmap = new Bitmap(picData);
    			pic.smoothing = true;

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center