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 JPG to webserver <code>
Page Index Toggle Pages: 1
Save JPG to webserver <code> (Read 3643 times)
Save JPG to webserver <code>
Jan 25th, 2006, 9:39pm
 
I rewrote my entire "post jpg to web"-code, and got a significant increase in speed.

Usage:
Code:

void saveJPG()
{
println("SAVING JPG START");
postData(bufferImage(get(0,0,width,height)).toByteArray());
println("SAVING JPG STOP");
}


Code:
Code:

import com.sun.image.codec.jpeg.*;

void postData(byte[] bytes)
{
try{
String url = "http://YourWebURL/";
URL u = new URL(url+"saveJpg.php");
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, (in this case it's an image file)
dstream.writeBytes("Content-Disposition: form-data; name=\"img\"; 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(sIn.substring(0,5).equals("saved")) link(url+sIn, "_new");
System.out.println(sIn);
}
sIn = in.readLine();
}
}
catch(Exception e){
e.printStackTrace();
}
}
catch(Exception e){
e.printStackTrace();
}
}

ByteArrayOutputStream 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;
}


PHP: (assumed saveJpg.php)
Code:

<?
$Title = "ImageTitle";

$savepath = dirname($_SERVER["PATH_TRANSLATED"]);

$filename = $Title."_".date("Ymd-His").".jpg";
while(file_exists("saved/".$filename))
$filename = $Title."_".date("Ymd-His")."-".rand(2,500).".jpg";

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


This code assumes you have a folder named "saved" in the same folder you have saveJpg.php in.
The folder "saved" should also be set to writable (chmod 777).

hope you guys will enjoy!

-seltar
Re: Save JPG to webserver <code>
Reply #1 - Jun 12th, 2006, 2:32am
 
Dear Seltar,

I've been trying to use both your codes (the old one and this new one) since 48 hours and I recognize that I encountered some difficulties.

Anyway, this new version (my current version only includes additional println, echo and printf... for debugging) seems to work almost correctly. The PHP code receives the image: is_uploaded_file($img) is TRUE.

Unfortunately, the copy($img, $newfile) remains desperately FALSE and a move_uploaded_file(...) is similar. Pre-existence of $newfile (or not) has no influence, and if it exists, it has been chmoded 777 previously.

Exploring the global $_FILES indicates that the properties of 'img' are respectively:
$_FILES['img']['name'] = whatever\"
$_FILES['img']['type'] = image/jpeg
$_FILES['img']['size'] = 2715
$_FILES['img']['tmp_name'] = /tmp/phpXxXwVm
$_FILES['img']['error'] = 0

Since I am not perfectly at ease with PHP, I probably have missed something. Can it be related to windows XP, Firefox, php.ini or something like that ?

Thanks in advance,
JMC
Re: Save JPG to webserver <code>
Reply #2 - Jun 12th, 2006, 7:46am
 
the folder "saved" that i set in the variable $newfile, is a folder, which has to have chmod 777 as well.
You can just remove that, but whatever folder you are trying to write to, has to have chmod 777

I'm hoping that this will solve your problems, cause if not i'm not quite sure.. unless you show me the code you're using.

I'm on the same system as you, so that shouldn't matter..

best
seltar
Re: Save JPG to webserver <code>
Reply #3 - Jun 14th, 2006, 9:44pm
 
It works !!! In fact the problem was not the permissions since both folder and file were 777. Appearingly, the problem was in the line
$newfile = $savepath."/saved/".$filename;

Changing to
$newfile = $savepath."saved/".$filename;
makes it work.

Thanks a lot for help and emulation.
Best regards,
JMC
Re: Save JPG to webserver <code>
Reply #4 - Aug 22nd, 2006, 1:22am
 
Dear Seltar,

