A Flash Developer Resource Site

Results 1 to 10 of 10

Thread: saving masked image to server

  1. #1
    Senior Member
    Join Date
    Oct 2000
    Posts
    184

    Thumbs up saving masked image to server

    Hi Guys,

    Anyone able to help me change the following php/AS3 to make it save the dynamically created masked image to the server instead of offering it for download? You have to pass it variables from the as3 AT THE BOTTOM OF THIS POST...

    PHP -

    <?php

    if(isset($_FILES['Filedata'])) { // basically if file data exists to actually upload.

    $maximum_filesize = 1024 * 9000; // 9000KB
    $destination_dir = 'images/'; // where we end up putting the file. Be sure this folder exists on your server


    function getExtension($file_name) { // this function gets called on line 24
    // The strrpos() function finds the numerical position of the last occurrence of a string inside another string (in this case, the period).
    $i = strrpos($file_name,".");
    if ( !$i ) {
    return "";
    }
    $h = strlen($file_name) - $i; // The strlen() function returns the length of a string.

    $ext = substr($file_name, $i+1, $h); // The substr() function returns a part of a string.
    //The three parameters in paranthesis are (the string, start location, length to slice out).

    return $ext; // this function returns our extension (like jpg or png, etc)
    } // closes out the getExtension function


    $filename = stripslashes($_FILES['Filedata']['name']); // The stripslashes() function removes backslashes added by the addslashes() function.
    $extension = getExtension($filename); //uses the function we created above
    $extension = strtolower($extension); // The strtolower() function converts a string to lowercase. So JPG would become jpg

    // if statement below will test to make sure the extension is equal to one of these 4 values.
    if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) {

    //if the extension was not equal to at least one of those extensions above then upload is aborted.
    exit( "unable to upload file ($filename)" ); // we exit out of the script entirely with an error message

    } // closes off if statement

    // finally we test to make sure the filesize is below the max file size we set above.
    if ($_FILES['Filedata']['size'] <= $maximum_filesize){

    if($fileNameAddOn != "" ) { // if fileNameAddOn does not equal nothing
    move_uploaded_file($_FILES['Filedata']['tmp_name'], $destination_dir.$fileNameAddOn.$_FILES['Filedata']['name']); // upload with the filename add on variable

    } else {
    move_uploaded_file($_FILES['Filedata']['tmp_name'], $destination_dir.$_FILES['Filedata']['name']); // upload without the filename add on variable
    }


    }// closes off if statement that checks the filesize

    }


    ?>

    AS3 -

    import com.adobe.images.JPGEncoder;
    import com.adobe.images.BitString;
    //import com.adobe.images.*;
    //import JPGEncoder;
    saveBtn.addEventListener( MouseEvent.MOUSE_DOWN, launchPrompt2 );


    /*var img_mc:MovieClip = new MovieClip(); //we'll use img_mc to hold our image later
    addChild(img_mc); //added to the stage, although not visible (due to nothing being in the movieclip)
    */
    //put mask image into a movie clip and save it the server

    function launchPrompt2(event:MouseEvent) {
    //trace(img_mc);


    function createJPG(m:MovieClip, q:Number, fileName:String)
    {
    var jpgSource:BitmapData = new BitmapData (m.width, m.height);
    jpgSource.draw(m);
    var jpgEncoder:JPGEncoder = new JPGEncoder(q);
    var jpgStream:ByteArray = jpgEncoder.encode(jpgSource);

    var header:URLRequestHeader = new URLRequestHeader ("Content-type", "application/octet-stream");

    //Make sure to use the correct path to jpg_encoder_download.php
    var jpgURLRequest:URLRequest = new URLRequest ("jpg_encoder_download.php?name=" + fileName + ".jpg");
    jpgURLRequest.requestHeaders.push(header);
    jpgURLRequest.method = URLRequestMethod.POST;
    jpgURLRequest.data = jpgStream;

    var jpgURLLoader:URLLoader = new URLLoader();
    navigateToURL(jpgURLRequest, "_blank");
    }


    //Call the function and pass in the movieclip that you want to encode,
    //the quality, and the file name

    createJPG(img_mc,70, 'badge');


    // NOW I NEED TO SAVE THE ENCODED JPG TO THE SERVER with a random number attached to the filename so they don't overwrite each other...
    /*var fileNameVar:String = "";
    var fileNameAddOn:String = "";*/
    // remember if you don't want to use the fileNameAddOn, then comment out only this next line.
    fileNameAddOn = String( Math.round ( Math.random() * 100000 ) ) ;

    var imageTypes:FileFilter = new FileFilter("Images (*.jpg, *.jpeg, *.gif, *.png)", "*.jpg; *.jpeg; *.gif; *.png"); //limits your upload type to images
    var allTypes:Array = new Array( imageTypes );
    var fileRef:FileReference = new FileReference();
    fileRef.addEventListener(Event.SELECT, selectHandler );
    fileRef.addEventListener(Event.CANCEL, cancelHandler );
    fileRef.addEventListener(Event.COMPLETE, completeHandler );
    //fileRef.browse(allTypes); //initiates browsing

    function cancelHandler(event:Event):void{
    trace("you've cancelled selecting an image");
    } //closing tag for cancelHandler

    function selectHandler(event:Event):void{

    fileNameVar = fileNameAddOn + fileRef.name; // creates a variable to store the name of the file. This include the fileNameAddOn even if it equals nothing. Which is fine.
    trace ( fileRef.name);
    //trace ( fileRef.size);

    var request:URLRequest = new URLRequest("http://www.craftmagic.co.uk/upload.php"); //location of the upload.php file.
    var params:URLVariables = new URLVariables();
    request.method = URLRequestMethod.POST;
    params.fileNameAddOn = fileNameAddOn;
    request.data = params;

    try {
    fileRef.upload(request);
    trace ( "The name of the file is... " + fileNameAddOn + fileRef.name);
    submitMessages.text = "Your Image is Uploading";

    }
    catch(error:Error) {
    trace( error );
    trace( "uh oh, there's an error");
    submitMessages.text = "Your Image Failed to Save";
    }
    } //closing tag for selectHandler
    function completeHandler(event:Event):void{

    var pictureLoader:Loader = new Loader();
    var pictureURL:String = "http://www.craftmagic.co.uk/images/" + fileNameVar; //this is the location of the folder you are uploading to PLUS the file's name
    var pictureRequest:URLRequest = new URLRequest ( pictureURL );
    pictureLoader.load( pictureRequest );

    pictureLoader.contentLoaderInfo.addEventListener(E vent.COMPLETE , imageLoaded);

    function imageLoaded(event:Event):void{
    img_mc.addChild(pictureLoader); // adds the image data into img_mc

    submitMessages.text = "Your Image was Uploaded";
    after_upload_example();

    //uploadBtn.visible = false; // we make the button invisible.
    }
    }//closing tag for completeHandler
    } // this is the closing bracket for the launchPrompt function


    Thanks for reading this and if anyone can help I'll buy them a mars bar...

    Cheers

  2. #2
    Member
    Join Date
    Jan 2002
    Posts
    44
    did you ever get this issue solved?

    rocksteady,
    danno~
    rocksteady,
    danno~

  3. #3
    Senior Member
    Join Date
    Oct 2000
    Posts
    184
    Hi there,

    Thanks for your response... No it was never resolved.

    Have a look at

    http://www.craftmagic.co.uk/mark/ this is as far as I got with it...

    If you like I'll post the fla.

    Cheers

    Mark

  4. #4
    Member
    Join Date
    Jan 2002
    Posts
    44
    yeah, if you wouldn't mind posting the .fla and any associated .as files, that would be awesome.

    thanks!

    rocksteady,
    danno~
    rocksteady,
    danno~

  5. #5
    Senior Member
    Join Date
    Oct 2000
    Posts
    184

    Thumbs up image mask uploader files...

    Here is the link to the files. It will be valid for 7 days or 100 dwnlds so let me know if I need to resend...

    http://www.yousendit.com/download/Y1...aFJJMHZIRGc9PQ

    I just remembered what a pain this was to do as I use mainly macs but had to do this in windows as there was a problem installing the AS3 corelib path into Flash on a mac that I never resolved. I could get it to work in XP so I used that to build this... Perhaps you can comment on this if you use a mac?

    Anyway best of luck with this one. A few of the files are duplicated but I'm sure you can figure out what is being used... Any problems just ask and we can crack this together.

    Cheers

    Mark

  6. #6
    Member
    Join Date
    Jan 2002
    Posts
    44
    thanks for posting that up, i appreciate it.

    couple quick questinons:
    what's the difference between upload_and_simple_mask.fla & upload_and_simple_maskMB.fla
    as well as
    the difference between save2server.as and save2server2.as

    thanks again!

    rocksteady,
    danno~
    rocksteady,
    danno~

  7. #7
    Member
    Join Date
    Jan 2002
    Posts
    44
    and a couple quick notes ... it's not doing anything after it uploads the file. i'm gonna look through the code to see if i can figure out what's happening with it though.
    i also made it publish as as3 on flash player 10, so had to update some parts in the .as files


    rocksteady,
    danno~
    rocksteady,
    danno~

  8. #8
    Senior Member
    Join Date
    Oct 2000
    Posts
    184
    Hi Mate,

    The difference in both those files is the version with MB and 2 at the end is they are versions of the same file that I have tested... Let me look at it again...

    upload_and_simple_mask is the version currently running in the example
    upload_and_simple_maskMB is an attempt at making this save the file to a server but I didn't know the code so it doesn't work. It includes a file called save2server.as which again doesn't work but needs changing to save the masked jpg to the server instead of offering it for download as currently happens.

    Hope this is clear... I'd have a look at both versions as I remember there being comments but stick with the originals if you want code that works...

    Incidentally I also had problems with it working intermittantly - but then someone pointed out that http://www.craftmagic.co.uk and http://craftmagic.co.uk are different sites as far as this code is concerned so it needs the www to work.

    Let's get this sorted as I'm only just starting AS3 and its an interesting project.

    Cheers

    Mark

  9. #9
    Junior Member
    Join Date
    Aug 2009
    Posts
    6
    Hi there,
    is there a way to save other figure of the image not only a rectangle staring x=0,y=0?

  10. #10
    Member
    Join Date
    Aug 2003
    Posts
    70
    Thank you yombi69 for sharing your code. It has helped me in my project.

    Thanks!
    Learning flash, one frame at a time

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