PDA

View Full Version : How can I send a bitmap to a php file?



almost
01-25-2009, 02:39 AM
I need to use SCAR with a php file to take a bitmap saved on my computer and save it to a .bmp file on my hosting site where the php file is.

This should be possible because in SCAR you can do

program New;

var
Source: string;
FileNumber: integer;
Client: integer;

begin
FileNumber:= OpenFile('Path.bmp', false);
ReadFileString(FileNumber, Source, FileSize(FileNumber));
CloseFile(FileNumber);
FileNumber:= RewriteFile('Path2.bmp', false);
WriteFileString(FileNumber, Source);
CloseFile(FileNumber);
end.
It will take the image in Path.bmp and copy it to a new bitmap file, Path2.bmp.

So I figured this would work-
program New;

var
Source: string;
FileNumber: integer;
Client: integer;

begin
FileNumber:= OpenFile('Path.bmp', false);
ReadFileString(FileNumber, Source, FileSize(FileNumber));
CloseFile(FileNumber);
Client:= InitializeHTTPClient(true, true);
AddPostVariable(Client, 'pic', Source);
PostHTTPPageEx(Client, 'http://yoursite.php');
end.
It takes that same bmp information but sends it to the php file.
The php source is

<?php

$pic = $_POST['pic'];
if ($pic <> '')
{
$handle = fopen('pic.bmp', 'w');
fwrite($handle, $pic);
fclose($handle);
}

?>
It works as far as sending it there and the file pic.bmp gets written.
But not with the same information I sent to it.
The crazy little chars that make up a bmp file can't be sent from SCAR to a php file.
No matter what picture I send, it writes BMZ9 to the bmp file.

So is there any way to do this?
I don't have to use php, I'm just looking for any way possible to write a bmp file on the web from SCAR.

ian.
01-25-2009, 02:56 AM
couldn't you have it like.. open paint then paste the picture then upload that to a site? (sorry.. don't think that's what you want.. but I couldn't understand it =X)

almost
01-25-2009, 03:00 AM
Something like that but done in code.
It isn't supposed to use mouse movements or key presses.
It sends the information of a bitmap to the web, and that information is saved as a bitmap file, which can then be viewed by anyone from wherever it was saved.

ian.
01-25-2009, 03:01 AM
and are you wanting to get the picture of it or the bitmap? (like 1835693825y1987235tsdhgaiu2e9+
24htoung2ng031iugb1oiygbr

stuff..)

almost
01-25-2009, 03:09 AM
If you do

