Forum Moderators: coopster

Message Too Old, No Replies

Glue chunks to jpeg file

         

asantos

1:13 am on Jun 11, 2009 (gmt 0)

10+ Year Member



Hi, im developing an API for a java application. That java application only uploads 60kb max to the server, so if a picture is 100 kb, it uploads 60kb and the the other 40kb.

chunk.1.tmp (60kb)
chunk.2.tmp (40kb)

How can i glue them back together as a JPEG file in PHP?

I guess i should first glue them to a final.tmp file and then use imageCreateFromJPEG('final.tmp')... so as you can see, i only need help with the glue part.

Thanks!

asantos

1:29 am on Jun 11, 2009 (gmt 0)

10+ Year Member



It seems i already got it:

file_put_contents('chunk.tmp',file_get_contents($chunk,FILE_BINARY),FILE_APPEND);

Any ideas?

jatar_k

2:25 pm on Jun 11, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



>> Any ideas?

did it work?

asantos

3:36 pm on Jun 11, 2009 (gmt 0)

10+ Year Member



actually it did work. i meant... any ideas on how to improve that hehe.

jatar_k

3:39 pm on Jun 11, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



not really, I think that's fine

especially if it works ;)

asantos

4:07 pm on Jun 11, 2009 (gmt 0)

10+ Year Member



me again... it worked theorically... because it did glue the chunks together. Now im trying to port them to a jpeg file with:

$img = imageCreateFromJPEG($dir.'chunk.tmp');

the $img object doesnt seem to be a valid image... and the chunk.tmp weights around 20kb more than the original sliced up image that was uploaded from the java app.

Any ideas :S ?

[edited by: asantos at 4:12 pm (utc) on June 11, 2009]

asantos

4:11 pm on Jun 11, 2009 (gmt 0)

10+ Year Member



BTW, this is my code:

$dir = 'somedir/';
$file = $dir.'chunk.'.$_GET['i'].'.tmp';
if(move_uploaded_file($_FILES['chunk']['tmp_name'],$file)) {
# First file
if($_GET['i']==1) {
# Create main chunk file
file_put_contents($dir.'chunk.tmp',file_get_contents($file,FILE_BINARY),FILE_BINARY);
} else {
# Append chunk!
file_put_contents($dir.'chunk.tmp',file_get_contents($file,FILE_BINARY),FILE_APPEND);
}
# Last file
if($_GET['i']==$_GET['n']) {
# Build JPEG
$img = imageCreateFromJPEG($dir.'chunk.tmp');
}
} else {
die('error');
}

jatar_k

5:34 pm on Jun 11, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



no clue actually

I would guess at the java is changing/appending/terminating the chunks

possibly even adding in extra data in the beginning of the file

I would say you need to figure out what exactly the java is doing to the data before you can reassemble it

asantos

5:39 pm on Jun 11, 2009 (gmt 0)

10+ Year Member



found the error ;)
it seems the java code was resending the last byte of the chunk in the next chunk.

the code i posted works perfectly. i hope someone can use it too.

jatar_k

5:43 pm on Jun 11, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



nice catch