We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › save to web --- image export
Pages: 1 2 3 
save to web --- image export (Read 15128 times)
save to web --- image export
Jun 5th, 2008, 11:40am
 
hello,

when starting my program every 5 seconds there will be generated a new image. if pressing "RETURN" the image is saved in the local "data - folder" on my computer using "saveFrame ()".
I want to put my applet on an internetsite an when pressing RETURN the image has to be saved and displayed on the website. So that all users can see what others haved saved before..

I read through the script http://processing.org/hacks/doku.php?id=hacks:savetoweb but i struggle at some aspects.

Do I have to use this syntax for saving images
postToWeb ( String Title, String File-Extension, String SaveFolder, byte[] Data, boolean Open file when done);

or that

url = "http://processing.org/"; // the saveFile.php has to be here \\

// then i want to save all jpgs to a folder called savedJPG \\
saveToWeb_saveJPG("mytitle","savedJPG",bufferImage(get(0,0,width,height)));



What is the "saveFile.php" to be? do i need a database in my website which needs to be writable (777) for all users?
If so, where do I have to place the url in my script? In the example above it just says "url = "http://processing.org/", but wehre do i place this in my processing script?

Thank you for help, i really appreciate this great community.


Re: save to web --- image export
Reply #1 - Jun 5th, 2008, 1:02pm
 
mmmm hacks;

http://processing.org/hacks/doku.php?id=hacks:savetoweb
Re: save to web --- image export
Reply #2 - Jun 5th, 2008, 1:33pm
 
Manic, you repeat the URL leanderlike gave... Wink

leanderlike, the page is useful but a bit inconsistent, one should correct it.
There is no postToWeb function in this page. Only postData which is kind of private. The signature corresponds actually to saveToWeb_saveFile function...
So that's saveToWeb_saveJPG function you have to use.

The PHP FILE (saveFile.php) is given at the bottom of the code box (one should make two distinct code boxes).
There is no database, you need to have one (or several, eg. one per user) writable folders, were the saved images will go.

Say you have a site named www.leanderlike.com. You put the script in a folder named /scripts, for example.
So you write url = "http://www.leanderlike.com/scripts/";
Re: save to web --- image export
Reply #3 - Jun 5th, 2008, 4:25pm
 
sorry, my bad.
Re: save to web --- image export
Reply #4 - Jun 5th, 2008, 6:03pm
 
i worked on it once again.

now the script is working without errors, but it does not load up any image.

when running the script on my desktop and pressing 's' for saving, i get printed "Saving JPG start" and then directly "Saving JPG stop". nothing happens on my webserver.

on my webserver i made a folder called "Saved" (permission 777) and a file called "saveFile.php"
they are both in the same folder with my processing scripts. all on permission 777

do i have to make any other files? do i have to place any default content inside my saveFile.php - file or in the saved - file?

thanks for help
Re: save to web --- image export
Reply #5 - Jun 5th, 2008, 6:08pm
 
Sad I see several problems (in the previous version of your last message, I no longer see it since I started this answer).

First, apparently there is no http://leanderlike.com/ Web site (Firefox shows an error page). So it cannot work, period. I gave this URL as example, hoping you have a Web site with PHP support.
If you don't have one, you cannot save anything.
Now, supposing you have such site (you mention a server providing the applet), you have to replace the http://leanderlike.com/ URL by the real URL. If that's a test server on your computer, put http://localhost for example.

Second problem: it looks like you have put the PHP FILE section in your sketch. It must be in a separate file (saveFile.php), with .php extension.
Third problem: the PHP snippet looks incomplete: I don't know where the $data variable come from. The is_uploaded_file page at PHP.net site shows what should be there.

Somehow, it is an interesting problem, so I will try and make it work, and when we will be able to log in Hacks, I will update this page.

BTW, there will be no pop-up to display the image! I don't know if an applet is able to spawn such pop-up.
At best you can have a separate page, generated by another PHP script, showing all the uploaded images in a given folder (for example).
Re: save to web --- image export
Reply #6 - Jun 5th, 2008, 7:07pm
 
ok, first of all: inside my processing script i have these script for saving:

void keyReleased () {
 switch (key) {
 case 's':
   saveToWeb_saveJPG("processing",true, get(0,0,width-175,height));
   break;
 }
}
in a second file i have this script:


import com.sun.image.codec.jpeg.*;
String url = "http://leanderlike.com/processing";  // i put the correct url inside it
boolean saving = false;

void saveToWeb_saveFile(String title, String ext, String folder, byte[] data, boolean popup)
{
 println("SAVING File START");  
 postData(title+"_"+aZ(year())+aZ(month())+aZ(day())+"-"+aZ(hour())+aZ(minute())+
aZ(second()),ext,folder,data,popup);
 println("SAVING File STOP");  
}

void saveToWeb_saveJPG(String title, boolean date, PImage src)
{
 println("SAVING JPG START");  
 postData(title+((date)?"_"+aZ(year())+aZ(month())+aZ(day())+"-"+aZ(hour())+aZ(mi
nute())+aZ(second()):""),"jpg","saved",bufferImage(src),true);
 println("SAVING JPG STOP");  
}