FileNumber:= OpenFile('Path.bmp', false);
ReadFileString(FileNumber, Source, FileSize(FileNumber));
CloseFile(FileNumber);
you will have the coding that makes up the bitmap in the variable "Source".
It is different looking than when you do something like
bmp := BitmapFromString(11, 12, 'beNr79unNyxt0Qy+fX31y/9zDu' +
'6ce3DmJjOAKgLLPHl6omrpCNbyZwaYEjuAKHt07DZRV92099/w/Mk' +
'JWANS758o3NARXALQdaOCuy193Xf72BwR+QxCyCUAF2y982XH xy69' +
'fP+EITcGWs5+3nvv848c3OEJTsPH0JyD69u0zHKEpWHviw9rj H799' +
'+wRFAA6sR7w=');
Because SCAR reads it different.
It looks like
BMZ9 6 ( ™ Ó$9
ÿ ÿ ÿ ÿ ÿ ÿ
And a bunch of weird chars.

ian.
01-25-2009, 03:11 AM
yeah, but what I'm asking is do you want the bmp like

bmp := BitmapFromString(11, 12, 'beNr79unNyxt0Qy+fX31y/9zDu' +
'6ce3DmJjOAKgLLPHl6omrpCNbyZwaYEjuAKHt07DZRV92099/w/Mk' +
'JWANS758o3NARXALQdaOCuy193Xf72BwR+QxCyCUAF2y982XH xy69' +
'fP+EITcGWs5+3nvv848c3OEJTsPH0JyD69u0zHKEpWHviw9rj H799' +
'+wRFAA6sR7w=');

or the picture of that? :o

almost
01-25-2009, 03:30 AM
I don't understand what you mean.
But if this helps any, the script won't use a single thing that looks like
bmp := BitmapFromString(11, 12, 'beNr79unNyxt0Qy+fX31y/9zDu' +
'6ce3DmJjOAKgLLPHl6omrpCNbyZwaYEjuAKHt07DZRV92099/w/Mk' +
'JWANS758o3NARXALQdaOCuy193Xf72BwR+QxCyCUAF2y982XH xy69' +
'fP+EITcGWs5+3nvv848c3OEJTsPH0JyD69u0zHKEpWHviw9rj H799' +
'+wRFAA6sR7w=');
It just gets the information in a bitmap and sends it.
That pretty much sums it up.

ian.
01-25-2009, 03:59 AM
ok, you wantbmp := BitmapFromString(11, 12, 'beNr79unNyxt0Qy+fX31y/9zDu' +
'6ce3DmJjOAKgLLPHl6omrpCNbyZwaYEjuAKHt07DZRV92099/w/Mk' +
'JWANS758o3NARXALQdaOCuy193Xf72BwR+QxCyCUAF2y982XH xy69' +
'fP+EITcGWs5+3nvv848c3OEJTsPH0JyD69u0zHKEpWHviw9rj H799' +
'+wRFAA6sR7w=');

to be sent to your website right?

what I thought you meant was you want it to send a picture to your webby.. like convert the bitmapfromstring to a picture then send the picture :p

if you want the bmp := BitmapFromString(11, 12, 'beNr79unNyxt0Qy+fX31y/9zDu' +
'6ce3DmJjOAKgLLPHl6omrpCNbyZwaYEjuAKHt07DZRV92099/w/Mk' +
'JWANS758o3NARXALQdaOCuy193Xf72BwR+QxCyCUAF2y982XH xy69' +
'fP+EITcGWs5+3nvv848c3OEJTsPH0JyD69u0zHKEpWHviw9rj H799' +
'+wRFAA6sR7w=');

copy and paste it into a text file, send the file to your webby..

I hope that's what you mean =X

almost
01-25-2009, 04:27 AM
No

But if this helps any, the script won't use a single thing that looks like
BitmapFromString won't do anything for this.
That is just used for SCAR.
It reads the bitmap file to get the information.

ian.
01-25-2009, 04:28 AM
I'm really confused.. :'(

you think you could tell me it in terms an idiot would understand? :p

A G E N T
01-25-2009, 05:00 AM
SCAR does mess up the string encoding - looking at them side by side in Notepad++ and SCAR, I think the problem is that SCAR treats all of the various header bytes (SOH, NUL, CAN) as 'unknown' and probably assigns some strange value to them, which corrupts the file when it is sent over the 'net.
If you really want to, you could use cURL (even in a plugin), as it supports multi-part POST (i.e, file upload).

EDIT: P.S, to those who are confused, if I'm reading this right, he wants to read a local file in SCAR, then POST it to his webpage which will save it on the server.

ian.
01-25-2009, 05:37 AM
EDIT: P.S, to those who are confused, if I'm reading this right, he wants to read a local file in SCAR, then POST it to his webpage which will save it on the server.

so couldn't you just copy and paste the stuff instead of saving the file? or save the file as .txt then manually paste in scar :o

almost
01-25-2009, 05:45 AM
Agent has the right idea.
I will try to explain it the best I can.

You have a picture on your computer.
Let's say it is called picture.bmp
You can get the coding of that picture by reading the file with SCAR.
You put that coding into a variable called Source.
You send that variable to the web.
It takes that information and writes it to a file.
Let's say that file is www.thesite.com/picture2.bmp
Now your picture, picture.bmp, is available to be seen by anyone at www.thesite.com/picture.bmp
It's like uploading a picture on photobucket or something I guess, but it has to be done with coding.

ian.
01-25-2009, 05:56 AM
oh I get it now..

well one more thing..

you want to do this

BitmapToString

load the string to the webby

BitmapFromString

so it will show the pic..

right?

and you will somehow run scar on the webby? >.>

A G E N T
01-25-2009, 03:32 PM
No...
He wants to take a file from his computer, and load it in SCAR in a way that it can be sent with regular HTTP POST (i.e, as a string).
He sends the string to his webpage, which saves it as a picture file, but the problem is that SCAR doesn't encode the header bytes properly, so the data is not sent correctly.

EDIT:
One more thing I can think of, you might try urlencoding the data before you send it.

ian.
01-25-2009, 03:46 PM
oh ok :p

I dunno then :\

almost
01-25-2009, 04:55 PM
I think SCAR actually does encode it correctly and keep the string the way it is supposed to be, because you can get the information from your picture using SCAR and instead of sending it to a picture file on the web, send it to a picture file on your computer.
Just like in the example I posted in the first post, it does work.
But something about my php file getting the variable, it then turns it into pretty much nothing.

Zyt3x
01-25-2009, 07:47 PM
Hey, 99_: He wants to make a code that uploads a bitmap from your computer to the internet, like imageshack.us does but done in scar.

Dan Cardin
01-25-2009, 07:56 PM
couldnt you put more of the code on your server and do as little as possible in SCAR? by just sending the path with SCAR and having your php/perl script upload it to the server?

A G E N T
01-25-2009, 10:07 PM
Yeah, seems you're right - SCAR is encoding it fine, but it gets garbled during the transmission... Since I didn't feel like making a url-encode function, I did it this way (easy).
Take each character, encode with ord(), then PHP end will decode with chr().

program New;

var
Source : string;
FileNumber, i: integer;
Client: integer;

begin
FileNumber:= OpenFile('C:\bmp.bmp', false);
ReadFileString(FileNumber, Source, FileSize(FileNumber));
CloseFile(FileNumber);
Client := InitializeHTTPClient(false, false);
for i := 1 to length(source)-1 do
begin
AddPOSTVariable(Client, IntToStr(i), inttostr(ord(Source[i])));
end;
POSTHTTPPageEx(Client, 'http://127.0.0.1/scarfile.php');
end.




<?PHP
$s = "";
foreach($_POST as $value)
{
$s .= chr($value);
}
file_put_contents("./up.bmp", $s);
?>

Of course you might want to add more security so people don't go uploading nasty things to your server...

almost
01-26-2009, 12:13 AM
That actually worked, but there is still a problem.
It takes over 5 minutes to send the whole computer screen.
It can do a 30 X 30 bitmap in a few seconds though.
But what I'm using this for will need to use a bitmap almost the size of the screen and it will be doing it over and over.
I think there is a way to send it all at once, or at least in chunks, instead of every byte one by one.


couldnt you put more of the code on your server and do as little as possible in SCAR? by just sending the path with SCAR and having your php/perl script upload it to the server?
That would be ideal, but I have learned only the very basics of php and I don't know perl.
Actually if that's possible I will learn how and do it that way.

Dan Cardin
01-26-2009, 01:28 AM
perl - you need perl installed on your server.
you'll need to change the 1024 *5000 if you want to upload things bigger than 5MB

#!/usr/bin/perl -wT

use strict;
use CGI;
use CGI::Carp qw ( fatalsToBrowser );
use File::Basename;

$CGI::POST_MAX = 1024 * 5000;
my $safe_filename_characters = "a-zA-Z0-9_.-";
my $upload_dir = "/home/mywebsite/htdocs/upload";

my $query = new CGI;
my $filename = $query->param("photo");
my $email_address = $query->param("email_address");

if ( !$filename )
{
print $query->header ( );
print "There was a problem uploading your photo (try a smaller file).";
exit;
}

my ( $name, $path, $extension ) = fileparse ( $filename, '\..*' );
$filename = $name . $extension;
$filename =~ tr/ /_/;
$filename =~ s/[^$safe_filename_characters]//g;

if ( $filename =~ /^([$safe_filename_characters]+)$/ )
{
$filename = $1;
}
else
{
die "Filename contains invalid characters";
}

my $upload_filehandle = $query->upload("photo");

open ( UPLOADFILE, ">$upload_dir/$filename" ) or die "$!";
binmode UPLOADFILE;

while ( <$upload_filehandle> )
{
print UPLOADFILE;
}

close UPLOADFILE;


and i cannot find a way to do it in php without having a form, and user interaction.

EDIT: JK, i dont think that will do it. I think that prompts the user :(. Nvm, i dont think its possible to do it automatically.

almost
01-26-2009, 03:37 AM
I think most methods of uploading images require some sort of user interaction.
Uggghh.
I still know it can be done though.

A G E N T
01-26-2009, 05:03 AM
This is the only way I could think of to encode long strings - it doesn't really have to be split to chunks, but it will avoid heavy lag on larger strings... Of course it's still a lot slower than a plugin, but it can do 500x500px bitmap in about 30s.
SCAR end:

program New;
const
LeastChunks = 10; //number of chunks to try splitting to -
//script will find a number that divides cleanly
var
Source, Enc : string;
FileNumber, i, l, chunks : integer;
Client: integer;

function inttohex(i:longint):string;
var
n:longint;
s, lookup :string;
begin
lookup := '0123456789ABCDEF';
n:=i;
if i=0 then s:='00' else s:='';
while n>0 do
begin
s:=lookup[n mod 16+1]+s;
n:=n div 16;
end;
while length(s)<2 do s:='0'+s;
result:=s;
end;

function StrToHex(str:string):String;
var
i : integer;
begin
Result := '';
for i := 1 to length(str) do
begin
Result := Result + inttohex(ord(str[i]));
end;
end;

begin
FileNumber:= OpenFile('C:\insane.bmp', false);
ReadFileString(FileNumber, Source, FileSize(FileNumber));
CloseFile(FileNumber);
l := length(source);
for i := leastchunks to 9999 do
begin
chunks := i;
if (l mod chunks) = 0 then Break;
end;
Client := InitializeHTTPClient(false, false);
for i := 1 to chunks do
Enc := Enc + StrToHex(copy(Source, (((l div chunks) * i) - (l div chunks))+1, (l div chunks)));

AddPOSTVariable(Client, 'enc', Enc);
POSTHTTPPageEx(Client, 'http://127.0.0.1/scarfile.php');
end.

PHP End:


<?PHP
function hexstr($hex)
{
$string="";
for ($i=0;$i<strlen($hex)-1;$i+=2)
$string.=chr(hexdec($hex[$i].$hex[$i+1]));
return $string;
}
$s = "";
foreach($_POST as $value)
{
echo $value;
$s .= hexstr($value);
}
file_put_contents("./up.bmp", $s);
?>

almost
01-26-2009, 11:54 PM
I tried that one and everything ran but I didn't end up with a picture somehow.
And I've tried to get it to work all sorts of ways.

Also if I can't get it down to about 1 second per pic or less I don't think I'll be able to do what I'm working on.

I appreciate the help though, I'm getting somewhere.
repped

A G E N T
01-27-2009, 02:21 AM
Yeah, well without using some sort of precompiled/binary program, I'm not sure how else to do this. Good luck :)

fastler
02-23-2009, 05:10 AM
Looks like your best chance is to send the file to the server using SCAR's bitmap-to-string function
A php script that could by itself, with no user interaction, upload a file would be ideal...
I played around with this but to no avail...

PS
Something just popped into my head
If this is a script intended only for your use you could make a php script to upload a file and a firefox extension that would automatically fill out the form and do all necessary things to upload
all scar would have to do is open up the php script in firefox(not that hard just set firefox as default browser and use scar's OpenWebPage function)
If you want help using this method just email me : centillion50@yahoo.com

~Fastler

Laser
04-01-2009, 02:52 AM
Well, this thread is a few months old but here's another working answer:

server side:

<?php

if($_SERVER['REQUEST_METHOD'] == "POST" && strlen($_POST['pic']) > 0) {
file_put_contents('out.bmp',base64_decode($_POST['pic']));
print "picture uploaded";
} else print "unknown request";

?>


client side:
program New;

const
Codes64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvw xyz0123456789+/';

//from http://www.swissdelphicenter.ch/torry/showcode.php?id=1524
//the codes const needed to be changed around
function Encode64(S: string): string;
var
i: Integer;
a: Integer;
x: Integer;
b: Integer;
begin
Result := '';
a := 0;
b := 0;
for i := 1 to Length(s) do
begin
x := Ord(s[i]);
b := b * 256 + x;
a := a + 8;
while a >= 6 do
begin
a := a - 6;
x := b div (1 shl a);
b := b mod (1 shl a);
Result := Result + Codes64[x + 1];
end;
end;
if a > 0 then
begin
x := b shl (6 - a);
Result := Result + Codes64[x + 1];
end;
end;

var
Source: String;
FileNumber: integer;
Client: integer;

begin
FileNumber:= OpenFile('path.bmp', false);
ReadFileString(FileNumber, Source, FileSize(FileNumber));
CloseFile(FileNumber);
Client:= InitializeHTTPClient(true, true);
AddPostVariable(Client, 'pic', Encode64(Source));
PostHTTPPageEx(Client, 'http://yourdomain.com/pic.php');
end.

I got the Base64 encode function from http://www.swissdelphicenter.ch/torry/showcode.php?id=1524

almost
05-05-2009, 10:33 AM
That's faster than any other way I have tried, but it still won't work with what I need it to do.
I'm pretty sure I'll have to use a plugin or another language.