Php Saving BASE64 Raw Code

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

simonstaton
Forum Newbie
Posts: 11
Joined: Fri Jul 02, 2010 6:07 am

Php Saving BASE64 Raw Code

Post by simonstaton »

Hi,

I was wondering I have an online applet that generates an image and shows it on the page however the "src=" is raw base64 binary code which I know only shows in firefox. Now is there anyway for php to detect this image by its id and then save it as a temp file on the server.

if this is possible would I then be able to have it attached somehow in my email form as an actual .png attachment and not the raw binary code.

if you would like to see my applet so you get an idea of what I am trying to do you can visit it here:
http://www.coding-development.co.uk/BGH.html

You may need to download googles o3d software to actually build a climbing frame in the applet however to get an idea of what I mean you can use the export image at the top and the email model options to see my form and see how the image is generated.

Simon
yochai20
Forum Newbie
Posts: 6
Joined: Tue May 18, 2010 4:27 pm

Re: Php Saving BASE64 Raw Code

Post by yochai20 »

You Can get the base 64 string through javascript, and send it to php script, then all what you need to do is save the binary content of the image into a file, you can parse the string content with base64_decode.
simonstaton
Forum Newbie
Posts: 11
Joined: Fri Jul 02, 2010 6:07 am

Re: Php Saving BASE64 Raw Code

Post by simonstaton »

I have managed to get the base64 code over to the php code through javascript, so say for example I have it displaying as text inside the sent email. but how could I have it as a file attached to the email. so is there a function I could use that will compress the base64 code into an actual .png file to be emailed.
simonstaton
Forum Newbie
Posts: 11
Joined: Fri Jul 02, 2010 6:07 am

Re: Php Saving BASE64 Raw Code

Post by simonstaton »

hmm I found this online program that can convert the Base64 String back into a .png file any ideas on how it works and how I could do the same thing but have it saving to my server?

http://www.motobit.com/util/base64-decoder-encoder.asp
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: Php Saving BASE64 Raw Code

Post by Jade »

What I would do is decode the image and then re-create it with GD.

Take a look at these:
http://php.net/manual/en/function.base64-decode.php
http://php.net/manual/en/book.image.php
simonstaton
Forum Newbie
Posts: 11
Joined: Fri Jul 02, 2010 6:07 am

Re: Php Saving BASE64 Raw Code

Post by simonstaton »

Thankyou, the first link is very helpful, however I am confused to what GD does. will it turn the decoded base64 into an image? All I rearlly need to do is to get the base64 image from my client and get it inside an email any other ideas on how I could do this? I already have the base64 displaying the image on the first window and I have a form emailing just not sure how to get the information over and in what way should I get the information over.
simonstaton
Forum Newbie
Posts: 11
Joined: Fri Jul 02, 2010 6:07 am

Re: Php Saving BASE64 Raw Code

Post by simonstaton »

I was looking at this from the link you gave me however I am terrible with php and its like running through a maze blind. do you think the following could save a base64 string as a .png to my server?


<?php
function base64_to_jpeg( $inputfile, $outputfile ) {
/* read data (binary) */
$ifp = fopen( $inputfile, "rb" );
$imageData = fread( $ifp, filesize( $inputfile ) );
fclose( $ifp );
/* encode & write data (binary) */
$ifp = fopen( $outputfile, "wb" );
fwrite( $ifp, base64_decode( $imageData ) );
fclose( $ifp );
/* return output filename */
return( $outputfile );
}
?>
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: Php Saving BASE64 Raw Code

Post by Jade »

That will convert the file back to a jpg but it won't "save" it to your server. You'll need to use something like file_put_contents to do that, otherwise you'll need to open the file, read it in, write it to the server and close the stream when you're done.

http://www.php.net/manual/en/function.f ... ntents.php
simonstaton
Forum Newbie
Posts: 11
Joined: Fri Jul 02, 2010 6:07 am

Re: Php Saving BASE64 Raw Code

Post by simonstaton »

Hi Jade,

any idea on how I could use that code to turn it into a jpeg?

I have the base64 code comming through raw and not as a file so how could I change the "$inputfile" to be raw base64 code?

Simon
simonstaton
Forum Newbie
Posts: 11
Joined: Fri Jul 02, 2010 6:07 am

Re: Php Saving BASE64 Raw Code

Post by simonstaton »

anyone?
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: Php Saving BASE64 Raw Code

Post by Jade »

Are you uploading these files or are they already on your server?
simonstaton
Forum Newbie
Posts: 11
Joined: Fri Jul 02, 2010 6:07 am

Re: Php Saving BASE64 Raw Code

Post by simonstaton »

Hi Jade,

the images arnt saved anywhere it is simply a base64 string of code that generates the image in the "src="....." to see what I am doing visit the following link, http://www.coding-development.co.uk/BGH.html you may need to download googles o3d to view the models in 3d however if you drag in an item from the right sidebar into the window then at the top click export image then look at the code for that image you will see how it is made.

Simon
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: Php Saving BASE64 Raw Code

Post by Jade »

Do you have GD installed? What about trying something like this: http://us2.php.net/manual/en/function.i ... omjpeg.php
simonstaton
Forum Newbie
Posts: 11
Joined: Fri Jul 02, 2010 6:07 am

Re: Php Saving BASE64 Raw Code

Post by simonstaton »

Hi Jade, I dont currently have GD installed as im still not sure on how to use it, im also confused as to what the fucntion in that link does, does it save the base64 as a jpeg?

Many Thanks,
Simon
simonstaton
Forum Newbie
Posts: 11
Joined: Fri Jul 02, 2010 6:07 am

Re: Php Saving BASE64 Raw Code

Post by simonstaton »

Hi Jade, I dont currently have GD installed as im still not sure on how to use it, im also confused as to what the fucntion in that link does,

Thanks,
Simon
Post Reply