Load PNG, GIF or progressive JPEG into SWF?
Did you have the problem before that you wanted to load some file formats into your Flash site but loadMovie does not accept them? Something like PNGs, GIFs or progressive JPEGs? Do you want to load images or SWFs from a different domain but the security sandbox is in your way? For these purposes I have written swfImageProxy, a PHP script that automatically converts file formats that are usually indigestible for Flash into something that it can display. On top of that it acts a proxy and is able to download images from other domains.
Here is a simple Flash demo that allows you to enter the URL of an image and swfImageProxy will try to retrieve and convert it.
If you want to use the full functionality of the script your server will have to fulfill certain prerequisites:
- For basic functionality you will at least need PHP with GD support.
- If you want the conversion of progressive JPEGs to baseline optimized JPEGs to be lossless the script needs access to the jpegtran utility.
- If you want to be able to read GIFs the minimum required version of GD is 2.0.28 which currently happens to be the latest version
- If you want to preserve the transparency of PNGs and GIFs they have to be packaged as SWFs. For this purpose the png2swf utility from the excellent swftools package by Rainer Böhme and Matthias Kramm has to be installed
- To convert animated GIFs you need access to ImageMagick
You can download swfImageProxy here. Currently I have only tested this on Linux - if you are able to run this on Windows I'm happy to hear about it. I have tried to make this as secure as possible, but of course you use this on your own risk. It also looks like the png2swf tool does not fully support every format of PNGs yet.
Thanks to Jon Williams for the initial inspiration!
Update: I have just fixed a bug that killed the transparency with 24-bit PNGs so the latest version is swfImageProxy v0.2
Update: fixed the quality=0 bug that occured when you used GD only for conversion. The latest version is now swfImageProxy v0.3
Latest update: I integrated an option to use ImageMagick for the conversion of animated GIFs to SWFs. Though it does not yet give optimum results for every type of GIF. Latest version: swfImageProxy v0.4
Posted at September 14, 2004 02:12 PM | Further reading
this sounds very cute! :) but actually the download link doesn't work. for any reason it forwards me to a search results page. :(
As nicole richie would say.. "thats hot"
ok, well here's the correct link: click
thanks a lot :)
try to use "swfimgproxy" instead of "swfimageproxy" in the download link. Anyway, thats a real nice add-on to the exsisting functionality :)
Thanks for pointing me to the wrong links - I've corrected them now...
What kind of lincense are you releasing this under?
Good Question. I always forget the legal mumbo-jumbo. So let this be released under GPL: http://www.gnu.org/copyleft/gpl.html
Looks like I'll have to add that to the sourcecode, too.
Sorry... but this is not "new"... used PHP with GD-lib so often for flashbased e-Commerce systems... my new project FLOE (flash-based online editor) will also a module with image editing of GIF, JPG and PNG...
Hello -
I just downloaded and adapted this to my site…
I mean by adapted, my .SWF, for the rest I upload both .PHP files on the serveur (Apache/1.3.29 Server at thebend.be Port 80 , PHP Version 4.3.8, GV version 2.0 or higher).
BUT, I encounter a very strange problem, my pictures (.GIF, .JPG, …) are well downloaded, but look very damaged.
Please would you mind to take a rapid look at: http://www.thebend.be/ideas/
Search for "log" (by example) and tell me what do you think don't works correctly? Thanks…
Have a nice evening…
- Dimitri
@dimitri: oh it looks like I forgot to put the quality setting into the function that returns the JPEG. I have fixed that bug and uploaded the new version.
But if you see this it also means that the script on your machine can only use GD. So your PNGs and GIFs will not be transparent.
@Erik Lembke: I'm sure that others have found similar solutions for this task before and maybe I just used the wrong keywords to find them on Google.
But feel free to add the URL to your script here. It would be interesting to see if you found a way that needs less installations on the server (which is definitely a negative point of my solution).
Well, it looks like I did not search well enough. Here is a .NET based solution I just came across: http://www.img2swf.com - you can get that one for US$99.
By any chance, do you think that an animated gif could be turned into a swf?
Animated GIF ... now that would be something wouldn't it? Unfortunately, it looks like GD is not able to access the single frames of animated GIFs which is a pity because the png2swf utility is already able to create an animation out of a series of PNGs (to which I convert GIFs anyway). So what I would need for that is some tool or script to access the single frames of a GIF and their respective delays - then it should be possible to do it. So if anybody can point me to a free tool I'll give it a try.
Hey Mario - very cool - nice work!
I thought I would mention that there are a couple other PHP specific solutions for doing this that have been around for some time - (I really like the support for PNG and gif in yours though as it seems more robust) - I had written an article back in July of 2002 comparing some features of Flash 5 with new features found in MX right after MX had just been released, that used a similar php system to load jpeg's. for a dynamic webcam. You can check it out here: http://www.informit.com/articles/article.asp?p=27770&seqNum=1
Wow, havent read that article in sometime - and it looks like I also referenced Claus's Wahlers tool jpg2swf, and another tool I still use occassionally by Stefan Schuessler - which can now be found here: http://www.bronsonbeta.com/oldstuff/
Have you ever played with imagemagick? http://www.imagemagick.org/
I have used that in a few projects server side to accomplish some similar results as well. Thought you might want to take look at it because of its support for many different image formats, including steganographic images. I simply call it from php using shell or exec, and it works great. Might be worth looking into for the animated .gif support.
Nice work!
Rob
I agree with Rob, very nice work!
Thanks for all Mario… Long life to Quasimondo!
So good! I just translated this news into Russian and posted on flash-ripper site. Thank you!
Okay, I have checked out ImageMagic now and yes - it's really powerful. Theoretically It could convert almost any image format to something I can embed into an SWF file. With the right additional libraries one could even take screenshots from webpages.
But okay let's stay with the animated gifs for a start. I have integrated a first version and if you test it in the demo you should be able to see something if you point the URL to an animated GIF.
Though it is not perfect yet: currently I completely ignore the delay of the single GIF frames and just use the default framerate of the swf. But what is worse is that I could not force Imagemagick to export all the frames of a GIF in the same size if they use an optimized canvas. The consequence is that GIFs that use this technique will jump around. I have tried any trick that I could imagine, but no success. Maybe someone here has an idea?
Does this works with tiff format also?
Do you need TIFF? Well, with ImageMagick's help it's definitely possible to do that. I'll try to integrate that in the next Update.
That's great to here!
Eyes open for the next update :-)
Hi Mario! Great stuff! But I still haven't been able to get a noncorrupted file from all of these links! Can you post it as a zip or sit?
Thanks,
Bruce
Love it! Been using gif2swf on my local machine until this point. This is a godsend!
Asad iqbal you are regisered to post coment
Mario,
swfImageProxy is very cool and exactly the type of package I'm looking for. I'm in the process of reading your code and hopefully I'll get it running soon.
In the mean time, I was wondering how you view the project several months later. Are you using it in any production environments? Has it been stable? Did you find a better solution?
Thanks,
Denny
Does anyone know if this is possible to do in ASP? I'm afraid I don't know PHP.
I am unable to get this to work on Windows. I get a large "X" image (image not found) error. Anybody get this to work on Windows?
WinXP/Apache
I never tried to run it on Windows. I guess it's the wrong file paths that break it. Do you have all the necessary libraries installed already?
@Tamara - that is strange indeed. Currently I have no idea why it doesn't work anymore. Maybe it's a problem with my server's configuration?
i obtain Warning: fopen(p1110286756018452000) [function.fopen]: failed to create stream: Permission denied
what file o what directory need permiss?
great work!
I don't suppose it's possible to get a load percentage when loading images via swfimgproxy? In my tests getBytesLoaded() is either 0 or 100%.
@felix: That's strange because the format that arrives at your flash file is either a JPEG or a regular swf. So it should not affect the way that flash loads files. Maybe the files you tested were so small that they loaded immediately?
It looks like GD's imageCreateFromPNG function has a problem with your PNG file. How do you generate those files?
See thats part of the problem ... I dont know. They are both created by other peoples web script ... first one is from xfire a gaming client that monitors all games you play and makes a pic w/ stats of which game you play the most. And the second one is made by Zone a gaming website ... that particular one is for a game called Age of Conqures.
As your PNG doesn't use any transparency you could modify my script so it skips the part that uses imageCreateFromPNG and use the png2swf directly (I tested png2swf manually with your file and that worked)
hi, this tool is great!! i've just downloaded it.. and about to use..well, try to use it..
butta, is there a tutorial that'd show us how to use it? what to do with it? how to code it in flash Actionscript or etc??? i'm clueless..
Hello Mario... really nice code..
Just want to post that what Felix wrote above, happens with me too. loadMovie can't get the bytes of the jpgs.
loadClip doesnt even work.
Could be fixed ? or my code that should be fixed ?
Hello again,
Have someone had troubles with " " white spaces when using Mario's code?
The original I get this
/albums/album%20with%20spaces/pic1.jpg
dont work... so I str_replace to this:
/albums/album with spaces/pic1.jpg
should be something about changing the $imgname for the imageCreateFromJpeg() function...
but i'm a newbie in php.... could someone help ? heh
hey mario,
first of all ... great work you did.
but secondly ... i have the same problem with png like caffeinAddiction. and it seems that the png you proposed yourself (sep 4, 2004) ... http://www.quasimondo.com/swfimgproxy/png24_interlaced.png ... doesnt work either. hm?! so, what now? :-|
As my own png is not working anymore that makes it sound like some update to one of the used libraries: GD, imageMagick or swftools made it break.
Maybe I will find some time to look into this problem, but don't wait for it...
yes, your png doesnt work on your site, on mine it does, so its your libraries! go find it :)
come on, bo ... tell us ... which libs / versions do you have? wouldnt it be nice, if everone knew what to use to make this very nice tool work?!
We stumbled by this issue with a shop with many items (transp. PNG's). ASPimage, and GDimage didnt do it well, so testing img2swf (from www.img2swf.com) was a big relief :-)
can we run swfimageproxy through flash mx 2004