Feb 23, 2009

File manipulation client side with the Flash player with Flex 4 and Coldfusion

Ever wanted to load an image into your web application instantly? Well now you can. Well you have been able to for a little while actually, it was introduced with Flash player 10 and Mick Chambers has a good article on getting started with reading and writing files from withing the Flash player.

What you need for the demo: Flash 10 and Windows.
What I needed: Coldfusion ( I'll assume the same can be done in .NET/PHP ect ), the updates to Flexbuilder 3.3 SDK or Flex 4 ( I'm using Flex 4 Gumbo Max pre beta edition ) and that's it.

I don't know what the issue is but I just can't get the file.load() methods to work on OSX or my Ubuntu partition. If you know why or how to fix this please let me know.

This code should be reasonably easy to follow but here is a quick break down.

Using the new Flash file reference methods load a file into the Flash player as a byteArray ( binary ). At this stage my demo is only JPG but the AS3 core library also has a PNG library. I set the image data to the byteArray so we can preview the size of the image and how it will look once its been converted.
Clicking the resize button converts the byteArray to bitMapData and based on the value of the HSlider, work out a scaling factor and send this to the Matrix.

Then create a new bitMapData and draw the old data with the new Matrix transform. There are many additional things that can be applied to the data check out the references for the rest.

Finally convert the bitMapData back to JPG format and re-assign that to the data property of the image.

Once it's converted you can save it back to your local file system without the server ever seeing the data or knowing what file you loaded. It has no performance impact on the server and uses no bandwidth or traffic.

As the result is binary as a byteArray we can also send this back to Coldfusion and save it directly as a .jpg The nice thing is that the image sent back to the server is the new size and simply needs to be written to disk as we all know that server side image resizing/manipulation can be resource heavy.

Application?
OK it's a nice feature but what can I use it for? Say you have a real-estate web site and you have many agents needing to upload images. Now they can do all the resizing/manipulation locally which in general is going to be reducing ( but could be enlarging ) the size of the images within the application removing the need for either a thick client side app ( Photoshop/Fireworks/Paint etc ) or the server side manipulation saving traffic, server load and upload time.

Source view: Source View

Full screen app: Full Screen Resize



Sources used:
Mike Chambers Blog
AS3 core library
Andres Alderson
Adobe Flex RFeferences
House of Fusion Flex mailing list

I like having a working demo that you can try for yourself saving back to my server is enabled and you can browse to the saved images. I clean this up every few hours with a scheduled task.

I don't yet have a need for this in our current applications...yet but I think it can be very usefull. Sure the code needs some work and I'd like to work out why it's not working in OSX/*nix but I think its a good proof of concept/starting point.

Be aware that trying to resize a large/high quality image will impact browser performance for the duration of the conversion. Also there is no error checking if you try load a non jpg into the browser its not going to work. And while the controls are a little rough they still give a good indication of what can be accomplished.

Feb 19, 2009

Coldfusion and Flex resources

Lately I have seen a few posts about Coldfusion and Flex references so I wanted to put together a list of my key reference places. Its 90% CF and Flex but there are some others there. If there is something ( especially podcasts ) that I am missing let me know.
General:
http://www.carehart.org/cf411
Screen casts
http://www.meetup.com/coldfusionmeetup
http://ria.meetup.com/10/
http://recordings.coldfusionmeetup.com
Pod Casts
http://www.cfconversations.com
http://www.cfhour.com
http://www.ukcfug.org/archives.cfm/category/podcast
http://www.theflexshow.com/blog
http://www.boagworld.com
http://www.dotnetrocks.com
http://www.webdevradio.com
Mailing lists
http://www.houseoffusion.com ( CF Talk )
http://tech.groups.yahoo.com/group/flexcoders
http://groups.google.com/group/cfaussie
Forums
http://forums.digitalpoint.com/forumdisplay.php?f=56
http://www.dreamincode.net/forums/index.php?showforum=31
http://www.sitepoint.com/forums/forumdisplay.php?f=214
Aggregators:
http://www.coldfusionbloggers.org
http://www.fullasagoog.com
http://feeds.adobe.com

And lately I think the best source of help/news/community around Coldfusion is twitter. The community seams to support each other really well and help is just a tweet away!

Feb 7, 2009

Do you check file extensions before uploading to web accessible areas?

Last night a friend of mine informed me that his site had been hacked. It wasn't SQL injection or a guess-able password but was traced back to a page where documents are uploaded. The upload methods were not checking file extensions and allowed a .cfm to be uploaded to a web accessible area which in turn allowed for easy access to really mess up the site. I have one small family site where I allow uploads of images. I quickly headed over there to check my code to make sure I hadn't left the same hole open. I have a flex widget that only allows .jpg extensions and my save methods also checked the extension before it saved aswell as a try catch around the image resize incase the rename a executable to .jpg so I think I'm safe ( I used this as my starting point. )

Anyway just a warning as this could be easily overlooked.

Feb 2, 2009

SVN Hosting

I use SVN every day. I love it. Merge conflicts are about the worst you have to deal with and the rest of what SVN offers it just great. In my last post you'll see I moved from Vista to Windows 7. Was I worried about my projects data being lost during the move? No not at all, all my code is checked into SVN. I work for a company that has developers in Australia and the USA and without SVN coding on a shared DEV server would be painful. Internally we host our own SVN repositories which has served us well so far.
On the side I am always playing with some idea or another and generally that code I do not check into SVN. I do have my own personal server back at my parents house running on an old P3 with a 2nd had UPS running debian. The code there is probably 6+ months old and due to the recent power outages the UPS ran flat and the server is still turned off. Time for a move.
While conversing with Steve Good I was given a link to XP Dev. I signed up I like what I see lots of nice features including SSL, Web svn browser, private repositories, email alerts on commit and a mini project tracker.
I like it and I hope you will too.

BTW my SVN Client of choise Smart SVN. Cross platform ( Mac/*nix/Windows )