Home Tutorials Forums Articles Blogs Movies Library Employment Press

Go Back   ActionScript.org Forums > ActionScript Forums Group > ActionScript 2.0

Reply
 
Thread Tools Rate Thread Display Modes
Old 11-28-2005, 12:55 PM   #1
ryando
Member
 
Join Date: Jun 2003
Location: Chicago
Posts: 97
Default Save a jpeg from flash 8, help with source code

This could potentially be useful to a lot of people out there.

I have been deconstructing the save bitmap script from, http://www.flash-db.com/Tutorials/snapshot/. Trying to turn it into a really simple application a lot of people could use.

Here is where I am at, (FLASH 8 player required).
(I had to take this link down, in fear of blowing out my bandwidth on my site, source code is below...)

It is simply duplicating a movie clip as bitmap data and then passing it to a php script that then turns it into a jpeg. Pretty cool and potentially useful.

The problem, as you probably have seen is that the color shifts a lot on conversion. I am guessing the problem is in the php script...

I am not a php guru, any of you?

THE PHP CODE:
PHP Code:
<?php
    
//If GD library is not installed, say sorry
    
if(!function_exists("imagecreate")) die("Sorry, you need GD library to run this example");
    
//Capture Post data
    
$data explode(","$_POST['img']);
    
$width $_POST['width'];
    
$height $_POST['height'];
    
//Allocate image
    
$image=(function_exists("imagecreatetruecolor"))?imagecreatetruecolor$width ,$height ):imagecreate$width ,$height );
    
$background imagecolorallocate$image ,);
    
//Copy pixels
    
$i 0;
    for(
$x=0$x<=$width$x++){
        for(
$y=0$y<=$height$y++){
            
$r 255-hexdec("0X".substr$data[$i] , )); 
            
$g 255-hexdec("0x".substr$data[$i] , )); 
            
$b 255-hexdec("0x".substr$data[$i++] , ));
            
$color =  ($r << 16) | ($g << 8) | $b
            
imagesetpixel $image $x $y $color );
        }
    }
    
//Output image and clean
    
header"Content-type: image/jpeg" );
    
ImageJPEG$image );
    
imagedestroy$image );    
?>
THE FLASH CODE:
ActionScript Code:
/** Screenshot and jpg output **/ import flash.display.BitmapData; import flash.geom.Matrix; //Buttons handlers. Should add an extra function because delegate doesn't allow to pass parameters shaF.onPress = mx.utils.Delegate.create(this,makeShadow); //Helper functions to pass parameters function makeShadow() { capture(0) } /* create a function that takes a snapshot of the Video object whenever it is called and shows in different clips */ function capture(nr){     this["snapshot"+nr] = new BitmapData(output_vid._width,output_vid._height);      //the bitmap object with no transformations applied     this["snapshot"+nr].draw(output_vid,new Matrix());     var t:MovieClip = createEmptyMovieClip("bitmap_mc"+nr,nr);         //positions clip in correct place     //t._x = 350; t._y = 10+(nr*130); t._xscale = t._yscale = 50         //display the specified bitmap object inside the movie clip     t.attachBitmap(this["snapshot"+nr],1); output(nr);     //attachMovie("print_but", "bot"+nr, 100+nr, {_x:t._x+t._width+50, _y:t._y+t._height/2})    } //Create a new bitmapdata, resize it 50 %, pass image data to a server script // using a LoadVars object (large packet) function output(nr){         //Here we will copy pixels data         var pixels:Array = new Array()         //Create a new BitmapData         var snap = new BitmapData(this["snapshot"+nr].width, this["snapshot"+nr].height);            //Matrix to scale the new image         myMatrix = new Matrix();         myMatrix.scale(1, 1)                 //Copy image         snap.draw(this["snapshot"+nr],  myMatrix);         var w:Number = snap.width, tmp         var h:Number = snap.height         //Build pixels array         for(var a=0; a<=w; a++){             for(var b=0; b<=h; b++){                 tmp = snap.getPixel32(a, b).toString(16)                 pixels.push(tmp.substr(1))             }         }         //Create the LoadVars object and pass data to PHP script         var output:LoadVars = new LoadVars()         output.img = pixels.toString()         output.height = h         output.width = w         //The page (and this movie itself) should be in a server to work         output.send("show.php", "output", "POST")             } stop()
Attached Files
File Type: zip save_bitmap.zip (147.3 KB, 3100 views)

Last edited by CyanBlue; 03-28-2007 at 06:30 PM. Reason: Code formatting tag applied...
ryando is offline   Reply With Quote
Old 11-28-2005, 01:32 PM   #2
Xeef
Off-Line
 
Xeef's Avatar
 
Join Date: Aug 2004
Location: Ibiza/Spain language :Hungarian/German/ abit English
Posts: 6,539
Default

1.
tmp = snap.getPixel32(a, b).toString(16)
pixels.push(tmp.substr(1))
if you don't use the Alpha why do you get it ?
use snap.getPixel(a, b)

2.

