PHP Data-URI to file

51,282

Solution 1

A quick look at the PHP manual yields the following:

If you want to save data that is derived from a Javascript canvas.toDataURL() function, you have to convert blanks into plusses. If you do not do that, the decoded data is corrupted:

$encodedData = str_replace(' ','+',$encodedData);
$decodedData = base64_decode($encodedData);

Solution 2

The data URI you have in your example is not a valid PNG image. This will never work and is unrelated to the code, it's related to the data.


Does not apply but might be of interest:

file_put_contents($_POST['logoFilename'], file_get_contents($data));

The idea behind: PHP itself can read the contents of data URIs (data://) so you don't need to decode it on your own.

Note that the official data URI scheme (ref: The "data" URL scheme RFC 2397) does not include a double slash ("//") after the colon (":"). PHP supports with or without the two slashes.

 # RFC 2397 conform
 $binary = file_get_contents($uri);

 # with two slashes
 $uriPhp = 'data://' . substr($uri, 5);
 $binary = file_get_contents($uriPhp);

Solution 3

The all code that works :

$imgData = str_replace(' ','+',$_POST['image']);
$imgData =  substr($imgData,strpos($imgData,",")+1);
$imgData = base64_decode($imgData);
// Path where the image is going to be saved
$filePath = $_SERVER['DOCUMENT_ROOT']. '/ima/temp2.png';
// Write $imgData into the image file
$file = fopen($filePath, 'w');
fwrite($file, $imgData);
fclose($file);
Share:
51,282
GAgnew
Author by

GAgnew

I am a software developer in Ontario.

Updated on August 31, 2021

Comments

  • GAgnew
    GAgnew over 2 years

    I have a data URI I am getting from javascript and trying to save via php. I use the following code which gives a apparently corrupt image file:

      $data = $_POST['logoImage'];
    
      $uri = substr($data,strpos($data,",")+1);
    
      file_put_contents($_POST['logoFilename'], base64_decode($uri));
    
    
    
    data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs 9AAAACXBIWXMAAAsTAAALEwEAmpwYAAAAxklEQVQYlYWQMW7CUBBE33yITYUUmwbOkBtEcgUlTa7COXIVV5RUkXKC5AxU EdyZVD4kyKxkwIrr9vd0c7Oih aopinLNsF6Qkg2XW4XJ7LGFsAAcTV6lF5/jLdbALA9XDAXYfthFQVx OrmqKYK88/7rbbMFksALieTnzu9wDYTj6f70PKsp2kwAiSvjXNcvkWpAfNZkzWa/5a9yT7fdoX7rrB7hYh2fXo9HdjPYQZu3MIU8bYIlW20y0RUlXG2Kpv/vfwLxhTaSQwWqwhAAAAAElFTkSuQmCC
    

    Below the code is the actual image as a Data-URI. 'logoImage' is the string above, and $uri is the string minus 'image/jpeg;base64,'.

  • GAgnew
    GAgnew almost 13 years
    This worked. Wow, I can't believe I could not find this earlier! I'll accept this as soon as I can, thanks @Graydot
  • andreszs
    andreszs over 9 years
    And finally: file_put_contents($_POST['logoFilename'], $binary);
  • Andz
    Andz almost 9 years
    We're on PHP 5.5.25, and it seems it is not necessary to add the double slashes.
  • hakre
    hakre almost 9 years
    @Andz: Looks like with PHP 5.2+ this always worked: 3v4l.org/Fk4RY - very nice Comment on PHP.net is years old, too. Don't trust the map, trust the terrain, thanks again for the hint. Corrected the answer.
  • Jake
    Jake about 8 years
    !! I have tried a TON of answers on SO and elsewhere, and this is the only thing that worked! It worked! Thanks.
  • Joel
    Joel over 7 years
    This is a great answer, no idea this was built-in!
  • Kamlesh
    Kamlesh almost 4 years
    Your solution did not work for me. I have tried file_put_contents('instagram.com/nasa/?__a=1') but does not work.
  • P A S H
    P A S H over 3 years
    This alone was not enough for me. I also had to strip everything up to the first comma, as well as the comma, before escaping the spaces with pluses: $decoded = Base64::decode(PHP\str_replace(' ','+',PHP\explode(',', $imageDataURL)[1]));