void postData(String title, String ext, String folder,  byte[] bytes, boolean popup)
{
 try{
   URL u = new URL(url+"saveFile.php?title="+title+"&ext="+ext+"&folder="+folder);
   URLConnection c = u.openConnection();
   
   // post multipart data
   c.setDoOutput(true);
   c.setDoInput(true);
   c.setUseCaches(false);
   
   // set request headers
   c.setRequestProperty("Content-Type", "multipart/form-data; boundary=AXi93A");
   
   // open a stream which can write to the url
   DataOutputStream dstream = new DataOutputStream(c.getOutputStream());
   
   // write content to the server, begin with the tag that says a content element is comming
   dstream.writeBytes("--AXi93A\r\n");
   
   // discribe the content
   dstream.writeBytes("Content-Disposition: form-data; name=\"data\"; filename=\"whatever\" \r\nContent-Type: image/jpeg\r\nContent-Transfer-Encoding: binary\r\n\r\n");
   dstream.write(bytes,0,bytes.length);
   
   // close the multipart form request
   dstream.writeBytes("\r\n--AXi93A--\r\n\r\n");
   dstream.flush();
   dstream.close();
   
   // read the output from the URL
   try{
     DataInputStream in =
       new DataInputStream(
     new BufferedInputStream(c.getInputStream()));
     String sIn = in.readLine();
     boolean b = true;
     
     while(sIn!=null){
       if(sIn!=null){
         if(popup) if(sIn.substring(0,folder.length()).equals(folder)) link(url+sIn, "_blank");
         System.out.println(sIn);
       }
       sIn = in.readLine();
     }
   }
   catch(Exception e){
     e.printStackTrace();
   }
 }
 catch(Exception e){
   e.printStackTrace();
 }
}

String aZ(int v)
{
 if(v<10) return "0"+v;
 return ""+v;
}

byte[] bufferImage(PImage srcimg){
 ByteArrayOutputStream out = new ByteArrayOutputStream();
 BufferedImage img = new BufferedImage(srcimg.width, srcimg.height, 2);
 img = (BufferedImage)createImage(srcimg.width, srcimg.height);
 for(int i = 0; i < srcimg.width; i++)
 {
   for(int j = 0; j < srcimg.height; j++)
   {
     int id = j*srcimg.width+i;
     img.setRGB(i,j, srcimg.pixels[id]);
   }
 }
 try{
   JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
   JPEGEncodeParam encpar = encoder.getDefaultJPEGEncodeParam(img);
   encpar.setQuality(1,false); // 0.0-1.0, force baseline (?)
   encoder.setJPEGEncodeParam(encpar);
   encoder.encode(img);
 }
 catch(FileNotFoundException e){
   System.out.println(e);
 }
 catch(IOException ioe){
   System.out.println(ioe);
 }
 return out.toByteArray();
}


On my server i put a folder called"processing" my script stuff from processing (the applets) and a folder called "saved" and a saveFile.php file. inside the saveFile.php - file is this script

<?
$Title = $_GET['title'];
$Ext = $_GET['ext'];
$folder = $_GET['folder'];
$savepath = dirname($_SERVER["PATH_TRANSLATED"]);

$filename = $Title.".".$Ext;
while(file_exists($folder."/".$filename))
 $filename = $Title."-".rand(2,500).".".$Ext;

if (is_uploaded_file($data))
{
  $newfile = $savepath."/".$folder."/".$filename;
  if (!copy($data, $newfile))
  {
     // if an error occurs the file could not
     // be written, read or possibly does not exist
     echo "Error #1 Uploading File.";
     exit();
  }else{
     echo $folder."/".$filename;
  }
}else{
     echo "Error #2 Uploading File.";
     exit();
}
?>

I did not change anything in that last code script.

still not working Sad

BTW: when talking about the popup -> look at this site. when saving the image a new window pops up. http://wliia.org/projects/spiro/

What sort of script do i have to use to show all uploaded images on one page? as you mentioned in your last sentence?

i'm sorry, i don't have any knowledge of php, but i hope we get it fixed anyway. thanks a lot for your help!!!!!!!

Re: save to web --- image export
Reply #7 - Jun 5th, 2008, 9:19pm
 
Ah, OK, good to have a working example... The only missing part is the full PHP code, but it shouldn't be hard to do.
For the popup window, I forgot there is a link() function in Processing, which can just do that.
OK, I will work a bit on the PHP side, which is nice because I wanted to explore this file upload thingy...
Re: save to web --- image export
Reply #8 - Jun 5th, 2008, 9:29pm
 
great.
yes, that's what i found out, that the whole script is working, just the php - part is missing in the example. if we get this running it should not be problematic anymore.
if you learn for your own projects, too --- great.
thanks for your help. makes me feel much better.


Re: save to web --- image export
Reply #9 - Jul 4th, 2008, 5:04pm
 