snap.getPixel32(a, b).toString(16)
0x0AFFFFFF --> toString will give AFFFFFF so the "0" is missing !

3.

hexdec don't need "0x" infront !
also there is no need at all for it
echo 255-("0x"."ff"); will do the job


Hmmmm i just test your link and it asking me the 4 time to cancel the script because it causing flashplayer to run slow and it already took 550Mb and i have just 768
__________________

test your self -->http://www.actionscript.org/forums/s...86&postcount=8
Xeef is offline   Reply With Quote
Old 11-28-2005, 02:01 PM   #3
ryando
Member
 
Join Date: Jun 2003
Location: Chicago
Posts: 97
Default

Yeah it forces flash to pass a huge string, not sure if there is a good way around this.

What do you mean by 550mb, how are you measuring this.

Thanks for the reply, I am going to implement these and put up updated source code.
ryando is offline   Reply With Quote
Old 11-28-2005, 08:14 PM   #4
Xeef
Off-Line
 
Xeef's Avatar
 
Join Date: Aug 2004
Location: Ibiza/Spain language :Hungarian/German/ abit English
Posts: 6,539
Default

i was opening your link

the flash player was warning me the the script is runing slow ....
i pressed 4 times no (to not to stop it) and was looking to taskmanager and the instance IE in wich it was runing was took around 550Mb memory
__________________

test your self -->http://www.actionscript.org/forums/s...86&postcount=8
Xeef is offline   Reply With Quote
Old 11-29-2005, 03:57 AM   #5
ryando
Member
 
Join Date: Jun 2003
Location: Chicago
Posts: 97
Default

Here is the updated code:
Stillnot working, anybody?!!!!
I made the file smaller so it would not crash computers.

http://www.rdlstudio.com/save_bitmap/

As you can see the colors are not correct yet.

I attached the source code as a .zip and here is the code:
ActionScript Code:
FLASH: /** Screenshot and jpg output **/ //setup filter import flash.display.BitmapData; //function that takes a snapshot of the movie clip function capture(nr){     this["snapshot"+nr] = new BitmapData(image_holder._width,image_holder._height);      //the bitmap object with no transformations applied     this["snapshot"+nr].draw(image_holder);     var t:MovieClip = createEmptyMovieClip("bitmap_mc");         //display the specified bitmap object inside the movie clip     t.attachBitmap(this["snapshot"+nr],1); output(nr); } //Create a new bitmapdata, pass image data to a server script function output(nr){             //Here we will copy pixels data         var pixels:Array = new Array()                 //Create a new BitmapData         var snap = new BitmapData(this["snapshot"+nr].width, this["snapshot"+nr].height);                    //Copy image         snap.draw(this["snapshot"+nr]);         var w:Number = snap.width         var h:Number = snap.height         //Build pixels array         for(var a=0; a<=w; a++){             for(var b=0; b<=h; b++){                 tmp = snap.getPixel32(a, b)                 pixels.push(tmp)                                                         }         }                                 //Create the LoadVars object and pass data to PHP script         var output:LoadVars = new LoadVars()         output.img = pixels.toString()         output.height = h         output.width = w         //The page (and this movie itself) should be in a server to work         output.send("show.php", "output", "POST")             } stop()
PHP:
PHP Code:
<?php
    
//If GD library is not installed, say sorry
    
if(!function_exists("imagecreate")) die("Sorry, you need GD library to run this example");
    
//Capture Post data
    
$data explode(","$_POST['img']);
    
$width $_POST['width'];
    
$height $_POST['height'];
    
//Allocate image
    
$image=(function_exists("imagecreatetruecolor"))?imagecreatetruecolor$width ,$height ):imagecreate$width ,$height );
    
$background imagecolorallocate$image ,);
    
//Copy pixels
    
$i 0;
    for(
$x=0$x<=$width$x++){
        for(
$y=0$y<=$height$y++){
            
$r 255-hexdec(substr$data[$i] , )); 
            
$g 255-hexdec(substr$data[$i] , )); 
            
$b 255-hexdec(substr$data[$i++] , ));
            
$color =  ($r << 16) | ($g << 8) | $b
            
imagesetpixel $image $x $y $color );
        }
    }
    
//Output image and clean
    
header"Content-type: image/jpeg" );
    
ImageJPEG$image );
    
imagedestroy$image );    
?>
Attached Files
File Type: zip save_bitmap.zip (38.5 KB, 1778 views)

Last edited by CyanBlue; 03-28-2007 at 06:31 PM. Reason: Code formatting tag applied...
ryando is offline   Reply With Quote
Old 11-29-2005, 01:12 PM   #6
Xeef
Off-Line
 
Xeef's Avatar
 
Join Date: Aug 2004
Location: Ibiza/Spain language :Hungarian/German/ abit English
Posts: 6,539
Default

i still can't test your link !!!
Attached Thumbnails
Click image for larger version

Name:	qqq.JPG
Views:	1458
Size:	34.6 KB
ID:	15377  
__________________

test your self -->http://www.actionscript.org/forums/s...86&postcount=8
Xeef is offline   Reply With Quote
Old 02-11-2007, 07:16 AM   #7
bensir
Registered User
 
