Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8963415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:21:00+00:00 2026-06-15T16:21:00+00:00

I have a javascript code like this var testCanvas = document.getElementById(‘canvas-1’); var canvasData =

  • 0

I have a javascript code like this

var testCanvas = document.getElementById('canvas-1');
var canvasData = testCanvas.toDataURL("image/png");
var ajax = new XMLHttpRequest();
ajax.open("POST",'http://www.domain.com/imgsave.php',true);
ajax.setRequestHeader('Content-Type', 'canvas/upload');
ajax.send("canvasData"+canvasData );

My php code is like this

if (isset($GLOBALS["HTTP_RAW_POST_DATA"]))
{
    // Get the data
    $imageData=$GLOBALS['HTTP_RAW_POST_DATA'];

    $filteredData=substr($imageData, strpos($imageData, ",")+1);

    $unencodedData=base64_decode($filteredData);
    $fp = fopen( 'test.png', 'wb' );
    fwrite( $fp, $unencodedData);
    fclose( $fp );
        echo "saved";
}
  else{

  echo "no raw data";
  }

When executing this code i got a zero size png file image? Whats the problem with my code?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T16:21:01+00:00Added an answer on June 15, 2026 at 4:21 pm

    I had to do this recently myself.

    First I put my canvasData into a hidden field and posted it to my PHP page.

    It comes back in this format: data:image/png;base64,iVBORw0......

    You need to split the data up first, as this: data:image/png;base64, is header information. The rest is the encoded data.

    $rawData = $_POST['imageData'];
    $filteredData = explode(',', $rawData);
    
    $unencoded = base64_decode($filteredData[1]);
    

    I then create the image on my server:

    //Create the image 
    $fp = fopen('sigs/test1.png', 'w');
    fwrite($fp, $unencoded);
    fclose($fp); 
    

    And then read it to do whatever I want with it.

    $file_name = 'test1.png';
    $file_size = strlen($filteredData[1])/1024; //This may be wrong, doesn't seem to break for me though.
    
    
    $fh = fopen('sigs/test1.png', 'r');
    $content = fread($fh, $file_size);
    $content = base64_encode($content);
    fclose($fh);
    

    I’m more than sure there is a much more elegant solution to this, but this has been working for me!

    Check this for more info (possibly): My own question

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi i have a javascript code like this var a=10; var b=100; document.write(b-a); Actually
I have javascript code some thing like this -- var count=3; var pl=new Array(count);
I have a JS code like this <script type=text/javascript> $(document).ready(function(){ $('.main').on('click', '.block', function (){
I have some javascript code like this, var worker = new Worker(javascript/worker.js); worker.onmessage =
Suppose I have some javascript code like this: function Person(){} var pro=Person.prototype; pro[setName]=function(){} pro[setAge]=function(){}
i have javascript code that looks like this: $('#lnkPopup').click(function() { var id = $(this).attr('rel');
I have some very simple javascript code that looks like this: var newWindow =
I have a javascript file which has some code somewhat like this: var Slide
I have code like this: <?php echo '<script type=text/javascript>'; echo 'var out='.$txt.';'; echo '</script>';
I have this code: <script> function EvalSound(soundobj) { var thissound=document.getElementById(soundobj); thissound.Play(); } </script> <embed

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.