dcsimg
A Flash Developer Resource Site

Results 1 to 15 of 15

Thread: JPGEncoder

  1. #1
    THE YELLOW FLASH!
    Join Date
    Feb 2008
    Posts
    57

    JPGEncoder

    Hi Experts,
    I need your help. I m making a T-shirt design application in Flash.In this application users can drag n drop designs on their T-shrit,change its colour and add text...etc...and when he press a button...say "Print"...the swf shud generate a jpg(or PNG) of that design(that the user made) and send it to a php file(that estimates its rate and all) and saves another copy in my domain(like www.mycompany.com/shirts_made/).
    How can i do that??

    by searching in google i found this

    http://henryjones.us/articles/using-...3-jpeg-encoder

    It was of great help!
    But the php included in the sample prompts for download only and is not able to save file into server. So i searched again and got this:

    http://blog.joa-ebert.com/2006/05/01...file-with-php/

    but aint understandin anything! i donno where to put the code!

    Please help! URGENT!

  2. #2
    Member
    Join Date
    Sep 2007
    Posts
    48
    Create a temporary folder in the folder where you have your t-shirt designer and call it "temp"

    Open your jpeg_encoder_download.php and replace its contents with this:

    PHP Code:
    <?php

    if ( isset ( $GLOBALS["HTTP_RAW_POST_DATA"] )) {
        
        
    // get bytearray
        
    $im $GLOBALS["HTTP_RAW_POST_DATA"];
        
        
    fwrite($im,"temp/output.jpg");
        
    }  else echo 
    'An error occured.';

    ?>
    Which I assume should work. This will create a file called output.jpg inside your temp folder. It is untested.

  3. #3
    THE YELLOW FLASH!
    Join Date
    Feb 2008
    Posts
    57
    Dude i get this error!
    Warning: fwrite(): supplied argument is not a valid stream resource in /home/www/axeleron.net/flashtest/jpg_encoder_download.php on line 8
    The CodeBreaker!

  4. #4
    THE YELLOW FLASH!
    Join Date
    Feb 2008
    Posts
    57
    i got this one...but donno where to put it!
    http://blog.joa-ebert.com/2006/05/01...file-with-php/
    In my swf...there is no array variable!
    The CodeBreaker!

  5. #5
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    I don't know php, but that code up there looks like an invitation to try to upload malicious code to your server. Never ever trust the client to send the sort of data you expect. Be extra careful that any file generated by this process is not executable as php, and ideally be sure that it is a valid image.

  6. #6
    Member
    Join Date
    Sep 2007
    Posts
    48
    Quote Originally Posted by 5TonsOfFlax View Post
    I don't know php, but that code up there looks like an invitation to try to upload malicious code to your server. Never ever trust the client to send the sort of data you expect. Be extra careful that any file generated by this process is not executable as php, and ideally be sure that it is a valid image.

    That code wont let anyone upload a malicious code as it is going to save it with a "jpg" extension and a PHP file wont run if it has a jpg extension.

  7. #7
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Good to know. It's still a vector for people to store arbitrary files on your server, such as malicious javascript or anything else. The name is a minor hurdle there. Regardless, that wasn't the intent of this thread so I'll stop hijacking it.

  8. #8
    Member
    Join Date
    Sep 2007
    Posts
    48
    Yes, anyway. billiondegreedr, this is how I export JPEG from Flash:

    1. use BitmapData.draw() method to get a snapshot of the movieclip you want to take a snap of.

    2. Encode it using the JPEGEncoder or PNG encoder and save it in a string.

    3. Encode it using the Base64 Encoder.

    4. Send it to PHP using the URLLoader:

    PHP Code:
                    var Access:URLRequest = new URLRequest();
                    
    Access.url"get_images.php";
                    
    Access.method=URLRequestMethod.POST;
                    
    Access.dataEncodedImageString;
                                    
    //EncodedImageString is the response string of the Base64 Encoder class.
                    
    var LODR:URLLoader = new URLLoader();
                    
    LODR.load(Access); 
    This should be all at the ActionScript part. Next I create a PHP file and fill it up with this:

    PHP Code:
    <?php 
    $today 
    date("Ymd_His"); 
    $filename $today.rand(0,5000).'_img.png'#.png is because I use PNG Encoder, you can write .jpg for JPEGEncoder.

    $somecontent base64_decode($_REQUEST['png']);

    if (
    $handle fopen("images/".$filename'w+'))
    if (!
    fwrite($handle$somecontent) === FALSE)     
    fclose($handle);
    ?>
    This should be a good help for you to think on. I didn't copy and paste the entire code because I think spoon feeding is strictly prohibited here. Since one who wants to get help should help them selves too.

    Let me know if this works out for you.

  9. #9
    THE YELLOW FLASH!
    Join Date
    Feb 2008
    Posts
    57
    Yea, i too dont like spoon feeding lol! But i donno php well too.Thats y i asked.But i get this error.

    5001: The name of package 'com.dynamicflash.util' does not reflect the location of this file. Please change the package definition's name inside this file, or move the file. C:\Documents and Settings\D\Desktop\TempImp\test\Base64.as

    My test.fla is in C:\Documents and Settings\D\Desktop\TempImp\test\test.fla

    I setted my prefrences in settings as C:\Documents and Settings\D\LocalSettings\Adobe\Flash CS4\en\configuration\classess\Base64\src\com\dynam iclfash\util\

    And I even copied the Base64.as file into C:\Documents and Settings\D\Desktop\TempImp\test\

    but again i get the same error!Even setted the classpath as C:\Documents and Settings\D\Desktop\TempImp\test\com\dynamicflash\u til

    no use!

    here is my flash code: temme if its correct:
    -------------------------------------------
    Code:
    import com.adobe.images.JPGEncoder;
    import com.dynamicflash.util.Base64;
    var jpgSource:BitmapData = new BitmapData (sketch_mc.width, sketch_mc.height);
    jpgSource.draw(sketch_mc);
    var jpgEncoder:JPGEncoder = new JPGEncoder(85);
    var jpgStream:ByteArray = jpgEncoder.encode(jpgSource);
    //converting to base64
    var sbase64:String = Base64.encodeByteArray(jpgStream);
    //sending...
    var Access:URLRequest = new URLRequest();
                    Access.url= "www.axeleron.net/flashtest/get_images.php";
                    Access.method=URLRequestMethod.POST;
                    Access.data= sbase64;
                                    //EncodedImageString is the response string of the Base64 Encoder class.
                    var LODR:URLLoader = new URLLoader();
                    LODR.load(Access); 
                    navigateToURL(Access,"_blank");
    -----------------------------------------------------------------------------
    please help ! coz i m working with classess for the first time ...Learning only!...
    Last edited by billiondegreedr; 08-07-2009 at 11:36 AM.
    The CodeBreaker!

  10. #10
    THE YELLOW FLASH!
    Join Date
    Feb 2008
    Posts
    57
    dude i got the 5001: error solved and i uploaded test.swf,get_images.php in my server and ran it.But the image files in the "images" folder are 0 bytes and contain nothing!!! Is it the problem with the Flashcode or php?? The sending will b done as soon as the clip loads rite? LODR.load(access); there is no other trigger in this rite?
    >sigh< ... even google doesnt have any solutions! please help guys!!
    Last edited by billiondegreedr; 08-07-2009 at 11:35 AM.
    The CodeBreaker!

  11. #11
    Member
    Join Date
    Sep 2007
    Posts
    48
    Well, google does have solutions.

    Anyway,
    The image is of 0 bytes probably because the movie clip you're trying to "draw" is empty.

    Also, its "JPEGEncoder" not "jpgEncoder."
    I have never passed the JPEGEncoder's constructor a number as argument as you passed it
    PHP Code:
    new JPEGEncoder(85
    There is no need to navigate to that URL. Though you can use Event.COMPLETE to see if it has been loaded, however that should be the *side dish* as of course you'll have lots of other functions too sitting in your code to do a lot of other stuff?

  12. #12
    THE YELLOW FLASH!
    Join Date
    Feb 2008
    Posts
    57
    does this support canvas bigger than 100x100? coz i saw in some forums that it wont support.for abv 100x100 u need to use External Interface.And one more doubt...I drew some abstract with brush in flash...that shud be converted rite?so its not empty!
    The CodeBreaker!

  13. #13
    Member
    Join Date
    Sep 2007
    Posts
    48
    Hi,

    Ok I've created a class for you. You just have to instanciate it and call its function that snaps and then simply tell it the URL to the PHP page where it needs to be departed. I have attached a sample PHP file as well.

    This Require's the AdobeAS3CoreLib and Dynamic Flash's Base64 Encoder/Decoder.

    Go here for details: http://www.muhammadbinyusrat.com/flash/PNGExport/

  14. #14
    THE YELLOW FLASH!
    Join Date
    Feb 2008
    Posts
    57
    Awesome buddy!! its workin 100% and I think this thread must be made sticky coz there are no perfect and simple code like this! Anyway...i m breaking into ur code to study it.Thanks a lot!! catchya l8r!
    The CodeBreaker!

  15. #15
    Member
    Join Date
    Sep 2007
    Posts
    48
    You're welcome.

Tags for this Thread

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