Join Date: Feb 2007
Posts: 1
Thumbs up

This was two year's old Thread la.
but I really got some idea to make the flash to jpg here.
and I finally found the answer.

I have updated the php and actionscript and it work in IE!!

OLD PHP code:
PHP Code:
$r 255-hexdec("0X".substr$data[$i] , ));
$g 255-hexdec("0x".substr$data[$i] , ));
$b 255-hexdec("0x".substr$data[$i++] , ));
$color = ($r << 16) | ($g << 8) | $b;
imagesetpixel $image $x $y $color ); 
new PHP code:
PHP Code:
<?php
    $data 
explode(","$_POST['img']);
    
$width $_POST['width'];
    
$height $_POST['height'];
    
$image=imagecreatetruecolor$width ,$height );
    
$background imagecolorallocate$image ,);
    
//Copy pixels
    
$i 0;
    for(
$x=0$x<=$width$x++){
        for(
$y=0$y<=$height$y++){
            
$int hexdec($data[$i++]);
            
$color ImageColorAllocate ($image0xFF & ($int >> 0x10), 0xFF & ($int >> 0x8), 0xFF $int);
            
imagesetpixel $image $x $y $color );
        }
    }
    
//Output image and clean
    
header"Content-type: image/jpeg" );
    
ImageJPEG$image );
    
imagedestroy$image );    
?>
OLD actionscript:
ActionScript Code:
tmp = snap.getPixel32(a, b).toString(16) pixels.push(tmp.substr(1))
new actionscript:
ActionScript Code:
import flash.display.BitmapData; shaF.onPress = function() {     output(); }; function output() {     snap = new BitmapData(mc._width, mc._height);     snap.draw(mc);     var pixels:Array = new Array();     var w:Number = snap.width;     var h:Number = snap.height;     for (var a = 0; a<=w; a++) {         for (var b = 0; b<=h; b++) {             var tmp = snap.getPixel(a, b).toString(16);             pixels.push(tmp);         }     }     var output:LoadVars = new LoadVars();     output.img = pixels.toString();     output.height = h;     output.width = w;     output.send("show.php", "output", "POST"); } stop();
ben
Attached Files
File Type: zip save_bitmap 11Feb07.zip (12.5 KB, 5127 views)

Last edited by CyanBlue; 03-28-2007 at 06:32 PM. Reason: Code formatting tag applied...
bensir is offline   Reply With Quote
Old 02-11-2007, 04:11 PM   #8
inhan
it's all about patience
 
inhan's Avatar
 
Join Date: Jun 2005
Location: istanbul
Posts: 5,859
Default

I don't know the PC's but it only directs me to the php page when in FF and Safari..
__________________
Ali Inhan
Turkish graphic and web designer
an Apple fan
www.aliinhan.com
inhan is offline   Reply With Quote
Old 02-14-2007, 01:34 PM   #9
spikeuk22
Registered User
 
spikeuk22's Avatar
 
Join Date: May 2004
Posts: 60
Wink

This looks like what I am looking for.... been out of flash for a while so bear with me....

Basically im trying to create a small flash based proggy to make signatures with kill details on...

e.g. for world of warcraft. For example :

Name Bob Last kill : Steve

on a background picture

I have a background jpg it looks like I can use the coding above to take the shot, question is will I be able to import the dynamic text onto the bitmap and will the screenshot take the picture including the dynamic text or will it just be the background picture?...

ignore importing the data, ill figure out that bit im just tryint to figure out if I can take the screenshot and it will include the dynamic text, as dynamic text is a pain from previous expierience... any suggestions most appreciated.

..........UPDATE... I just had a look at the script and it looks as if flash is used just to display the picture and pass on the width and height where the screenshot will be taken to the PHP ...soo basically the php will just take a snapshot of what is on screen at the co ords provided? in which case it should have no problem with dynamic text etc? is this correct?

Last edited by spikeuk22; 02-14-2007 at 01:51 PM.
spikeuk22 is offline   Reply With Quote
Old 03-28-2007, 06:11 PM   #10
alan_bourne
Member
 
Join Date: Feb 2006
Posts: 98
Default

I have added this to my flash document but it keeps sending blank jpgs, is this because the movie clip thats being created is too low in depth. Im trying to capture dynamically created movieclips, text and images pulled into flash. However it doesnt even capture a background image that im using?

any ideas?
alan_bourne is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
one template, many looks? subquark ActionScript 1.0 (and below) 1163 12-22-2009 01:10 AM
Flash, JavaScript and the HTML DOM (dHTML) drZoode HTML and JavaScript 10 08-21-2008 06:59 PM
Help: Calling ASP code in Flash hysteria ActionScript 2.0 4 10-16-2006 03:42 AM
Some Flash Video Converter Tools Compare terry117 Detention 7 05-18-2006 02:08 AM
source code editor for flash 5 percepi ActionScript 1.0 (and below) 4 08-10-2001 04:58 PM


All times are GMT. The time now is 08:16 PM.


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2010 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.