hello,

one question again.
i still did not find out how to modify the php script and where to put it.
i have my website e.g.
http://www.test.com

on this site i place the folder processing. inside this i place my processing applet.
so i type as the URL: http://www.test.com/processing.

inside of this i placed my php script as well, the file is named saveFile.php

When running my applet i get an outofbounce - error.

this is the php script of the file:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Unbenanntes Dokument</title>
</head>

<body>
</body>
</html>

//--------------------------------
//PHP FILE:
<?
$Title = $_GET['title'];
$Ext = $_GET['ext'];
$folder = $_GET['folder'];
$savepath = dirname($_SERVER["PATH_TRANSLATED"]);

$filename = $Title.".".$Ext;
while(file_exists($folder."/".$filename))
 $filename = $Title."-".rand(2,500).".".$Ext;

if (is_uploaded_file($data))
{
  $newfile = $savepath."/".$folder."/".$filename;
  if (!copy($data, $newfile))
  {
     // if an error occurs the file could not
     // be written, read or possibly does not exist
     echo "Error #1 Uploading File.";
     exit();
  }else{
     echo $folder."/".$filename;
  }
}else{
     echo "Error #2 Uploading File.";
     exit();
}
?>

Hope for help!

Re: save to web --- image export
Reply #10 - Jul 4th, 2008, 9:31pm
 
Hey

Did you make a folder inside the url (i.e. http://www.test.com/processing/) , called "saved" and set the chmod to 0777?

Cause that's where it saves the files, and if that isn't there, it will fail.

Also, remove the html before the <? tag.. don't need it.

Hope that helps.. if not, post again Smiley
Re: save to web --- image export
Reply #11 - Jul 4th, 2008, 9:50pm
 
Oh, excuse me, I started to write a robust PHP upload solution, but I halted just before the tests. Sad
I should finish this work, it might be of general interest.

Will try and post it here this week-end.
Re: save to web --- image export
Reply #12 - Jul 5th, 2008, 3:10pm
 
@ seltar:
i have a folder called saved inside the url with the correct settings...
still does not work Sad

@ PhiLho:
that sounds great. i think that will be of general interest, indeed.
thanks for your work. can't wait to test it Smiley
Re: save to web --- image export
Reply #13 - Jul 17th, 2008, 11:04am
 
First part of the solution, PHP side, not complete yet...

I post it to show I make progress... It works with a classical Web form, I have yet to test it with an applet.

Unlike what is done in Hacks page, I put a time stamp in the file name on the PHP side: it simplifies the applet, and the times are consistent as they all belong to the server's time zone! (as opposed to the time on the computer of the user).

You might wonder why I made a so convoluted program, since the input is controlled as it comes from a known source (the applet)...
Right? Wrong!

If I have access to such applet (and I have, it is in my browser, on my own computer), I can very easily (with some appropriate tools, though) find how it communicates with the server, where this one is located, and what kind of input it expects. Since it feeds back the uploaded image, I can even know where it stores them.
With this info, I can play with your server, either in a malicious way, or just to experiment, to find the limits...
I can make a little script uploading quickly some megabyte files (just below the limits set by PHP itself), just to see if I can saturate the server's disk...
I can try weird file names, so see how it reacts, or if I find a vulnerability.
I can even upload a PHP file, which, when called from my browser, would show the files on the server, what they content, allow me to change them or even to delete them...
And I am not even a cracker, just somebody with base knowledge of security for PHP scripts...

So I chose to careful inspect the input data: the maximum size of the file, the size of the name and the characters it uses, etc.
Actually, I can even totally ignore these names and make up my own: I can just increment a counter, for example, or use a more or less precise time stamp and/or the IP address of the uploader. Or perhaps I can use some provided info (a nickname for the user, for example), after sanitization, to be part of the destination path or file name.

Likewise, you can make variants of the script: check we don't reach a maximum size or number of files, and if so, delete the oldest files. Or just purge the files of the previous day, etc.
This can be added in the upload function itself, or just before calling it.

For convenience (syntax highlighting, easy copy) I have put the code in Pastebin: http://philho.pastebin.com/f74d8ffdb
I even plan to make a Web page on my site about it, there are some tricks worth sharing.
No paste here, it is a bit too long.
Will make a Hacks page too, when it will be mature enough.
Re: save to web --- image export
Reply #14 - Jul 17th, 2008, 11:32am
 
Dear Phil,

i'm deeply impressed by your work !!! as i don't have any idea of PHP i consider it really interesting what you've written about safety. that's why i plan to put my projects on a webserver where nothing else is online. no private stuff or anything like that.

hm, now i read through your script you've posted and i'm not sure how to use it yet. if i got you right, it's "just" a sketch of the final version and its not working yet with processing applets.
nevertheless, it's impressing and i'm really exciting about the final version and my first attempt to use it. (i think i'm gonna be extremely happy when i'm able to save my first pics on my webserver Smiley

greetings and thanks to you!

Pages: 1 2 3