As I said earlier, saving JPG to server works quite well. The application I developed is a collaborative drawing website (http://www.drawover.com), where users can draw via a Processing interface, save their image that will be modified later by someone else.

Nevertheless a mistery remains: some of the images appear properly in the browser but some others do not (see menu VIEW for example). After discussion with users, it appeared that the "bad" pictures had been generated from a Mac and are reproducibly "bad". After some analysis, I found that these JPG images contain 4 channels (CMYB) whereas all others only contain 3 channels (RGB).

It is interesting to notice that Processing's "loadimage" can cope with both these formats without any problem. But PHP and HTML cannot...

So my question is: do you know a way to force the JPG generator to work with only 3 channels ? or is it related to an extra parameter in the environment of the user's computer ?

Thanks in advance,
Best regards,
JMC

PS: it is probably easier to convert CMYB to RGB in the PHP, I did not try yet.
Re: Save JPG to webserver <code>
Reply #5 - Aug 23rd, 2006, 8:32pm
 
I noticed that problem too, but couldn't for the life of me figure out why..
Anybody have any pointers here? Converting CMYB to RGB?

-seltar
Re: Save JPG to webserver <code>
Reply #6 - Aug 26th, 2006, 10:51pm
 
I made an example that does something similar, haven't tested it fully, but does the CMYB vs. RGB happen with this code too? (i'm only working on a mac right now so can't tell)

http://www.shiffman.net/2006/05/02/upload/

Dan
Re: Save JPG to webserver <code>
Reply #7 - Dec 16th, 2006, 12:47am
 
Dan, All,

first off, thanks for this class... it's made my life significantly easier.  

i've been struggling lately trying to adapt what you've done there such that i can upload to a server while running in a browser, which has necessitated finding a way around saving a local jpg.  i think i’ve managed something which works, and thought i’d share the results:

so, i haven’t changed much, basically instead of using a locally saved jpg to create the InputStream for your pipe() function, i’m using a ByteArrayOutputStream created from a BufferedImage.  also, i’ve added a little bit so that we can set the target image dimensions.  everything else is pretty much the same, and i think the class will still work with the php stuff that dan’s got posted in this link above.

hope someone finds this useful,

kyle

http://www.ksteinfe.net/public_lib/JPGMakerUploader/src.zip
Re: Save JPG to webserver <code>
Reply #8 - May 10th, 2007, 11:31am
 
Hi,

It seems like Dan-Kyle's solution does work through a browser both with PC and Mac. I added a few lines from Seltar's example to control the JPEG quality. It's here:

http://joan.cat/codi/jpgup.zip

-joan
Re: Save JPG to webserver <code>
Reply #9 - May 10th, 2007, 8:14pm
 
Really really nice!
Spotted two small mishaps in the timestamp function.. day() instead of hour() and minute()..

fixed:
Code:

//add hour with two characters
if(hour()<10)stamp += "0"+hour();
else stamp+= hour();

//add minute with two characters
if(minute()<10)stamp += "0"+minute();
else stamp+= minute();


-seltar
Re: Save JPG to webserver <code>
Reply #10 - May 10th, 2007, 8:34pm
 
oups! Thank you! It's fixed now:
http://joan.cat/codi/jpgup.zip

j
Re: Save JPG to webserver <code>
Reply #11 - Feb 2nd, 2008, 2:58pm
 
hey buddys,

i get the first Save JPG to webserver code working for me. now i am trying to use the latest version from "jpgup.zip"..

my problem:

if i would run the sketch p5 says: expecting EOF, found 'PFont'

what does it mean and what could i do?



thx alot, great work!

(or does somebody had fixed the problem with cmyk???)
Re: Save JPG to webserver <code>
Reply #12 - Feb 2nd, 2008, 9:42pm
 
the color bug seem to be a osx tiger problem. with leopard it works perfect..
Re: Save JPG to webserver <code>
Reply #13 - Feb 11th, 2008, 10:42am
 
Someone ever resolved that color bug?
With Dan's first solution everything was working fine (although not online). But Joans last version also got me that strange color bug (running Vista).
Was this caused by Kyle's InputStream addition? (his original upload is missing)

..update..

Even stranger.. when viewing the output image in Firefox it says: "The image “(...).jpg” cannot be displayed, because it contains errors."
Lets see...

..update..

Ah, it wasn't a color bug, it only looked like one. It was the TYPE_INT_ARGB format that was the problem, deleting it fixed it.
Page Index Toggle Pages: 1